Skip to content

Instantly share code, notes, and snippets.

@ceberous
Last active June 8, 2018 07:40
Show Gist options
  • Select an option

  • Save ceberous/acb6a0da22e99e3b365c647c399d55fd to your computer and use it in GitHub Desktop.

Select an option

Save ceberous/acb6a0da22e99e3b365c647c399d55fd to your computer and use it in GitHub Desktop.
Twitch Chat Timeout / Ban Non English
const process = require( "process" );
const path = require( "path" );
const cp = require( "child_process" );
function get_node_global_path() {
try {
const output = cp.spawnSync( "npm" , [ "root" , "-g" ] , { encoding : "utf8" } );
return output.stdout.trim();
}
catch( error ) { console.log( error ); process.exit( 1 ); }
}
const NodeGlobalPath = get_node_global_path();
//const currentPath = process.cwd();
const tmi = require( path.join( NodeGlobalPath , "tmi.js" ) );
const translate = require( path.join( NodeGlobalPath , "google-translate-api" ) );
// Config
// =============================================
// =============================================
//const TwitchKeys = require( "./personal.js" );
const TwitchKeys = {
username: "" ,
oauth: ""
};
const channel_name = "";
const timeout_time = 180000; // 3 minutes
const timeout_reason = "not using english";
const ban_reason = "not using english";
// =============================================
// =============================================
process.on( "unhandledRejection" , function( reason , p ) {
console.error( reason, "Unhandled Rejection at Promise" , p );
console.trace();
});
process.on( "uncaughtException" , function( err ) {
console.error( err , "Uncaught Exception thrown" );
console.trace();
});
var h_channel = process.argv[ 2 ] || channel_name
h_channel = "#" + h_channel;
function is_english( message ) {
return new Promise( async function( resolve , reject ) {
try {
var answer = [ true , "en" ];
translate( message , { to: "en" } ).then( res => {
//console.log(res.text);
//console.log( res.from.language.iso );
if ( answer[ 1 ] !== "en" ) {
answer[ 0 ] = false;
answer[ 1 ] = res.from.language.iso;
}
resolve( answer );
}).catch( err => {
answer[ 0 ] = false;
answer[ 1 ] = err;
resolve( answer );
});
}
catch( error ) { answer[ 0 ] = false; answer[ 1 ] = err; resolve( answer ); }
});
}
async function moderate( from , to , text , message ) {
if ( !text ) { return; }
if ( text.length < 1 ) { return; }
const message_is_english = await is_english( text );
console.log( message_is_english );
if ( message_is_english[ 0 ] === false ) {
try {
console.log( "Timing Out " + to + " for chatting in '" + message_is_english[ 1 ] + "'" );
console.log( text );
// twitchIRCClient.timeout( h_channel , to , timeout_time , timeout_reason );
// twitchIRCClient.ban( h_channel , to , ban_reason );
// twitchIRCClient.say( h_channel , to + " was banned for chatting in " + message_is_english[ 1 ] );
}
catch( e ) { console.log( e ); }
}
}
var twitchIRCClient = undefined;
( async ()=> {
twitchIRCClient = new tmi.client({
identity: {
username: TwitchKeys.username ,
password: TwitchKeys.oauth ,
},
channels: [ h_channel ]
});
await twitchIRCClient.connect();
twitchIRCClient.on( "message" , moderate );
console.log( "connected to : " + h_channel );
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment