📜  discord.js v11 - Javascript (1)

📅  最后修改于: 2023-12-03 15:00:25.466000             🧑  作者: Mango

Discord.js v11 - Javascript

Discord.js is a powerful Node.js module that allows you to interact with the Discord API. It makes it easy to build bots and other Discord applications, and provides a lot of useful functionality out of the box.

Getting Started

To get started with Discord.js, you'll need to install it via npm:

npm install discord.js

Once you've installed Discord.js, you can create a new Discord client and start interacting with the API:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('Pong!');
  }
});

client.login('your-token-here');
Useful Functionality

Discord.js provides a lot of useful functionality out of the box. Here are a few examples:

Sending Messages

// Send a message to a channel
msg.channel.send('Hello, world!');

// Send a message to a specific user
const user = client.users.get('1234567890');
user.send('Hi there!');

Embeds

const embed = new Discord.RichEmbed()
  .setTitle('My Embed')
  .setColor(0xff0000)
  .setDescription('Hello, world!')
  .setFooter('Powered by Discord.js', client.user.avatarURL)
  .setTimestamp();

msg.channel.send(embed);

Reactions

// React with a thumbs up emoji
msg.react('👍');

// Wait for the user to react with a thumbs up emoji
const filter = (reaction, user) => reaction.emoji.name === '👍' && user.id === '1234567890';
const collector = msg.createReactionCollector(filter);

collector.on('collect', () => {
  console.log('User reacted with thumbs up!');
});
Documentation

Discord.js provides comprehensive documentation on its website, including a Getting Started guide, API reference, and many examples:

https://discord.js.org

Conclusion

Discord.js is a powerful tool for building Discord bots and other applications. It provides a lot of useful functionality out of the box, and is well-documented. With Discord.js, you can easily interact with the Discord API and create powerful applications.