Skip to content

Instantly share code, notes, and snippets.

@AlcaDesign
Created May 19, 2016 23:23
Show Gist options
  • Save AlcaDesign/68a4463466283ea87d25b82a012921b0 to your computer and use it in GitHub Desktop.
Save AlcaDesign/68a4463466283ea87d25b82a012921b0 to your computer and use it in GitHub Desktop.
"Using two distinct bots on my channel" https://github.com/Schmoopiie/tmi.js/issues/135
'use strict';
const _ = require('lodash'), // Just to handle a couple of things more easily
tmi = require('tmi.js'),
channel = ''; // Your channel
let mainConfig = {
options: {
debug: true // Debug to console
},
connection: {
reconnect: true, // Make sure to reconnect on connection error
secure: true // Secure servers are cooler
},
channels: [channel] // Yooouurrr channel
},
identities = {
A: {
username: '', // botA's username and password
password: ''
},
B: {
username: '', // botB's username and password
password: ''
},
},
botAConfig = _.defaults({ identity: identities.A }, mainConfig),
botBConfig = _.defaults({ identity: identities.B }, mainConfig),
botA = new tmi.client(botAConfig),
botB = new tmi.client(botBConfig);
function reply(channel) {
return message => this.say(channel, message);
}
function handleCommand(chan, user, message) {
let parameters = message.split(' ').filter(n => n),
command = parameters.shift().toLowerCase().replace(/^!+/, ''),
replyA = reply.call(botA, chan), // Reply as botA
replyB = reply.call(botB, chan); // Reply as botB
if(command == 'time') { // botA will respond to this
let d = new Date();
replyA(user.displayName + ', the time is ' + [
(d.getHours()%12 || 12),
d.getMinutes(),
d.getSeconds()
]
.map(n => n.toString().length == 1 ? '0' + n : n)
.join(':'));
}
else if(command == 'date') { // botB will respond to this
let d = new Date();
replyB(user.displayName + ', the date is ' + [
d.getMonth() + 1,
d.getDate(),
d.getFullYear()
]
.join('/'));
}
}
function handleMessage(channel, _user, message, fromSelf) {
if(fromSelf) { // Don't respond to self
return false;
}
let chan = channel.replace('#', ''), // #channel -> channel
user = _.mapKeys(_user, _.rearg(_.camelCase, 1)); // user.['user-type'] -> user.userType
if(message.startsWith('!')) { // Possible commands
handleCommand(chan, user, message); // Send to command handler
}
}
botA.on('message', handleMessage); // Only 1 bot needs to listen to the chat
[botA, botB].forEach(n => n.connect()); // Connect multiple bots
{
"name": "multiple-bots",
"version": "0.1.0",
"description": "Multiple bots in one channel",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"twitch",
"chat",
"bot",
"multiple"
],
"author": "Jacob \"Alca\" Foster",
"license": "ISC",
"dependencies": {
"lodash": "^4.12.0",
"tmi.js": "0.0.29"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment