Created
August 24, 2018 23:11
-
-
Save TheGuywithTheHat/b0f11783dbaf224edb6575bc3b93f7d8 to your computer and use it in GitHub Desktop.
script for manually controlling a discord.js bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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