Skip to content

Instantly share code, notes, and snippets.

@hcortezr
Created December 7, 2017 07:33
Show Gist options
  • Save hcortezr/c185430cca14c30a862f2c0eda523be5 to your computer and use it in GitHub Desktop.
Save hcortezr/c185430cca14c30a862f2c0eda523be5 to your computer and use it in GitHub Desktop.
DiscordCLI - the beginning to discord cli edition... jk.. idk tho.. who knows tbh..
Make sure you call DiscordCLI after you set your listeners, but before you login!
const readline = require("readline");
class DiscordCLI {
constructor(client, options){
this.channel = null;
this.client = client;
this.options = {
exit_message: "EXIT_CHANNEL",
prompt_format: "#:channel [You] > ",
incoming_format: "#:channel [:sender] > :message"
};
this.options = Object.assign({}, this.options, options);
this.init();
}
init(){
this.client.on("message", message => {
if(message.channel.id !== this.channel.id) return;
if(message.author.id === this.client.user.id) return;
console.log(this.options.incoming_format.replace(":channel", message.channel.name).replace(":sender", message.author.username).replace(":message", message.content));
});
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
this.client.on("ready", () => {this.askFor("channel")});
}
askFor(what){
switch(what.toLowerCase()){
case "channel":
this.rl.question("Channel Id: ", id => {
id = id.trim();
this.channel = this.client.channels.get(id);
if(this.channel != null){
console.log("Channel found and set.");
console.log("You are now sending messages to #"+this.channel.name+" in "+this.channel.guild.name);
console.log("To leave send '"+this.options.exit_message+"'.");
this.askFor("message");
}else{
console.log("Channel not found!");
this.askFor("channel");
}
});
break;
case "message":
let message_prompt = this.options.prompt_format.replace(":guild", this.channel.guild.name).replace(":channel", this.channel.name);
this.rl.question(message_prompt, input => {
if(input === this.options.exit_message){
this.channel = null;
this.askFor("channel");
return;
}
this.channel.send(input);
this.askFor("message");
});
break;
}
}
}
module.exports = DiscordCLI;
const discord = require("discord.js"),
client = new discord.Client();
const DiscordCLI = require("./DiscordCLI");
client.on("ready", () => {
console.log("Bot is ready!");
});
new DiscordCLI(client, {
exit_message: "leave"
});
client.login("<your token>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment