简体   繁体   English

使用 Discord.js 发送消息

[英]Send a message with Discord.js

I am trying to make a discord bot, but I can't quite understand Discord.js.我正在尝试制作一个不和谐的机器人,但我不太了解 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.问题是我不太明白如何发送消息。

Can anybody help me ?有谁能够帮助我 ?

You have an error in your .send() line.您的.send()行中有错误。 The current code that you have was used in an earlier version of the discord.js library, and the method to achieve this has been changed.您拥有的当前代码用于早期版本的 discord.js 库,实现此目的的方法已更改。

If you have a message object, such as in a message event handler, you can send a message to the channel of the message object like so:如果您有消息对象,例如在message事件处理程序中,您可以像这样向消息对象的通道发送消息:

message.channel.send("My Message");

An example of that from a message event handler:来自message事件处理程序的示例:

client.on("message", function(message) {
  message.channel.send("My Message");
});

You can also send a message to a specific channel , which you can do by first getting the channel using its ID, then sending a message to it:您还可以向特定频道发送消息,您可以首先使用其 ID 获取频道,然后向其发送消息:

(using async/await) (使用异步/等待)

const channel = await client.channels.fetch(channelID);
channel.send("My Message");

(using Promise callbacks) (使用Promise回调)

client.channels.fetch(channelID).then(channel => {
  channel.send("My Message");
});

Works as of Discord.js version 12Discord.js版本 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.对于版本 12,下面将是正确的代码。 The details about this code are available in this link .有关此代码的详细信息可在此链接中找到

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 版之后来到这里的人。

The top answer is outdated最佳答案已过时

New way is:新方法是:

const channel = await client.channels.fetch(<id>);

await channel.send('hi')

To add a little context on getting the channel Id;添加有关获取频道 ID 的一些上下文; The list of all the channels is stored in the client.channels property.所有频道的列表存储在client.channels属性中。

A simple console.log(client.channels) will reveal an array of all the channels on that server.一个简单的console.log(client.channels)将显示该服务器上所有频道的数组。

Below is the code to dm the user:以下是 dm 用户的代码:

(In this case our message is not a response but a new message sent directly to the 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:更多文档:

https://github.com/AnIdiotsGuide/discordjs-bot-guide/blob/master/frequently-asked-questions.md#users-and-members https://github.com/AnIdiotsGuide/discordjs-bot-guide/blob/master/frequently-asked-questions.md#users-and-members

There are four ways you could approach what you are trying to achieve, you can use message.reply("Pong") which mentions the user or use message.channel.send("Pong") which will not mention the user, additionally in discord.js you have the option to send embeds which you do through:您可以通过四种方法来实现您想要实现的目标,您可以使用message.reply("Pong")提及用户或使用message.channel.send("Pong")不会提及用户,另外在discord.js您可以选择通过以下方式发送嵌入:

client.on("message", () => {
    var message = new Discord.MessageEmbed()
      .setDescription("Pong") // sets the body of it
      .setColor("somecolor")
      .setThumbnail("./image");
      .setAuthor("Random Person")
      .setTitle("This is an embed")
    msg.channel.send(message) // without mention
    msg.reply(message) // with mention
})

There is also the option to dm the user which can be achieved by:还可以选择 dm 用户,可以通过以下方式实现:

client.on("message", (msg) => {
msg.author.send("This is a dm")

})

See the official documentation .请参阅官方文档

It's message.channel.send("content");这是message.channel.send("content"); since you're sending a message to the current channel.因为您正在向当前频道发送消息。

You can only send a message to a channel您只能向频道发送消息

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

If you want to DM the user, then you can use the User.send() function如果你想 DM 用户,那么你可以使用 User.send() 函数

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

Types of ways to send a message: DM'ing whoever ran the command:发送消息的方式类型:向运行命令的人发送消息:

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

Sends the message in the channel that the command was used in:在使用该命令的通道中发送消息:

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

Sends the message in a specific channel:在特定频道中发送消息:

client.on('message', function(message) {
const channel = client.channels.get("<channel id>")
 if (message.content === 'ping') {
  channel.send("pong")
 }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM