Skip to content

Instantly share code, notes, and snippets.

@notpeelz
Last active February 22, 2016 00:18
Show Gist options
  • Save notpeelz/47cc7392747db1cbc80b to your computer and use it in GitHub Desktop.
Save notpeelz/47cc7392747db1cbc80b to your computer and use it in GitHub Desktop.
E-Walrus bot
import path from 'path';
import fs from 'fs';
import Discord from 'discord.js';
import request from 'superagent';
import plugins from '~/plugins';
import * as utils from '~/utils.js';
const bot = new Discord.Client();

function* getCommand(content) {
const pattern = /(\S+)/g;
let match;
do {
match = pattern.exec(content);
if (match) {
yield match[1];
}
} while (match);
}
const registeredCommands = {};
const PluginAPI = {
registerCommand(command, fn) {
registeredCommands[command] = registeredCommands[command] || [];
registeredCommands[command].push(fn);
}
};
Array.forEach(plugins, plugin => {
plugin(PluginAPI);
});
bot.on('message', message => {
if (message.isMentioned(bot.user)) {
bot.reply(message, 'Hello there my fellow walrus brethren.');
}
if (message.content.indexOf('!') === 0) {
const args = [...getCommand(message.content.substr(1))];
const cmd = args.shift().toLowerCase().trim();
const command = registeredCommands[cmd];
if (!command || !command.length) {
bot.reply(message, `Unknown command: \`${utils.sanitizeCode(cmd)}\``);
return;
}
Array.forEach(command, cmd => setTimeout(() => cmd(bot, message, args)));
}
});
bot.login('EMAIL', 'PASSWORD');
import bulk from 'bulk-require';
const modules = bulk(__dirname, ['./**/!(*index|*.spec).js']);
const plugins = [];
Object.keys(modules).forEach((key) => {
let item = modules[key].default;
plugins.push(item);
});
export default plugins;
import loadJSON from 'load-json-file';
import * as utils from '~/utils.js';
const defaultOptions = {
volume: 0.1
};
let playingSound = false;
function playSound(bot, message, { path: filename, options }) {
if (message.author.voiceChannel) {
playingSound = true;
bot.joinVoiceChannel(message.author.voiceChannel)
.then(connection => {
connection.playFile(filename, {...defaultOptions, ...options}).then(intent => {
intent.on('end', () => {
bot.leaveVoiceChannel();
playingSound = false;
});
});
})
.catch(err => {
if (err) {
bot.reply(message, "Sorry, couldn't join.");
}
});
return true;
}
return false;
}
let sounds;
function loadSounds() {
loadJSON('./sounds.json').then(json => {
sounds = json;
});
}
loadSounds();
const commands = {
reload(bot, message, args) {
loadSounds();
},
play(bot, message, args) {
if (!sounds) {
bot.reply(message, 'No sounds loaded.');
return;
}
if (!args[0]) {
bot.reply(message, 'No sound specified');
return;
}
const sound = sounds[args[0]];
if (!sound) {
bot.reply(message, `Couldn't play \`${utils.sanitizeCode(args[0])}\`; sound not defined.`);
return;
}
if (playingSound) {
bot.reply(message, "I'm busy, try again later!");
}
else if (!bot.internal.voiceConnection || !bot.internal.voiceConnection.playing) {
if (!playSound(bot, message, sound)) {
bot.reply(message, "Can't join channel.");
}
}
}
};
export default function load({registerCommand: register}) {
register('reload', commands.reload);
register('play', commands.play);
};
{
"darude": {
"path": "./sounds/darude.wav",
"options": { "volume": 0.1 }
},
"lagstorm": {
"path": "./sounds/darude.wav",
"options": { "volume": 0.1 }
},
"fired": {
"path": "./sounds/fired.wav",
"options": { "volume": 0.5 }
},
"comeagain": {
"path": "./sounds/comeagain.mp3",
"options": { "volume": 1.0 }
},
"anotherone": {
"path": "./sounds/anotherone.mp3",
"options": { "volume": 0.2 }
},
"dreamclutch": {
"path": "./sounds/dreamclutch.mp3",
"options": { "volume": 0.5 }
}
}
export function sanitizeCode(input) {
if (typeof input !== 'string')
return;
return input.replace('`', '\'');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment