39
loading...
This website collects cookies to deliver better user experience
“It is important to view knowledge as sort of a semantic tree -- make sure you understand the fundamental principles, i.e. the trunk and big branches, before you get into the leaves/details or there is nothing for them to hang on to.” ~ Elon Musk
cmd
and pressing enter. You will want to navigate to your project folder now. This can be done using the cd <path to folder>
command.package.json
. This file holds important information about your project, like the libraries used, its name, etc... The following command makes this file.> npm init -y
> npm i discord.js@dev --save
--save
means we save it in our package.json
file. Whenever we run npm i
now it will check if discord.js is already installed, if not it will install it.index.js
. Let's stick to this convention. Make a new file named index.js
. Open this file with your favorite code editor or IDE. Now we can finally get started with:const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('messageCreate', message => {
if (message.content === 'ping') {
message.channel.send('pong');
}
});
client.login('<place token here>');
const { Client, Intents } = require('discord.js');
const Discord = require('discord.js');
Client = Discord.Client;
Intents = Discord.Intents;
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
ClientOptions
. Which are the 'Options for the client'. Note that what I just mentioned is literally described in the docs here! You can specify a lot of options, to view which options you can change, click ClientOptions
. There is a lot of other information provided here. Like the properties, methods and events Client
has. Take your time to read all of them briefly...ClientOptions
here, you can see that one field is required to be set. Namely, the intents. Intents are what you want to access. In our case we want to be able to interact with guilds (servers) and the messages sent in these guilds. Therefore we also require Intents
from discord.js. What is Intents
, you may (probably not) ask. Why, thank you for asking. It is this. Indeed, I am referring to the docs once more! Everything there is to know is written there afterall. Let us take a look at that piece of code again:const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
FLAGS
property of Intents
. Which contains all the available intents. ready
event. This event is 'Emitted when the client becomes ready to start working.'client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
messageCreate
event and the message
event. Since the latter is deprecated we will be using messageCreate
. This event is 'Emitted whenever a message is created'.client.on('messageCreate', message => {
// ...
});
client.on('messageCreate', message => {
if (message.content === 'ping') {
message.channel.send('pong');
}
});
Message
has is content
. Which, unbelievably, contains the content of the message! message.content
will also be "Hello World". In the above code, IF the content of the message is (strictly) equal to 'ping', THEN do whatever you want it do to. message.channel.send('pong');
Message
object has a property called channel
. This gives us information about exactly that. Assuming you are still at the page about the Message
object (here), scroll down until you find the channel
property. As you can see it can have several types: TextChannel, DMChannel, NewsChannel, ThreadChannel. We only have to focus on the TextChannel. When you click TextChannel you will, again, see all its properties and methods. send
. Send takes as arguments options
. To keep it simple we only give the content of the message we want to send back.client.login('<place token here>');
<place token here>
. DO NEVER SHARE THIS TOKEN WITH ANYONE ELSE Anybody with that token can access your bot. With that done, we can finally move to:cd <path to project folder>
. You can now start your bot with:> node .
OAuth2
. Scroll down to OAuth2 URL Generator
. bot
option in the middle. Scroll further down and select permissions you want your bot to have. For now I recommend to just give it Administrator permissions. Copy the URL, paste it in a new browser tab. Accept all, and the bot should now be in the selected server.