如何让 DIscord.js 机器人真正回复消息?

时间:2021-03-30 10:24:03

标签: javascript discord discord.js

我一直在制作一个支持“say”命令类型的 Discord.js (V12) 机器人(请注意我是新手),但是当我使用这个“say”命令并单击“回复”时在特定消息中,它只发送我写的消息,它根本不显示我正在回复的原始消息。 Here is what I wrote with the bothere the result

我的“说”命令代码很简单,因为我找不到实际的回复功能,所以我无法添加它(我不得不删除内容以便更准确)。

client.on('message', (message) => {
    if (message.content.startsWith('p:')) {
      //This line reads the content of the message, creates the variable "say" for it.
        var say = message.content;
      //This removes my message, so it can be replaced by bot's one.
        message.delete();
      //This line deletes the prefix from "say" and sends the rest of the message.
        message.channel.send(say.replace('p:', '')
    .catch(() => console.error('Failed to send - Can\'t send empty messages!'));
    }
});

我发现还有另一种方式可以进行回复。 Here the two: The first one is the normal and the other is by a Tupperhook (Webhook) created by the bot Tupperbox.

有没有办法至少为机器人制作一个 Webhook 回复的版本?

在 Webhook 方法可以工作的情况下,我已经有办法跳转到一条消息(在这个例子中,原始消息正在触发一个特定的命令 - 这将被更改为跳转到回复的消息)内部嵌入,这是它的代码。

//These should be in the embed command.
//This line recognizes the servers the bot is in.
  var guild = message.guild
//This other line recognizes the channels of the servers.
  var channel = message.channel
//This one sends the word "Message" and includes a link to the message that triggered this command.
  `[Message](https://discordapp.com/channels/${guild.id}/${channel.id}/${message.id})`

是否可以使我的代码适应任何这些回复功能?我发誓我从一个月开始就一直在尝试这个。任何答案都非常感谢!

1 个答案:

答案 0 :(得分:0)

在 v12 中无法做到这一点,但可以通过更具体的 API 请求 Create Message

允许我们回复消息的字段是 message_reference

const fetch = require('node-fetch')
client.on("message", (message) => {
  if (message.author.bot) return;
  const url = `https://discord.com/api/v8/channels/${message.channel.id}/messages`;
  var payload = {
    content: "Hello, World!",
    tts: false,
    message_reference: {
      message_id: message.id
    }
  };
   fetch(url, {
    method: "POST",
    body: JSON.stringify(payload),
    headers: {
      Authorization: `${client.user?.bot ? "Bot " : ""}${client.token}`,
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
  }).catch(() => { });
});