Skip to content

Instantly share code, notes, and snippets.

@ceberous
Last active September 14, 2019 20:50
Show Gist options
  • Select an option

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

Select an option

Save ceberous/ce74e7909981bbb7acac57d821aff749 to your computer and use it in GitHub Desktop.
Twitch Block / Ignore Everyone
const process = require( "process" );
const path = require( "path" );
const child = require( "child_process" );
const global_package_path = child.spawnSync( 'npm', [ 'root' , '-g' ] , { encoding: 'utf8' } ).stdout.trim();
const request = require( path.join( global_package_path , "request" ) );
const tmi = require( path.join( global_package_path , "tmi.js" ) );
const JFODB = require( path.join( global_package_path , "jsonfile-obj-db" ) );
const db = new JFODB( "already_blocked" );
if ( !!!db.self.already_blocked ) { db.self.already_blocked = {}; db.save(); }
const PersonalPivotFilePath = path.join( process.env.HOME , ".config" , "personal" , "twitch_helper.js" );
const PersonalPivot = require( PersonalPivotFilePath );
const Personal = require( "./personal.js" ).twitch;
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();
});
function BlockUser( username ) {
return new Promise( async function( resolve , reject ) {
try {
const blocking_userid = await GET_USER_ID( username );
if ( !!db.self.already_blocked[ blocking_userid ] ) { resolve(); return; }
console.log( "\nBlocking User: " + username );
const headers = {
'Accept': 'application/vnd.twitchtv.v5+json',
'Client-ID': Personal.client_id ,
'Authorization': 'OAuth ' + Personal.oauth_token
};
const options = {
url: 'https://api.twitch.tv/kraken/users/' + Personal.user_id + '/blocks/' + blocking_userid ,
method: 'PUT',
headers: headers
};
//console.log( options );
request( options , function ( err , response , body ) {
if ( err ) { console.log( err ); reject( err ); return; }
let result = JSON.parse( body );
console.log( result );
db.self.already_blocked[ blocking_userid ] = username;
db.save();
resolve( result );
return;
});
}
catch( error ) { console.log( error ); reject( error ); return; }
});
}
function ON_RAW_MESSAGE( messageCloned , message ) {
return new Promise( function( resolve , reject ) {
try {
//console.log( `\nType === ${ message.command }` );
if ( message.tags[ "display-name" ] ) {
//console.log( "From: " + message.tags[ "display-name" ] );
}
//console.log( message.params );
//console.log( message.raw );
if ( !!message.tags[ "display-name" ] ) {
if ( message.tags[ "display-name" ] !== Personal.username ) {
BlockUser( message.tags[ "display-name" ] );
}
}
resolve();
return;
}
catch( error ) { console.log( error ); reject( error ); return; }
}.bind( this ) );
}
module.exports.onRawMessage = ON_RAW_MESSAGE;
function GET_USER_ID( wUserName ) {
return new Promise( async function( resolve , reject ) {
try {
let headers = {
'Accept': 'application/vnd.twitchtv.v5+json',
'Client-ID': PersonalPivot.twitch.client_id ,
};
let options = {
url: 'https://api.twitch.tv/kraken/users?login=' + wUserName ,
headers: headers
};
//console.log( options );
request( options , function ( err , response , body ) {
if ( err ) { console.log( err ); reject( err ); return; }
let result = JSON.parse( body );
//console.log( result );
if ( !result ) { resolve( false ); return; }
if ( !result.users ) { resolve( false ); return; }
if ( !result.users[ 0 ] ) { resolve( false ); return; }
if ( !result.users[ 0 ]._id ) { resolve( false ); return; }
resolve( result.users[ 0 ]._id );
});
}
catch( error ) { console.log( error ); reject( error ); }
});
}
module.exports.getUserID = GET_USER_ID;
function info( data ) { console.log( data ); }
function warn( data ) { console.log( data ); }
function error( data ) { console.log( data ); }
// Go to someones channel ,
// Open Dev Console, Network Tab
// Enable 'Preserve Log' , Disable Cache
// Refresh , search for oauth token
( async ()=> {
let irc_client = new tmi.client({
options: {
debug: false
} ,
connection: {
reconnect: true ,
secure: true
} ,
identity: {
username: Personal.irc.username ,
password: Personal.irc.oauth ,
},
channels: Personal.observe_channels ,
logger: {
info: info ,
warn: warn ,
error: error
}
});
await irc_client.connect();
irc_client.on( "raw_message" , ON_RAW_MESSAGE );
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment