Skip to content

Instantly share code, notes, and snippets.

@cherryblossom000
Last active April 2, 2021 16:48
Show Gist options
  • Save cherryblossom000/7182a66c577bb5858f4ef88ae713434f to your computer and use it in GitHub Desktop.
Save cherryblossom000/7182a66c577bb5858f4ef88ae713434f to your computer and use it in GitHub Desktop.
v12 Update: Simple, complete example of a bot in Discord.js

Updated for Discord.js v12

This is identical to the original except that I've updated it for Discord.js v12. For more information on updating to v12 you can read this guide.

Comments below are also not moderated and you will not receive support here.

Readme from original:

The Perfect Lil' Bot

This bot example is the combined work of members of the Discord.js and Idiot Guide's community. It attempts to provide a "complete" starter example of a simple, one-file bot, with comments and information to properly understand each part and how it works.

Want a better tutorial? Check out AnIdiots.Guide

NOTE: This code only works for discord.js version 11. Most of this doesn't work in v12 because they broke everything. I've no intention to update this code, so make sure you install version 11 as per instructions.

Setup

Before doing any of this make sure you have a bot account, check this page to know how to create the bot app and your token, as well as how to invite the bot in your guild.

  • Create a new folder somewhere, give it a bot name, aka MyBot or SuperBot
  • Create a file called index.js, and paste in the contents of index.js below.
  • Create a file called config.json and give it the following content, using your real bot token:
{ 
  "token"  : "MTg-this-IzNzU3OTA5NjA-is.not-DCeFB-a.real-r4DQlO-t0ken-qerT0",
  "prefix" : "+"
}

You then need to run a couple of things to make this work. Those things are run in console, which can be opened by using SHIFT+Right-Click in your folder, and selected Open command prompt here (your version of windows might say "PowerShell", don't worry it'll work fine!). In console, use the following commands:

npm init -y
npm install [email protected]
node index.js

If you've done things right, that should start the bot.

Support

All support for bot code is offered in the Idiot's Guide Official Server.

COMMENTS BELOW ARE NOT MONITORED AND YOU WILL NOT RECEIVE SUPPORT HERE

COMMENTS BELOW ARE NOT MONITORED AND YOU WILL NOT RECEIVE SUPPORT HERE

COMMENTS BELOW ARE NOT MONITORED AND YOU WILL NOT RECEIVE SUPPORT HERE

COMMENTS BELOW ARE NOT MONITORED AND YOU WILL NOT RECEIVE SUPPORT HERE

// Load up the discord.js library
const Discord = require("discord.js");
// This is your client. Some people call it `bot`, some people call it `self`,
// some might call it `cootchie`. Either way, when you see `client.something`, or `bot.something`,
// this is what we're refering to. Your client.
const client = new Discord.Client();
// Here we load the config.json file that contains our token and our prefix values.
const config = require("./config.json");
// config.token contains the bot's token
// config.prefix contains the message prefix.
client.on("ready", () => {
// This event will run if the bot starts, and logs in, successfully.
console.log(`Bot has started, with ${client.users.cache.size} users, in ${client.channels.cache.size} channels of ${client.guilds.cache.size} guilds.`);
// Example of changing the bot's playing game to something useful. `client.user` is what the
// docs refer to as the "ClientUser".
client.user.setActivity(`Serving ${client.guilds.cache.size} servers`);
});
client.on("guildCreate", guild => {
// This event triggers when the bot joins a guild.
console.log(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`);
client.user.setActivity(`Serving ${client.guilds.cache.size} servers`);
});
client.on("guildDelete", guild => {
// this event triggers when the bot is removed from a guild.
console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`);
client.user.setActivity(`Serving ${client.guilds.cache.size} servers`);
});
client.on("message", async message => {
// This event will run on every single message received, from any channel or DM.
// It's good practice to ignore other bots. This also makes your bot ignore itself
// and not get into a spam loop (we call that "botception").
if(message.author.bot) return;
// Also good practice to ignore any message that does not start with our prefix,
// which is set in the configuration file.
// `startsWith` was added in ES2015 and you can use this instead of
// if(message.content.indexOf(config.prefix) !== 0) return;
if(!message.content.startsWith(config.prefix)) return;
// Here we separate our "command" name, and our "arguments" for the command.
// e.g. if we have the message "+say Is this the real life?" , we'll get the following:
// command = say
// args = ["Is", "this", "the", "real", "life?"]
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
// Let's go with a few common example commands! Feel free to delete or change those.
if(command === "ping") {
// Calculates ping between sending a message and editing it, giving a nice round-trip latency.
// The second ping is an average latency between the bot and the websocket server (one-way, not round-trip)
const m = await message.channel.send("Ping?");
m.edit(`Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
}
if(command === "say") {
// makes the bot say something and delete the message. As an example, it's open to anyone to use.
// To get the "message" itself we join the `args` back into a string with spaces:
const sayMessage = args.join(" ");
// Then we delete the command message (sneaky, right?). The catch just ignores the error with a cute smiley thing.
message.delete().catch(O_o=>{});
// And we get the bot to say the thing:
message.channel.send(sayMessage);
}
if(command === "kick") {
// This command must be limited to mods and admins. In this example we just hardcode the role names.
// Please read on Array.some() to understand this bit:
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some?
if(!message.member.roles.cache.some(r=>["Administrator", "Moderator"].includes(r.name)))
return message.reply("Sorry, you don't have permissions to use this!");
// Let's first check if we have a member and if we can kick them!
// message.mentions.members is a collection of people that have been mentioned, as GuildMembers.
// We can also support getting the member by ID, which would be args[0]
let member = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
if(!member)
return message.reply("Please mention a valid member of this server");
if(!member.kickable)
return message.reply("I cannot kick this user! Do they have a higher role? Do I have kick permissions?");
// slice(1) removes the first part, which here should be the user mention or ID
// join(" ") takes all the various parts to make it a single string.
let reason = args.slice(1).join(" ");
if(!reason) reason = "No reason provided";
// Now, time for a swift kick in the nuts!
await member.kick(reason)
.catch(error => message.reply(`Sorry ${message.author} I couldn't kick because of : ${error}`));
message.reply(`${member.user.tag} has been kicked by ${message.author.tag} because: ${reason}`);
}
if(command === "ban") {
// Most of this command is identical to kick, except that here we'll only let admins do it.
// In the real world mods could ban too, but this is just an example, right? ;)
if(!message.member.roles.cache.some(r=>["Administrator"].includes(r.name)))
return message.reply("Sorry, you don't have permissions to use this!");
let member = message.mentions.members.first();
if(!member)
return message.reply("Please mention a valid member of this server");
if(!member.bannable)
return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");
let reason = args.slice(1).join(" ");
if(!reason) reason = "No reason provided";
await member.ban(reason)
.catch(error => message.reply(`Sorry ${message.author} I couldn't ban because of : ${error}`));
message.reply(`${member.user.tag} has been banned by ${message.author.tag} because: ${reason}`);
}
if(command === "purge") {
// This command removes all messages from all users in the channel, up to 100.
// get the delete count, as an actual number.
const deleteCount = parseInt(args[0], 10);
// Ooooh nice, combined conditions. <3
if(!deleteCount || deleteCount < 2 || deleteCount > 100)
return message.reply("Please provide a number between 2 and 100 for the number of messages to delete");
// So we bulk delete the last `deleteCount` messages. Simple enough, right?
message.channel.bulkDelete(deleteCount)
.catch(error => message.reply(`Couldn't delete messages because of: ${error}`));
}
});
client.login(config.token);
@cherryblossom000
Copy link
Author

See my comment here for the differences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment