使用 Discord.js 发送消息

2022-01-10 00:00:00 discord node.js javascript discord.js

我正在尝试制作一个不和谐的机器人,但我不太了解 Discord.js.我的代码如下所示:

I am trying to make a discord bot, but I can't quite understand Discord.js. My code looks like this:

client.on('message', function(message) {
 if (message.content === 'ping') {
  client.message.send(author, 'pong');
 }
});

问题是我不太明白如何发送消息.

And the problem is that I can't quite understand how to send a message.

谁能帮帮我?

推荐答案

发送码又改了.问题和答案中的项目都已过时.对于版本 12,下面将是正确的代码.有关此代码的详细信息,请参阅此 链接.

The send code has been changed again. Both the items in the question as well as in the answers are all outdated. For version 12, below will be the right code. The details about this code are available in this link.

向特定频道发送消息

const channel = <client>.channels.cache.get('<id>');
channel.send('<content>');

向 DM 中的特定用户发送消息

To send a message to a specific user in DM

const user = <client>.users.cache.get('<id>');
user.send('<content>');

如果你想给用户发私信,请注意机器人和用户应该至少有一个共同的服务器.

If you want to DM a user, please note that the bot and the user should have at least one server in common.

希望这个答案对版本 12 之后来这里的人有所帮助.

Hope this answer helps people who come here after version 12.

相关文章