What is discord.js?
discord.js is a powerful Node.js module that allows you to interact with the Discord API very easily. It provides a comprehensive set of features to create bots and manage Discord servers.
What are discord.js's main functionalities?
Creating a Bot
This code demonstrates how to create a simple Discord bot using discord.js. The bot logs 'Ready!' to the console when it is successfully logged in and ready.
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once('ready', () => {
console.log('Ready!');
});
client.login('your-token-goes-here');
Handling Messages
This code shows how to handle messages in a Discord server. When a user sends a message with the content '!ping', the bot responds with 'Pong!'.
client.on('messageCreate', message => {
if (message.content === '!ping') {
message.channel.send('Pong!');
}
});
Managing Roles
This code demonstrates how to manage roles in a Discord server. When a user sends a message with the content '!addRole', the bot adds a role named 'NewRole' to the user.
client.on('messageCreate', async message => {
if (message.content === '!addRole') {
let role = message.guild.roles.cache.find(r => r.name === 'NewRole');
if (role) {
await message.member.roles.add(role);
message.channel.send('Role added!');
}
}
});
Sending Embeds
This code shows how to send embedded messages in Discord. When a user sends a message with the content '!embed', the bot responds with a rich embed message.
const { MessageEmbed } = require('discord.js');
client.on('messageCreate', message => {
if (message.content === '!embed') {
const embed = new MessageEmbed()
.setTitle('Sample Embed')
.setDescription('This is an example of an embed message')
.setColor(0xff0000);
message.channel.send({ embeds: [embed] });
}
});
Other packages similar to discord.js
eris
Eris is another powerful library for interacting with the Discord API. It is known for being lightweight and efficient, making it a good alternative to discord.js. However, it may have a steeper learning curve for beginners.
discord.io
discord.io is a small, lightweight library for interfacing with Discord. It is less feature-rich compared to discord.js and Eris, but it can be a good choice for simpler bots or for those who prefer a minimalistic approach.
discord.js is a node module used as a way of interfacing with
Discord. It is a very useful module for creating
bots.
The examples in the repo are in ES6, either update your node or compile them down to babel yourself if you want to use them!
Installation
npm install --save discord.js
Example
var Discord = require("discord.js");
var mybot = new Discord.Client();
mybot.on("message", function(message){
if(message.content === "ping")
mybot.reply(message, "pong");
});
mybot.login("email", "password");
What's new in 3.9.0?
Amongst some fixes to web distribution creation, you can now opt for easier string formatting! However, it does modify String globally so you'll have to run:
Discord.patchStrings()
After you have run this, you can do:
"message".bold.underline.italic
// generates "*__**message**__*"
A full list of modifiers (all chainable):
- bold
**
- italic
*
- underline
__
- strike
~
- code
` - codeblock
```
Related Projects
Here is a list of other Discord APIs:
Java:
Discord4J
.NET:
Discord.Net
DiscordSharp
NodeJS
discord.io (similar to discord.js but lower level)
PHP
DiscordPHP
Python
discord.py
Ruby
discordrb
Links
Documentation
GitHub
Wiki
Website
NPM
Contact
If you have an issue or want to know if a feature exists, read the documentation before contacting me about any issues! If it's badly/wrongly implemented, let me know!
If you would like to contact me, you can create an issue on the GitHub repo, e-mail me via the one available on my NPM profile.
Or you could just send a DM to hydrabolt in Discord API.