【问题标题】:Using Discord.js, how can I trigger a bot when a webhook is triggered?使用 Discord.js,如何在触发 webhook 时触发机器人?
【发布时间】:2021-09-01 15:52:27
【问题描述】:

我在服务器中有一个机器人(例如,假设服务器称为“John's Server”),所以在 John's Server 中也有一个 webhook。

我想要做的是,每当在 John 的服务器中发送 webhook 消息时,机器人都会在通道中发送消息。 (例如,假设频道 ID 为 1234567890)

因此,如果 John 的服务器中 ID 为 0000000000 的 webhook 用于发送消息,则机器人会在通道 1234567890 中发送消息。

这怎么可能? (顺便说一下,使用 Discord.js)

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    为此,您必须监听 message 事件,当它触发时,您将检查 Message#webhookID 属性。如果它具有真值,则消息是从 webhook 发送的,否则它是从用户发送的。 然后你需要做的就是从服务器获取频道并发送消息。

    这是一个例子:

    client.on("message", (message) => {
      if (!message.webhookID) return; // If the message isn't from a webhook, don't do anything
      const channel = message.guild.channels.fetch("0123456789");
      // Using the fetch method, discord.js will check the cache first, if the channel is there
      // then it will return it, otherwise discord.js will hit the Discord API and get the channel
      
      channel.send(`
    A message from ${message.author.username} (${message.webhookID}) has been sent in 
    ${message.channel.name}.
    Message: ${message.url}
      `);
    });
    

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 2021-08-19
      • 2019-12-17
      • 1970-01-01
      • 2021-06-15
      • 2019-10-30
      • 2018-08-15
      • 2021-10-12
      • 1970-01-01
      相关资源
      最近更新 更多