2022-03-11 23:40:53 -07:00
/ *
* Created by Bryson Steck ( @ brysonsteck on GitHub )
* Feel free to make changes and send a PR !
* Open source under the MIT License :
*
* Copyright ( c ) 2022 Bryson Steck
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the "Software" ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software .
* THE SOFTWARE IS PROVIDED "AS IS" , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE .
*
* /
2022-03-11 15:00:05 -07:00
const tmi = require ( 'tmi.js' ) ;
2022-03-11 16:23:51 -07:00
const fs = require ( 'fs' ) ;
// read settings and reactions json files
2022-03-11 18:23:24 -07:00
try {
2022-03-11 20:48:52 -07:00
var settings = JSON . parse ( fs . readFileSync ( './settings.json' , 'utf8' ) ) ;
var reacts = JSON . parse ( fs . readFileSync ( './reacts.json' , 'utf8' ) ) ;
2022-03-11 23:13:00 -07:00
var chat _commands = JSON . parse ( fs . readFileSync ( './commands.json' , 'utf8' ) ) ;
2022-03-11 20:48:52 -07:00
var mod _commands = JSON . parse ( fs . readFileSync ( './mod_commands.json' , 'utf8' ) ) ;
2022-03-11 18:23:24 -07:00
} catch ( err ) {
console . error ( "An error occured trying to read the files for the Twitch bot: " + err ) ;
}
2022-03-11 15:00:05 -07:00
const opts = {
identity : {
2022-03-11 16:23:51 -07:00
username : settings . bot _username ,
password : settings . bot _token
2022-03-11 15:00:05 -07:00
} ,
2022-03-11 16:23:51 -07:00
channels : settings . channels
2022-03-11 15:00:05 -07:00
} ;
2022-03-12 23:44:04 -07:00
const linkProtection = settings . link _protection ;
const bannedUrlEndings = settings . banned _endings ;
2022-03-11 15:00:05 -07:00
var modString = "" ;
var mods ;
try {
2022-03-11 23:13:00 -07:00
modString = fs . readFileSync ( 'mods.txt' , 'utf8' ) ;
2022-03-12 23:44:04 -07:00
trustedString = fs . readFileSync ( 'trusted_users.txt' , 'utf8' ) ;
2022-03-11 15:00:05 -07:00
modString = modString . replace ( /(\r\n|\n|\r)/gm , "" ) ;
2022-03-12 23:44:04 -07:00
trustedString = trustedString . replace ( /(\r\n|\n|\r)/gm , "" ) ;
console . log ( '* Loaded mods and trusted users from file.' ) ;
2022-03-11 15:00:05 -07:00
mods = modString . split ( "," ) ;
2022-03-12 23:44:04 -07:00
trustedUsers = trustedString . split ( "," ) ;
2022-03-11 15:00:05 -07:00
} catch ( err ) {
console . error ( err )
}
var modCommand = false ;
function onMessageHandler ( target , context , msg , self ) {
if ( self ) { return ; } // Ignore messages from the bot
2022-03-11 16:23:51 -07:00
const user = context . username ;
2022-03-11 15:00:05 -07:00
msg = msg . toLowerCase ( ) ;
var message = msg . trim ( ) ;
2022-03-12 23:44:04 -07:00
// link protection stuff, only enables when true in settings.json
var findUrlEndings = false ;
if ( linkProtection ) {
for ( var i = urlEndings . length ; i -- ; ) {
if ( message . includes ( urlEndings [ i ] ) ) findUrlEndings = true ;
else if ( message . includes ( urlEndings [ i ] . replace ( '.' , '*' ) ) ) findUrlEndings = true ;
}
}
2022-03-11 15:00:05 -07:00
2022-03-11 16:23:51 -07:00
if ( message . charAt ( 0 ) === settings . command _char ) {
2022-03-11 15:00:05 -07:00
message = message . substr ( 1 ) ;
2022-03-11 16:23:51 -07:00
valid = commands ( target , message , user , mods ) ;
} else {
2022-03-12 23:44:04 -07:00
if ( linkProtection && findUrlEndings )
if ( ! linkProtect ( ) )
reactions ( target , message , user ) ;
else
reactions ( target , message , user ) ;
2022-03-11 15:00:05 -07:00
}
}
function commands ( target , commandName , user , mods ) {
modCommand = false ;
var valid = false ;
var isMod = false ;
if ( mods . indexOf ( user ) >= 0 ) isMod = true ;
2022-03-11 16:23:51 -07:00
if ( commandName === "help" ) {
2022-03-11 23:28:10 -07:00
var finalString = settings . command _char + "help " + settings . command _char + "about " ;
2022-03-11 23:13:00 -07:00
chat _commands . forEach ( command => {
finalString = finalString + settings . command _char + command . command + " " ;
2022-03-11 16:23:51 -07:00
} ) ;
client . say ( target , ` Here is the list of commands: ${ finalString } ` ) ;
2022-03-11 23:13:00 -07:00
valid = true ;
2022-03-11 23:28:10 -07:00
} else if ( commandName === "about" ) {
client . say ( target , ` This bot was stolen from https://github.com/brysonsteck/lset under the MIT License and configured by your streamer, ${ settings . your _username } ! ` ) ;
valid = true ;
2022-03-11 16:23:51 -07:00
} else {
2022-03-11 23:13:00 -07:00
chat _commands . forEach ( command => {
if ( commandName . search ( command . command ) !== - 1 ) {
2022-03-11 16:23:51 -07:00
client . say ( target , ` ${ command . reply } ` ) ;
valid = true ;
2022-03-11 17:47:59 -07:00
return true ;
2022-03-11 16:23:51 -07:00
}
} ) ;
2022-03-11 15:00:05 -07:00
}
if ( valid === false ) {
2022-03-11 16:23:51 -07:00
if ( isMod ) {
valid = modCommands ( target , commandName , isMod ) ;
}
2022-03-11 15:00:05 -07:00
}
2022-03-11 16:23:51 -07:00
2022-03-11 15:00:05 -07:00
if ( valid ) {
console . log ( ` * ${ user } executed ! ${ commandName } ` ) ;
} else {
if ( modCommand === false ) {
2022-03-11 16:23:51 -07:00
client . say ( target , ` @ ${ user } Unknown command " ${ settings . command _char } ${ commandName } ". Type ${ settings . command _char } help for all commands. ` ) ;
2022-03-11 15:00:05 -07:00
}
console . warn ( ` ! ${ user } tried to execute unknown/banned command ! ${ commandName } ` ) ;
}
}
function modCommands ( target , commandName , isMod ) {
var valid = false ;
2022-03-11 16:23:51 -07:00
mod _commands . forEach ( command => {
if ( message . search ( command . command ) !== - 1 ) {
if ( ! isMod ) {
client . say ( target , ` Only moderators can run this command... ` ) ;
2022-03-11 17:47:59 -07:00
return true ;
2022-03-11 16:23:51 -07:00
} else {
client . say ( target , ` ${ command . reply } ` ) ;
valid = true ;
2022-03-11 17:47:59 -07:00
return true ;
2022-03-11 16:23:51 -07:00
}
2022-03-11 15:00:05 -07:00
}
2022-03-11 16:23:51 -07:00
} ) ;
2022-03-11 15:00:05 -07:00
return valid ;
}
2022-03-11 16:23:51 -07:00
function reactions ( target , message , user ) {
reacts . forEach ( react => {
if ( message . search ( react . trigger ) !== - 1 ) {
client . say ( target , ` ${ react . reply } ` ) ;
}
} ) ;
2022-03-11 15:00:05 -07:00
}
2022-03-12 23:44:04 -07:00
function linkProtect ( ) {
if ( trustedUsers . indexOf ( user ) === - 1 ) {
if ( message . search ( "http" ) !== - 1 || message . search ( "www." ) !== - 1 || findUrlEndings ) {
urlAttempt = message ;
urlAttemptUser = user ;
console . log ( ` ! ${ user } tried to post a URL. Warned and timedout for 10 seconds. Message: " ${ urlAttempt } " ` ) ;
client . say ( target , ` /timeout ${ user } 10 Links from untrusted users are deleted as a protection against chat bots. ` ) ;
client . say ( target , ` @ ${ user } Your link was deleted because you haven't been in my chat before. If you have a legitimate link and aren't a bot, let me know! ` ) ;
// is a bot, don't go farther
return true ;
} else {
trustedString = trustedString + ` , ${ user } ` ;
trustedString = trustedString . replace ( /(\r\n|\n|\r)/gm , "" ) ;
trustedUsers . push ( user ) ;
fs . writeFile ( '/home/bryson/git/bryzinga-bot/trusted-users' , ` ${ trustedString } ` , function ( err ) {
if ( err ) return console . log ( err ) ;
console . log ( ` * ${ user } is now a trusted chatter. ` ) ;
} ) ;
// not a bot, move on to reactions
return false ;
}
}
}
2022-03-11 23:13:00 -07:00
// Twitch bot initialization
const client = new tmi . client ( opts ) ;
// Register our event handlers (defined below)
client . on ( 'message' , onMessageHandler ) ;
client . on ( 'connected' , onConnectedHandler ) ;
// Connect to Twitch:
client . connect ( ) ;
// Notify when connected
function onConnectedHandler ( addr , port ) {
console . log ( ` * Main Bot successfully connected to ${ addr } : ${ port } ` ) ;
}
2022-03-11 15:00:05 -07:00