From 6d5a21e0add8ec42f0bdd59dd9508af5a035a598 Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Sat, 12 Mar 2022 23:44:04 -0700 Subject: added link protection, actually need to test it though lol --- .gitignore | 2 +- install.sh | 1 + twitch-bot/bot.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- twitch-bot/mods.txt | 0 twitch-bot/settings.json | 5 ++++- 5 files changed, 50 insertions(+), 3 deletions(-) delete mode 100644 twitch-bot/mods.txt diff --git a/.gitignore b/.gitignore index b53b2c2..4d772cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ node_modules/ package-lock.json -source_output.txt +*.txt diff --git a/install.sh b/install.sh index db2dbeb..22804a1 100755 --- a/install.sh +++ b/install.sh @@ -48,6 +48,7 @@ cd .. echo echo "install: installing tmi node module for the twitch bot" cd twitch-bot +touch mods.txt trusted_users.txt npm install tmi.js cd .. diff --git a/twitch-bot/bot.js b/twitch-bot/bot.js index ec54b48..f4a2140 100644 --- a/twitch-bot/bot.js +++ b/twitch-bot/bot.js @@ -46,13 +46,19 @@ const opts = { channels: settings.channels }; +const linkProtection = settings.link_protection; +const bannedUrlEndings = settings.banned_endings; var modString = ""; var mods; try { modString = fs.readFileSync('mods.txt', 'utf8'); + trustedString = fs.readFileSync('trusted_users.txt', 'utf8'); modString = modString.replace(/(\r\n|\n|\r)/gm, ""); + trustedString = trustedString.replace(/(\r\n|\n|\r)/gm, ""); + console.log('* Loaded mods and trusted users from file.'); mods = modString.split(","); + trustedUsers = trustedString.split(","); } catch (err) { console.error(err) } @@ -64,12 +70,25 @@ function onMessageHandler (target, context, msg, self) { const user = context.username; msg = msg.toLowerCase(); var message = msg.trim(); + + // 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; + } + } if (message.charAt(0) === settings.command_char) { message = message.substr(1); valid = commands(target, message, user, mods); } else { - reactions(target, message, user); + if (linkProtection && findUrlEndings) + if (!linkProtect()) + reactions(target, message, user); + else + reactions(target, message, user); } } @@ -140,6 +159,30 @@ function reactions (target, message, user) { }); } +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; + } + } +} + // Twitch bot initialization const client = new tmi.client(opts); diff --git a/twitch-bot/mods.txt b/twitch-bot/mods.txt deleted file mode 100644 index e69de29..0000000 diff --git a/twitch-bot/settings.json b/twitch-bot/settings.json index a665436..e785f78 100644 --- a/twitch-bot/settings.json +++ b/twitch-bot/settings.json @@ -3,5 +3,8 @@ "bot_token": "enter the token that you generate here", "your_username": "YOUR_username_not_your_bots_username", "channels" : [ "enter the channel names for the bot to join", "you can have multiple!" ], - "command_char": "!" + "command_char": "!", + + "link_protection": false, + "banned_endings": [".com", ".org", ".edu", ".gov", ".gg", ".io", ".tv", ".uk", ".net", ".ca", ".de", ".jp", ".fr", ".au", ".us", ".ru", ".ch", ".it", ".nl", ".se", ".no", ".es", ".mil", ".xyz", ".top", ".info"] } -- cgit v1.2.3