Skip to content

Instantly share code, notes, and snippets.

@TheGuywithTheHat
Created August 24, 2018 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheGuywithTheHat/b0f11783dbaf224edb6575bc3b93f7d8 to your computer and use it in GitHub Desktop.
Save TheGuywithTheHat/b0f11783dbaf224edb6575bc3b93f7d8 to your computer and use it in GitHub Desktop.
script for manually controlling a discord.js bot
const config = require('./config.json');
const Discord = require('discord.js');
const client = new Discord.Client();
const repl = require('repl');
const handlers = {
say: sayHandler,
channel: channelHandler,
}
var currentChannel = undefined;
client.on('ready', () => {
console.log('client ready');
repl.start({
eval: handle,
ignoreUndefined: true,
});
});
client.login(config.user_token);
function handle(input, context, filename, cb) {
var split = input.search(/\s+/);
command = input.substring(0, split);
args = input.substring(split).trim()
//console.log('handling: [' + command + ', ' + args + ']');
if(command in handlers) {
cb(handlers[command](args));
} else {
//console.log('invalid');
cb("invalid command '" + command + "'");
}
}
function channelHandler(id) {
//console.log('switching channels');
//console.log(client.channels);
//console.log(id);
currentChannel = client.channels.get(id);
//console.log(currentChannel);
return currentChannel//.name || currentChannel.recipient || currentChannel.id;
}
function sayHandler(text) {
if(!currentChannel) {
//console.log('no channel selected');
return 'select a channel first with `channel <id>`';
} else {
//console.log('sending message');
return currentChannel.send(text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment