Javascript 使用 Discord.js 发送消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45120618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send a message with Discord.js
提问by Gabe
So. I am trying to make a discord bot, but i can't quite understand Discord.js. My code looks like this:
所以。我正在尝试制作一个不和谐的机器人,但我不太了解 Discord.js。我的代码如下所示:
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.
问题是我不太明白如何发送消息。
Can anybody help me ?
有谁能够帮助我 ?
采纳答案by Balasubramanian S
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.
发送代码已再次更改。问题和答案中的项目都已过时。对于版本 12,下面将是正确的代码。有关此代码的详细信息可在此链接中找到。
To send a message to specific channel
向特定频道发送消息
const channel = <client>.channels.cache.get('<id>');
channel.send('<content>');
To send a message to a specific user in DM
向 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.
如果您想 DM 用户,请注意机器人和用户应该至少有一个共同的服务器。
Hope this answer helps people who come here after version 12.
希望这个答案可以帮助那些在 12 版之后来到这里的人。
回答by Arct
You have an error in your .send()line. The current code that you have is used in an earlier version of the discord.js library, and the send function has been changed.
你的.send()线路有错误。您拥有的当前代码用于早期版本的 discord.js 库,并且发送函数已更改。
To send a message, use this line:
要发送消息,请使用以下行:
message.channel.send('My Message')
If you get an error saying that messageis not defined, make sure that you have put the line in your message event handler.
如果您收到一条错误消息,指出messagenot defined,请确保您已将该行放入您的消息事件处理程序中。
client.on("message", function(message) {
//message sending goes here
});
You can also send a message to a specific channel, which you can do using the line below.
您还可以向特定频道发送消息,您可以使用下面的行来执行此操作。
client.channels.get(channelID).send('My Message');
Or if you prefer, a guild's default channel (the #general channel that was made when the guild was created)
或者,如果您愿意,可以使用公会的默认频道(创建公会时创建的 #general 频道)
guildObj.defaultChannel.send('My Message');
Hope this helped,
希望这有帮助,
- Spy
- 间谍
回答by Daniel
Below I presenting script that send direct message to user.
下面我展示了向用户发送直接消息的脚本。
In this case our message is not a response but new message send directly to selected user.
在这种情况下,我们的消息不是响应,而是直接发送给选定用户的新消息。
require('dotenv').config({ path: __dirname + '/.env.local' });
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("ready", () => {
console.log(client.users.get('ID_OF_USER').send("hello"));
});
client.login(process.env.DISCORD_BOT_TOKEN);
Further documentation:
更多文档:

