Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
|
bea8da4120 | |
|
142ff5d740 | |
|
565fd2793a | |
|
d8c06ca14b | |
|
ec017737dc |
54
README.md
54
README.md
|
@ -1,3 +1,55 @@
|
||||||
# TMI_js
|
# TMI_js
|
||||||
|
|
||||||
TMI js is a Node & JavaScript API proved by Twitch.
|
Heyo / TMI_js is a `Hello World Chatbot`
|
||||||
|
|
||||||
|
## Why?
|
||||||
|
A starting point for all the default hello world stuff. TMI_js Chatbot is built in Node.js using Twitch's Official TMI.js API!
|
||||||
|
|
||||||
|
## What?
|
||||||
|
* dot ENV to keep API Keys out of code ever uploaded/downloaded.
|
||||||
|
* TMI.js obv connects
|
||||||
|
* Mod & Broadcaster Role Init
|
||||||
|
* !COMMAND => command parse
|
||||||
|
* Hello World Commands for Mods & Everyone
|
||||||
|
|
||||||
|
## TODO
|
||||||
|
* Add Subs-Only Commands
|
||||||
|
* MySQL Branch | Logs & Input
|
||||||
|
|
||||||
|
## How to install
|
||||||
|
### Git
|
||||||
|
Basic
|
||||||
|
```bash
|
||||||
|
sudo git clone git@git.sarimoko.com:Heyo/TMI_js.git --stable
|
||||||
|
```
|
||||||
|
With MySQL
|
||||||
|
```bash
|
||||||
|
sudo git clone git@git.sarimoko.com:Heyo/TMI_js.git --mysql
|
||||||
|
```
|
||||||
|
|
||||||
|
### Initialize/Prerequisites TMI_js
|
||||||
|
```bash
|
||||||
|
sudo npm install tmi.js@1.5.0
|
||||||
|
sudo npm install dotenv
|
||||||
|
sudo npm install request-promise
|
||||||
|
sudo node TMI_js.js
|
||||||
|
```
|
||||||
|
### Auto-Start on Boot
|
||||||
|
```bash
|
||||||
|
# Installing pm2
|
||||||
|
npm install -g pm2 # may require sudo
|
||||||
|
|
||||||
|
# Starting the app
|
||||||
|
pm2 start ~/Projects/red.js
|
||||||
|
pm2 save # saves the running processes
|
||||||
|
# if not saved, pm2 will forget
|
||||||
|
# the running apps on next boot
|
||||||
|
|
||||||
|
|
||||||
|
# check status
|
||||||
|
pm2 list
|
||||||
|
|
||||||
|
# IMPORTANT: If you want pm2 to start on system boot
|
||||||
|
pm2 startup
|
||||||
|
# starts pm2 on computer boot
|
||||||
|
```
|
338
node/chatbot.js
338
node/chatbot.js
|
@ -6,23 +6,9 @@
|
||||||
// --------------------------------
|
// --------------------------------
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const tmi = require('tmi.js');
|
const tmi = require('tmi.js');
|
||||||
// MySQL Setup | Optional - View README.md for MySQL setup!
|
|
||||||
// --------------------------------
|
|
||||||
var mysql = require('mysql');
|
|
||||||
var con = mysql.createConnection({
|
|
||||||
host: process.env.SQL_IP, // Add in DOTenv
|
|
||||||
user: process.env.SQL_USER, // Add in DOTenv
|
|
||||||
password: process.env.SQL_PASS // Add in DOTenv
|
|
||||||
});
|
|
||||||
// MySQL Error Check
|
|
||||||
// --------------------------------
|
|
||||||
con.connect(function(err) {
|
|
||||||
if (err) throw err;
|
|
||||||
console.log("MySQL | Connected!");
|
|
||||||
});
|
|
||||||
// TMI Config
|
|
||||||
// --------------------------------
|
|
||||||
console.log(process.env.API_HOST);
|
console.log(process.env.API_HOST);
|
||||||
|
|
||||||
const client = new tmi.Client({
|
const client = new tmi.Client({
|
||||||
options: { debug: true },
|
options: { debug: true },
|
||||||
connection: {
|
connection: {
|
||||||
|
@ -30,131 +16,265 @@ const client = new tmi.Client({
|
||||||
reconnect: true
|
reconnect: true
|
||||||
},
|
},
|
||||||
identity: {
|
identity: {
|
||||||
username: process.env.TTV_BOT_USERNAME, // Add in DOTenv
|
username: process.env.TTV_BOT_USERNAME,
|
||||||
password: process.env.TTV_BOT_OAUTH // Add in DOTenv
|
password: process.env.TTV_BOT_OAUTH
|
||||||
},
|
},
|
||||||
channels: [ process.env.TTV_CHANNELS ] // Add in DOTenv
|
channels: [ process.env.TTV_CHANNELS ]
|
||||||
});
|
});
|
||||||
// TTV IRC Connect
|
|
||||||
// --------------------------------
|
|
||||||
client.connect();
|
client.connect();
|
||||||
// Mod Setup
|
|
||||||
// ================================
|
// RNG User
|
||||||
// isMod = Broadcaster/Streamer & Mods
|
const rp = require('request-promise');
|
||||||
// --------------------------------
|
|
||||||
client.on('message', (channel, tags, message, self) => {
|
function getChatters(channelName, _attemptCount = 0) {
|
||||||
if(self) return;
|
return rp({
|
||||||
const badges = tags.badges || {};
|
uri: `https://tmi.twitch.tv/group/user/sarimoko/chatters`,
|
||||||
const isBroadcaster = badges.broadcaster;
|
json: true
|
||||||
const isMod = badges.moderator;
|
})
|
||||||
const isModUp = isBroadcaster || isMod;
|
.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)];
|
||||||
});
|
});
|
||||||
// Channel Reactions
|
} // RNG END
|
||||||
// ================================
|
|
||||||
// When an Anonymous Gift-Sub is Upgraded to Paid by the gifted user.
|
// Chatbot Commands
|
||||||
// --------------------------------
|
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
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
}
|
||||||
|
// 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(' ')}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
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!`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}); // END | MSG
|
||||||
|
|
||||||
|
// TTV Re-Active
|
||||||
|
// ==================================
|
||||||
|
// client.on("action", (channel, userstate, message, self) => {
|
||||||
|
// // Don't listen to my own messages..
|
||||||
|
// if (self) return;
|
||||||
|
// console.log('LOG: Action ');
|
||||||
|
// });
|
||||||
client.on("anongiftpaidupgrade", (channel, username, userstate) => {
|
client.on("anongiftpaidupgrade", (channel, username, userstate) => {
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: Anon2PaidSub...'); // VERY specific... Anon Gift Sub => Paid Re-Sub
|
||||||
});
|
});
|
||||||
// When a User is BANNED
|
|
||||||
// --------------------------------
|
|
||||||
client.on("ban", (channel, username, reason, userstate) => {
|
client.on("ban", (channel, username, reason, userstate) => {
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: User Banned...'); // BYE FELICIA!
|
||||||
});
|
});
|
||||||
// When Bits are Cheered
|
// client.on("chat", (channel, userstate, message, self) => {
|
||||||
// --------------------------------
|
// // Don't listen to my own messages..
|
||||||
|
// if (self) return;
|
||||||
|
// // Do your stuff.
|
||||||
|
// console.log('LOG: Chat');
|
||||||
|
// });
|
||||||
client.on("cheer", (channel, userstate, message) => {
|
client.on("cheer", (channel, userstate, message) => {
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: Bits Cheered...'); // Get Bits via Cheer then React
|
||||||
});
|
});
|
||||||
// After Chat is Cleared by a MOD
|
|
||||||
// --------------------------------
|
|
||||||
client.on("clearchat", (channel) => {
|
client.on("clearchat", (channel) => {
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: Chat Cleared...'); // Chat gets cleared by Mod/Streamer then this Reaction
|
||||||
|
});
|
||||||
|
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!');
|
||||||
});
|
});
|
||||||
// When a Gift-Sub is Upgraded to Paid by the gifted user.
|
|
||||||
// --------------------------------
|
|
||||||
client.on("giftpaidupgrade", (channel, username, sender, userstate) => {
|
client.on("giftpaidupgrade", (channel, username, sender, userstate) => {
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: Anon2PaidSub...'); // Huh? Tier 1 => Tier2?
|
||||||
|
});
|
||||||
|
client.on("hosted", (channel, username, viewers, autohost) => {
|
||||||
|
console.log('LOG: TYVM! For the host!');
|
||||||
});
|
});
|
||||||
// When @someone Hosts your channel
|
|
||||||
// --------------------------------
|
|
||||||
client.on("hosting", (channel, target, viewers) => {
|
client.on("hosting", (channel, target, viewers) => {
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: Incoming Host...'); // Get Hosted React
|
||||||
});
|
});
|
||||||
// When @someone connects to your Twitch IRC
|
|
||||||
// --------------------------------
|
|
||||||
// DOES NOT COUNT AS VIEWER!
|
|
||||||
// *** WARNING: Annoying AF don't really use it! I've commented it out.
|
|
||||||
client.on("join", (channel, username, self) => {
|
client.on("join", (channel, username, self) => {
|
||||||
// client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: User joined IRC...');// User joins IRC **WARNING: aggro/annoying
|
||||||
|
});
|
||||||
|
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 ');
|
||||||
});
|
});
|
||||||
// When @someone dis-connects from your Twitch IRC
|
|
||||||
// --------------------------------
|
|
||||||
// BYE FELICIA!
|
|
||||||
// *** WARNING: Annoying AF don't really use it! I've commented it out.
|
|
||||||
client.on("part", (channel, username, self) => {
|
client.on("part", (channel, username, self) => {
|
||||||
// client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: User left IRC...');// User leaves IRC **WARNING: aggro/annoying
|
||||||
});
|
});
|
||||||
// When @someone Raids your Channel
|
//client.on("ping", () => {});
|
||||||
// --------------------------------
|
//client.on("pong", (latency) => {});
|
||||||
|
// client.on("r9kbeta", (channel, enabled) => {
|
||||||
|
// console.log('r9kbeta? ');
|
||||||
|
// });
|
||||||
client.on("raided", (channel, username, viewers) => {
|
client.on("raided", (channel, username, viewers) => {
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: Incoming Raid...');// Get Raided React
|
||||||
|
});
|
||||||
|
// 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`);
|
||||||
});
|
});
|
||||||
// When @someone Gifts a Sub
|
|
||||||
// --------------------------------
|
|
||||||
client.on("subgift", (channel, username, streakMonths, recipient, methods, userstate) => {
|
client.on("subgift", (channel, username, streakMonths, recipient, methods, userstate) => {
|
||||||
let senderCount = ~~userstate["msg-param-sender-count"];
|
let senderCount = ~~userstate["msg-param-sender-count"];
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: Sub Gifted...');// Gift Sub React
|
||||||
});
|
});
|
||||||
// When @someone Randomly Gifts a Sub
|
|
||||||
// --------------------------------
|
|
||||||
client.on("submysterygift", (channel, username, numbOfSubs, methods, userstate) => {
|
client.on("submysterygift", (channel, username, numbOfSubs, methods, userstate) => {
|
||||||
let senderCount = ~~userstate["msg-param-sender-count"];
|
let senderCount = ~~userstate["msg-param-sender-count"];
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: Sub Gifted Anonomously...');// Anon Sub React
|
||||||
});
|
});
|
||||||
// When @someone Re-Subs
|
|
||||||
// --------------------------------
|
|
||||||
client.on("resub", function (channel, username, months, message, userstate, methods) {
|
client.on("resub", function (channel, username, months, message, userstate, methods) {
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: Sub Re-Subbed...');// Re-Sub React
|
||||||
|
});
|
||||||
|
client.on("subscribers", (channel, enabled) => {
|
||||||
|
client.say(channel, `CHAT-MODE: Subscribers only!`);
|
||||||
});
|
});
|
||||||
// When @someone Subs for the first time!
|
|
||||||
// --------------------------------
|
|
||||||
client.on("subscription", function (channel, username, method, message, userstate) {
|
client.on("subscription", function (channel, username, method, message, userstate) {
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: 1st Sub...'); // Either first or just General Sub
|
||||||
|
});
|
||||||
|
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!`);
|
||||||
});
|
});
|
||||||
// When a DM/Whisper is recieved on the bot account
|
|
||||||
// --------------------------------
|
|
||||||
client.on("whisper", (from, userstate, message, self) => {
|
client.on("whisper", (from, userstate, message, self) => {
|
||||||
// Don't listen to my own messages..
|
|
||||||
if (self) return;
|
if (self) return;
|
||||||
client.say(channel, `/me Hello World!`);
|
console.log('TTV Alert: DM/Whisper recieved...'); // When your bot gets a DM
|
||||||
});
|
});
|
||||||
// ! COMMAND => command
|
|
||||||
// ================================
|
|
||||||
client.on('message', (channel, tags, message, self) => {
|
|
||||||
if(self || !message.startsWith('!')) return;
|
|
||||||
const args = message.slice(1).split(' ');
|
|
||||||
const command = args.shift().toLowerCase();
|
|
||||||
const botUserState = client.userstate[channel];
|
|
||||||
const amMod = botUserState !== undefined && botUserState.mod === true;
|
|
||||||
// ! MOD Commands
|
|
||||||
// ================================
|
|
||||||
if(isModUp) {
|
|
||||||
else if(command === 'abc' || command === 'alpha') {
|
|
||||||
client.say(channel, `Mod Command: A`);
|
|
||||||
}
|
|
||||||
if(command === 'xyz' || command === 'beta') {
|
|
||||||
client.say(channel, `Mod Command: B`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// !heyo | !helloworld
|
|
||||||
// ----------------------------------
|
|
||||||
else if(command === 'heyo' || command === 'helloworld') {
|
|
||||||
client.say(channel, `${tags.username} says hi to @${args.join(' ')}!`);
|
|
||||||
}
|
|
||||||
// ERROR RESPONSE
|
|
||||||
// --------------------------------
|
|
||||||
else {
|
|
||||||
client.say(channel, `/me Hey @${tags.username}, Command not found!`);
|
|
||||||
}
|
|
||||||
}); // ! COMMAND END
|
|
Loading…
Reference in New Issue