Skip to content

Instantly share code, notes, and snippets.

@bretterer
Last active December 8, 2015 21:45
Show Gist options
  • Save bretterer/0edb175cf7cd92be0632 to your computer and use it in GitHub Desktop.
Save bretterer/0edb175cf7cd92be0632 to your computer and use it in GitHub Desktop.
Bot for Livecoding TV (based on https://github.com/Cristy94/livecoding-chat-bot)
var container = $('.message-pane');
var messageQueue = [];
var messageCount;
var greeted = [];
var textarea = $('#message-textarea');
var submit = $('input[type="submit"]');
var myUser = $('.main-navigation .user-name').text().trim().toUpperCase();
var gameStopped = true;
var botWritingCount = 0;
// Initiale the color pallete
$('#username-color').trigger('click');
$('#context-menu').trigger('mouseout');
var initialColor = $('#colorPremiumInput').val();
$('.message', container).addClass('read');
var responses = {};
try {
responses = JSON.parse(localStorage.responses);
}
catch (e) {
// Ignore
}
function postMessage(message) {
botWritingCount++;
// Make name white
$('.user-color-item').eq(0).attr('data-color', '#FFFFFF').trigger('click');
setTimeout(function(){
textarea.val("BOT: " + message);
submit.trigger('click');
// Restore name color
botWritingCount--;
if(botWritingCount == 0) {
setTimeout(function() {
// Only change back the color if no message is being written
if(botWritingCount == 0)
$('.user-color-item').eq(0).attr('data-color', initialColor).trigger('click');
}, 300);
}
}, 500);
}
function joinRoom(userName) {
if(userHasBeenGreeted(userName)) {
return;
}
postMessage("Hello " + userName + " welcome to my stream! Please use '!help` to get a list of commands available.");
greeted.push(userName);
trimGreeted();
}
function userHasBeenGreeted(userName) {
if(greeted.indexOf(userName) == -1) {
return false;
}
return true;
}
function trimGreeted() {
greeted = greeted.slice(greeted.length-10, 10);
}
function processMessage() {
if(messageQueue.length == 0)
return;
// Don't do anything if the streamer is writing
if(textarea.val() != '')
return;
var message = messageQueue.shift();
// If it's an info message
if(message.hasClass('message-info')) {
var text = message.text();
// Someone entered the room
if(text.indexOf(' joined the room.') != -1) {
var userJoined = text.slice(0, text.indexOf(' joined the room.'));
joinRoom(userJoined);
}
} else {
// var userName = $('a', message).text(); <- That includes all links in a message.
var userName = $('.nickname', message).text(); // There's a class for names!
var regexp = new RegExp("^"+userName, "g");
var userName = userName.substring(5);
// Check command
var command = message.text().replace(regexp, '');
var commandSetKey = undefined;
var parameter = '';
// See if the command has a parameter
if(command.indexOf(' ') != -1) {
commands = command.split(' ');
command = commands.shift();
parameter = commands.join(' ');
if (command.startsWith('!set-')) {
var setCommands = command.split('-');
command = setCommands.shift();
commandSetKey = setCommands[0];
}
}
switch(command) {
case '!help':
var availableCommands = getBaseCommands().concat(getCustomCommands());
if(isAdmin(userName)) {
availableCommands = availableCommands.concat(getAdminCommands());
}
postMessage('Available commands are : ' + availableCommands.join(', '));
break;
case '!stormpath':
postMessage('@' + userName + ', more information on Stormpath can be found at https://stormpath.com/product');
break;
case '!time':
postMessage('@' + userName + ', the time is: ' + (new Date()).toLocaleTimeString());
break;
// Admin only commands
case '!reset':
console.log(userName);
if(!isAdmin(userName)) break;
responses = {};
localStorage.responses = undefined;
postMessage('Bot has been reset! :-s');
break;
case '!set':
if(!isAdmin(userName)) break;
if (getBaseCommands().concat(getAdminCommands()).indexOf('!'+commandSetKey) > -1) {
postMessage('Command ' + commandSetKey + ' is reserved !');
break;
}
if(commandSetKey != undefined && parameter != '') {
responses[commandSetKey] = parameter;
postMessage('Command ' + commandSetKey + ' set to ' + responses[commandSetKey]);
localStorage.responses = JSON.stringify(responses);
}
break;
default:
var key = command.replace('!', '');
if (responses[key] != undefined) {
postMessage('@' + userName + ', ' + key + ' is : ' + responses[key]);
}
break;
}
}
}
function isAdmin(userName) {
return userName.toUpperCase() == myUser;
}
function processMessages() {
// Get new messages since last check
var newMessages = $('.message:not(.read)', container);
newMessages.each(function() {
// Mark message as read
$(this).addClass('read');
// Add new messages to the queue
messageQueue.push($(this));
});
}
function getBaseCommands() {
return ['!help', '!time', '!stormpath'];
}
function getAdminCommands() {
return ['!set', '!reset'];
}
function getCustomCommands() {
commands = [];
for (customKey in responses) {
commands.push('!' + customKey);
}
return commands;
}
function tick() {
processMessages();
processMessage();
setTimeout(tick, 300);
}
tick();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment