【问题标题】:Delete request message删除请求消息
【发布时间】:2021-08-11 11:33:46
【问题描述】:

如何删除请求机器人的消息?想象一下这是不和谐的:

Me
!quote

Bot
'quote........' - someone

然后机器人会删除它自己的消息以及我的消息。

我得到这个代码来删除机器人自己的消息:

if (msg.content === ping_char+'quote') {
    var randomItem = quotes[Math.floor(Math.random()*quotes.length)];
    msg.channel.send(randomItem).then(msg => msg.delete({timeout: 10000}));
  }

但如果可能的话,我不知道如何删除最初启动机器人的消息。我的代码基本上清除了最后两条消息,在这种情况下是机器人和用户/我的?

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    channel.send() 返回发送的消息(带有随机引号的消息),您将其命名为 msg,就像您传入的消息一样(它也是 msg)。确保这些是具有两个不同名称的两个不同变量。如果您将原始消息命名为message,并将发送的消息命名为sentMessage,则您希望在发送消息后删除message

    查看下面的工作代码:

    client.on('message', (message) => {
      if (message.author.bot) return;
    
      if (message.content === `${ping_char}quote`) {
        const randomItem = quotes[Math.floor(Math.random() * quotes.length)];
        message.channel
          .send(randomItem)
          // you want to delete the original message, not sentMessage
          .then((sentMessage) => message.delete({ timeout: 10000 }))
          .catch(console.error);
      }
    });
    

    【讨论】:

    • 好的,所以我使用了您的代码并添加到其中,希望机器人随后会删除他的消息,但机器人不会删除他的消息,这是代码:if (msg.content === ping_char+'quote') { const randomItem = quotes[Math.floor(Math.random() * quotes.length)]; msg.channel .send(randomItem) .then((sentMessage) => msg.delete({ timeout: 100 })) .then(msg => msg.delete({timeout: 10000})) .catch(console.error); }@Zsolt M跨度>
    • 请尝试再次阅读我的答案,因为这解释了您上面的代码有什么问题。
    猜你喜欢
    • 2018-06-29
    • 2012-01-03
    • 2014-02-03
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多