Miscellaneous Examples
Conventions Used in Examples
Conventions are important - they are the agreements on which society functions. So let's take a moment to agree on a few.
Placeholders
A "Placeholder" is a piece of text that replaces something else. In these FAQs we assume the following variables as "placeholders" for your own:
clientis a placeholder that corresponds to yourclientvariable, as we've covered at the end of the Getting Started guide.client.on("ready", () => {for example.messageis a placeholder corresponds to yourmessageCreateevent's variable which looks something like this:client.on("messageCreate", message => {.
Examples
awaitMessages
This example sends a question and waits to receive a message response that says test.
message.channel.send("What tag would you like to see? This will await will be cancelled in 30 seconds. It will finish when you provide a message that goes through the filter the first time.")
.then(() => {
message.channel.awaitMessages(response => response.content === "test", {
max: 1,
time: 30000,
errors: ["time"],
})
.then((collected) => {
message.channel.send(`The collected message was: ${collected.first().content}`);
})
.catch(() => {
message.channel.send("There was no collected message that passed the filter within the time limit!");
});
});Creating a guild
Discord quietly changed the Create Guild API endpoint, small bots (10 guilds or fewer) are able to create guilds programmatically now. This example will have your bot create a new guild and create a role with the administrator permission, and the single line of code at the bottom will apply it to you when you execute it when you join the guild.
Command Cooldown
Adds a cooldown to your commands so the user will have to wait 2.5 seconds between each command.
You can change the nature of the cool down by changing the return to something else.
Mention Prefix
Requiring a little bit of regex, this will catch when a message starts with the bot being mentioned.
Multiple Prefixes
Let's make it 3 prefixes, this is fairly universal. This could also be in the config.json once you get there. So we'll start by finding if the message content starts with either of the prefixes mentioned in the array. If it doesn't, we return.
Multiple Prefixes Extension
Purging a Channel
Example usage: !purge @user 10 , or !purge 25
Swear Detector
This quick & dirty swear detector takes an array of swear words we don't want to see, and triggers on it.
Kicking users (or bots) from a voice channel
Support for kicking members from voice channels has now been added by Discord and can be achieved by doing the following.
This does the same as clicking the disconnect button on a user or bot while they are in a voice channel.
Last updated