Skip to content

Instantly share code, notes, and snippets.

@littletsu
Last active October 13, 2018 01:13
Show Gist options
  • Save littletsu/9e88e886a28647ed2422b9ff6acfa4a8 to your computer and use it in GitHub Desktop.
Save littletsu/9e88e886a28647ed2422b9ff6acfa4a8 to your computer and use it in GitHub Desktop.
// Require Packages -- You will need to install these
const Discord = require('discord.js');
const client = new Discord.Client(); // This uses the discord.js package to setup a client
// Constant Variables
const prefix = '_'; // This can be changed to anything you want
const ownerID = 'yourID'; // This will be used in future videos
// Listener Events
client.on('message', message => {
// This is run whenever a new message is created in a channel the bot has access to view
// Variables
let args = message.content.slice(prefix.length).trim().split(' ');
let cmd = args.shift().toLowerCase();
// Now, we have two variables. cmd contains the command following the prefix
// args contains everything following that, split into an array by spaces
// Return Statemets
if (message.author.bot) return; // This will ignore all bots
if (!message.content.startsWith(prefix)) return; // This will return if the message doesn't start with your prefix
// Command Handler
try {
// Bonus: Auto-Reload (You should move this into it's own command)
delete require.cache[require.resolve(`./commands/${cmd}.js`)];
let commandFile = require(`./commands/${cmd}.js`); // This will require a file in the commands folder
commandFile.run(client, message, args); // This will pass three variables into the file
// We can create a basic ping-pong command for starters
} catch (e) { // This will catch any errors, either within the code or if the command doesn't exist
console.log(e.stack);
}
});
// Ready Event -- This will run whenever the turns on
client.on('ready', () => console.log('Launched!'));
// Discord Login -- Remember to use your own token, and to NEVER leak it
client.login(process.env.TOKEN); // This will be your token instead of process.env.TOKEN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment