📅  最后修改于: 2023-12-03 15:35:02.101000             🧑  作者: Mango
Discord.js is a powerful library for building Discord bots in JavaScript. With a simple and intuitive API, it makes it easy to handle events, create commands, and interact with the Discord API.
To install Discord.js, simply use npm:
$ npm install discord.js
To start using Discord.js, create a new file and import the library:
const Discord = require('discord.js');
Then, create a new client using the Discord.Client
class:
const client = new Discord.Client();
Next, add event listeners to handle messages, ready state, and other events:
client.on('message', message => {
if (message.content === '!ping') {
message.reply('pong');
}
});
client.once('ready', () => {
console.log('Ready!');
});
client.on('error', console.error);
client.login('your-token-goes-here');
By listening to the message
event, we can check if the message content matches a certain command and respond accordingly. The ready
event is used to know when the client is fully logged in and ready to perform actions. The error
event is used to log any errors that occur during runtime.
Lastly, we call client.login()
with your bot token to start the client and connect to Discord.
Discord.js is an easy-to-use and powerful library for building Discord bots in JavaScript. Its simple API and built-in command handling make it a great choice for beginners and experienced programmers alike.