![]() |
VOOZH | about |
The author selected the Free and Open Source Fund to receive a donation as part of the Write for DOnations program.
Discord is a chat application that allows millions of users across the globe to message and voice chat online in communities called guilds or servers. Discord also provides an extensive API that developers can use to build powerful Discord bots. Bots can perform various actions such as sending messages to servers, DM-ing users, moderating servers, and playing audio in voice chats. This allows developers to craft powerful bots that include advanced, complex features like moderation tools or even games. For example, the utility bot Dyno serves millions of guilds and contains useful features such as spam protection, a music player, and other utility functions. Learning how to create Discord bots allows you to implement many possibilities, which thousands of people could interact with every day.
In this tutorial, you will build a Discord bot from scratch, using Node.js and the Discord.js library, which allows users to directly interact with the Discord API. You’ll set up a profile for a Discord bot, get authentication tokens for the bot, and program the bot with the ability to process commands with arguments from users.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Former Senior Technical Editor at DigitalOcean, with a strong focus on DevOps and System Administration content. Areas of expertise include Terraform, PyTorch, Python, and Django.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Not recommended.
No command handler (all commands in the main file). No informations about intents (very important when the bot hit 100+ servers). Nothing about slash commands. So it’s not “How To Build a Discord Bot with Node.js” but “how to build a very useless basic discord bot with node.js”.
Don’t waste your time and take a look on YouTube.
Correct slash command link: https://discordjs.guide/interactions/slash-commands.html#registering-slash-commands
If you aren’t able to make the bot an administrator, then at least give it as many options possible under the “text permissions” column. When you follow the link to add the bot to the channel, make sure there is a green checkmark next to each of the items you selected. If you notice any denied permissions then your bot will not be able to do those activities. You may have to adjust your permissions on the server you are attempting to add the bot.
There are also new setting for a bot you may want to adjust in the https://discord.com/developers/applications/
Here is a new code snippt using discord.js v14
// Require the necessary discord.js classes
const { Client, Events, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
client.on("messageCreate", (message) => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const commandBody = message.content.slice(prefix.length);
const args = commandBody.split(' ');
const command = args.shift().toLowerCase();
if (command === "ping") {
const timeTaken = Date.now() - message.createdTimestamp;
message.reply("Pong! This message had a latency of " + timeTaken.ms + ".");
}
else if (command === "sum") {
const numArgs = args.map(x => parseFloat(x));
const sum = numArgs.reduce((counter, x) => counter += x);
message.reply("The sum of all the arguments you provided is " + sum + "!");
}
});
If you want your script to always stay running on your server in the background, look into the forever npm package: https://www.npmjs.com/package/forever
Once you install it you can run your script: forever index.js
If you want to see what scripts are running you can use forever list
If you want forever to watch your file for changes and automatically reload it, you can run it with the -w hook:
forever -w index.js
This comment has been deleted
If you want to node script to run forever in the background, checkout the forever npm package: https://www.npmjs.com/package/forever
If you want forever to watch your file for changes and automatically reload, run it with the -w flag:
forever -w index.js
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.