2022-02-11 05:48:47 +00:00
// Heyo / TMI_js by Sarimoko.com
2022-02-11 05:35:55 +00:00
// ================================
2022-02-11 05:48:47 +00:00
// Hello World Chatbot for Twitch IRC using TMI.js!
// --------------------------------
// View README.md for .env setup!!!
2022-02-11 05:35:55 +00:00
// --------------------------------
require ( 'dotenv' ) . config ( ) ;
2022-02-10 07:28:21 +00:00
const tmi = require ( 'tmi.js' ) ;
2022-02-11 10:14:24 +00:00
2022-02-11 05:35:55 +00:00
console . log ( process . env . API _HOST ) ;
2022-02-11 10:14:24 +00:00
2022-02-10 07:28:21 +00:00
const client = new tmi . Client ( {
2022-02-11 05:35:55 +00:00
options : { debug : true } ,
connection : {
secure : true ,
reconnect : true
} ,
identity : {
2022-02-11 10:14:24 +00:00
username : process . env . TTV _BOT _USERNAME ,
password : process . env . TTV _BOT _OAUTH
2022-02-11 05:35:55 +00:00
} ,
2022-02-11 10:14:24 +00:00
channels : [ process . env . TTV _CHANNELS ]
2022-02-10 07:28:21 +00:00
} ) ;
2022-02-11 10:14:24 +00:00
2022-02-10 07:28:21 +00:00
client . connect ( ) ;
2022-02-11 10:14:24 +00:00
2022-02-13 03:22:16 +00:00
// RNG User
const rp = require ( 'request-promise' ) ;
function getChatters ( channelName , _attemptCount = 0 ) {
return rp ( {
uri : ` https://tmi.twitch.tv/group/user/sarimoko/chatters ` ,
json : true
} )
. then ( data => {
return Object . entries ( data . chatters )
. reduce ( ( p , [ type , list ] ) => p . concat ( list . map ( name => {
if ( name === channelName ) type = 'broadcaster' ;
return { name , type } ;
} ) ) , [ ] ) ;
} )
. catch ( err => {
if ( _attemptCount < 3 ) {
return getChatters ( channelName , _attemptCount + 1 ) ;
}
throw err ;
} )
}
function getRandomChatter ( channelName , opts = { } ) {
let {
onlyViewers = false ,
noBroadcaster = false ,
skipList = [ ]
} = opts ;
return getChatters ( channelName )
. then ( data => {
let chatters = data
. filter ( ( { name , type } ) =>
! (
( onlyViewers && type !== 'viewers' ) ||
( noBroadcaster && type === 'broadcaster' ) ||
skipList . includes ( name )
)
) ;
return chatters . length === 0 ?
null :
chatters [ Math . floor ( Math . random ( ) * chatters . length ) ] ;
} ) ;
} // RNG END
// Chatbot Commands
2022-02-11 10:14:24 +00:00
client . on ( 'message' , ( channel , tags , message , self ) => { // START | MSG
if ( self ) return ; // Bot ignores itself
const badges = tags . badges || { } ; // Scan Badges
const isBroadcaster = badges . broadcaster ; // Define Streamer
const isMod = badges . moderator ; // Define Mod
const isModUp = isBroadcaster || isMod ; // Permission Merge = Mod+Streamer
const isSub = badges . subscriber || badges . founder ; // Define Subs
const botUserState = client . userstate [ channel ] ; // MOD Status Check
const amMod = botUserState !== undefined && botUserState . mod === true ; // Define Mod Status
2022-02-13 03:22:16 +00:00
// CMD | CHAT COMMANDS | Start
// CMD | RNGUser
if ( command === 'rnguser' ) {
// Get a random user but skip the user requesting a random user
getRandomChatter ( channel , { skipList : [ tags . username ] } )
. then ( user => {
if ( user === null ) {
client . say ( channel , ` Sorry ${ tags . username } , there was no one to choose from! ` ) ;
}
else {
let { name , type } = user ;
client . say ( channel , ` ${ tags . username } randomly chooses " ${ name } " as the best ${ type } ! ` ) ;
}
} )
. catch ( err => console . log ( '[ERR]' , err ) ) ;
2022-02-11 10:14:24 +00:00
}
2022-02-13 03:22:16 +00:00
// CMD | Color
if ( command === 'color' ) {
// Change your username color. Color must be in hex (#000000) or one of the following: Blue, BlueViolet, CadetBlue, Chocolate, Coral, DodgerBlue, Firebrick, GoldenRod, Green, HotPink, OrangeRed, Red, SeaGreen, SpringGreen, YellowGreen.
client . say ( channel , ` /color ${ args . join ( ' ' ) } ` ) ;
client . say ( channel , ` /me @ ${ tags . username } has changed my color to ${ args . join ( ' ' ) } ` ) ;
2022-02-11 10:14:24 +00:00
}
2022-02-13 03:22:16 +00:00
if ( isSub ) { // SUB | Start
if ( command === 'hi' ||
command === 'hello' ) {
client . say ( channel , ` !kappagen sarimoKO sarimoNERD Bleep-Bloop! ` ) ;
}
} // SUB | End
if ( isModUp ) { // MOD Start
// Shoutout
if ( command === 'so' ||
command === 'shoutout' ) {
client . say ( channel , ` /me Roll the clip! Smash @ ${ args . join ( ' ' ) } 's follow button at: https://twitch.tv/ ${ args . join ( ' ' ) } Bleep-Bloop! ` ) ;
}
2022-02-11 10:14:24 +00:00
}
} ) ; // END | MSG
// TTV Re-Active
// ==================================
2022-02-13 03:22:16 +00:00
// client.on("action", (channel, userstate, message, self) => {
// // Don't listen to my own messages..
// if (self) return;
// console.log('LOG: Action ');
// });
2022-02-11 05:35:55 +00:00
client . on ( "anongiftpaidupgrade" , ( channel , username , userstate ) => {
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: Anon2PaidSub...' ) ; // VERY specific... Anon Gift Sub => Paid Re-Sub
2022-02-11 05:35:55 +00:00
} ) ;
client . on ( "ban" , ( channel , username , reason , userstate ) => {
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: User Banned...' ) ; // BYE FELICIA!
2022-02-11 05:35:55 +00:00
} ) ;
2022-02-13 03:22:16 +00:00
// client.on("chat", (channel, userstate, message, self) => {
// // Don't listen to my own messages..
// if (self) return;
// // Do your stuff.
// console.log('LOG: Chat');
// });
2022-02-11 05:35:55 +00:00
client . on ( "cheer" , ( channel , userstate , message ) => {
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: Bits Cheered...' ) ; // Get Bits via Cheer then React
2022-02-11 05:35:55 +00:00
} ) ;
client . on ( "clearchat" , ( channel ) => {
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: Chat Cleared...' ) ; // Chat gets cleared by Mod/Streamer then this Reaction
2022-02-11 05:35:55 +00:00
} ) ;
2022-02-13 03:22:16 +00:00
client . on ( "connected" , ( address , port ) => {
console . log ( 'LOG: Connected ' ) ;
} ) ;
client . on ( "connecting" , ( address , port ) => {
console . log ( 'LOG: Connecting ' ) ;
} ) ;
client . on ( "disconnected" , ( reason ) => {
console . log ( 'LOG: Dis-Connected ' ) ;
} ) ;
client . on ( "emoteonly" , ( channel , enabled ) => {
console . log ( 'CHAT MODE: Emotes Only ' ) ;
} ) ;
// client.on("emotesets", (sets, obj) => {
// // Here are the emotes I can use:
// console.log('Emote Sets: IDK WHAT TO DO WITH THESE ');
// console.log(obj);
// });
client . on ( "followersonly" , ( channel , enabled , length ) => {
console . log ( 'CHAT MOD: Followers only enabled!' ) ;
} ) ;
2022-02-11 05:35:55 +00:00
client . on ( "giftpaidupgrade" , ( channel , username , sender , userstate ) => {
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: Anon2PaidSub...' ) ; // Huh? Tier 1 => Tier2?
2022-02-11 05:35:55 +00:00
} ) ;
2022-02-13 03:22:16 +00:00
client . on ( "hosted" , ( channel , username , viewers , autohost ) => {
console . log ( 'LOG: TYVM! For the host!' ) ;
} ) ;
2022-02-11 05:35:55 +00:00
client . on ( "hosting" , ( channel , target , viewers ) => {
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: Incoming Host...' ) ; // Get Hosted React
2022-02-11 05:35:55 +00:00
} ) ;
client . on ( "join" , ( channel , username , self ) => {
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: User joined IRC...' ) ; // User joins IRC **WARNING: aggro/annoying
2022-02-11 05:35:55 +00:00
} ) ;
2022-02-13 03:22:16 +00:00
client . on ( "logon" , ( ) => {
console . log ( 'LOG: Logon ' ) ;
} ) ;
// client.on("message", (channel, userstate, message, self) => {
// // Don't listen to my own messages..
// if (self) return;
//
// // Handle different message types..
// switch(userstate["message-type"]) {
// case "action":
// // This is an action message..
// break;
// case "chat":
// // This is a chat message..
// break;
// case "whisper":
// // This is a whisper..
// break;
// default:
// // Something else ?
// break;
// }
// });
client . on ( "messagedeleted" , ( channel , username , deletedMessage , userstate ) => {
console . log ( 'LOG: Msg deleted ' ) ;
} ) ;
client . on ( "mod" , ( channel , username ) => {
// Modded.
console . log ( 'LOG: Modded ' ) ;
} ) ;
client . on ( "mods" , ( channel , mods ) => {
// List
console . log ( 'LOG: Mods ' ) ;
} ) ;
client . on ( "notice" , ( channel , msgid , message ) => {
console . log ( 'LOG: Notice ' ) ;
} ) ;
2022-02-11 05:35:55 +00:00
client . on ( "part" , ( channel , username , self ) => {
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: User left IRC...' ) ; // User leaves IRC **WARNING: aggro/annoying
2022-02-11 05:35:55 +00:00
} ) ;
2022-02-13 03:22:16 +00:00
//client.on("ping", () => {});
//client.on("pong", (latency) => {});
// client.on("r9kbeta", (channel, enabled) => {
// console.log('r9kbeta? ');
// });
2022-02-11 05:35:55 +00:00
client . on ( "raided" , ( channel , username , viewers ) => {
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: Incoming Raid...' ) ; // Get Raided React
2022-02-11 05:35:55 +00:00
} ) ;
2022-02-13 03:22:16 +00:00
// client.on("raw_message", (messageCloned, message) => {
// console.log(message.raw);
// });
client . on ( "reconnect" , ( ) => {
// Do your stuff.
console . log ( 'Re-Connecting ' ) ;
} ) ;
client . on ( "resub" , function ( channel , username , months , message , userstate , methods ) {
console . log ( 'Re-Sub ' ) ;
} ) ;
// client.on("roomstate", (channel, state) => {
// console.log('LOG: Roomstate ');
// });
client . on ( "serverchange" , ( channel ) => {
console . log ( 'LOG: Server Change ' ) ;
} ) ;
client . on ( "slowmode" , ( channel , enabled , length ) => {
console . log ( 'CHAT MODE: Slow Mode enabled! ' ) ;
client . say ( channel , ` CHAT-MODE: Slow ` ) ;
} ) ;
2022-02-11 05:35:55 +00:00
client . on ( "subgift" , ( channel , username , streakMonths , recipient , methods , userstate ) => {
let senderCount = ~ ~ userstate [ "msg-param-sender-count" ] ;
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: Sub Gifted...' ) ; // Gift Sub React
2022-02-11 05:35:55 +00:00
} ) ;
client . on ( "submysterygift" , ( channel , username , numbOfSubs , methods , userstate ) => {
let senderCount = ~ ~ userstate [ "msg-param-sender-count" ] ;
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: Sub Gifted Anonomously...' ) ; // Anon Sub React
2022-02-11 05:35:55 +00:00
} ) ;
client . on ( "resub" , function ( channel , username , months , message , userstate , methods ) {
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: Sub Re-Subbed...' ) ; // Re-Sub React
2022-02-11 05:35:55 +00:00
} ) ;
2022-02-13 03:22:16 +00:00
client . on ( "subscribers" , ( channel , enabled ) => {
client . say ( channel , ` CHAT-MODE: Subscribers only! ` ) ;
} ) ;
2022-02-11 05:35:55 +00:00
client . on ( "subscription" , function ( channel , username , method , message , userstate ) {
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: 1st Sub...' ) ; // Either first or just General Sub
2022-02-11 05:35:55 +00:00
} ) ;
2022-02-13 03:22:16 +00:00
client . on ( "timeout" , ( channel , username , reason , duration , userstate ) => {
client . say ( channel , ` I know this only frustrates you more, but please take this time to cooldown. ` ) ;
} ) ;
client . on ( "unhost" , ( channel , viewers ) => {
client . say ( channel , ` /me @Sarimoko has turned if hosting, please standby for further instruction! ` ) ;
} ) ;
client . on ( "unmod" , ( channel , username ) => {
client . say ( channel , ` Oof, modship ` ) ;
} ) ;
client . on ( "vips" , ( channel , vips ) => {
client . say ( channel , ` @Sarimoko is fortunate to have many great viewers but these VIPs are a few specifically to thank! ` ) ;
} ) ;
2022-02-11 05:35:55 +00:00
client . on ( "whisper" , ( from , userstate , message , self ) => {
if ( self ) return ;
2022-02-11 10:14:24 +00:00
console . log ( 'TTV Alert: DM/Whisper recieved...' ) ; // When your bot gets a DM
} ) ;