【问题标题】:Deleting my messages in a server using bulkDelete (discord.js)使用 bulkDelete (discord.js) 在服务器中删除我的消息
【发布时间】:2019-02-18 11:52:06
【问题描述】:

我是 discord.js 的新手,但了解到我可以使用 bulkDelete 删除我的消息,即使它们超过 2 周,它也会全部删除。我每月在我手动审核一次的服务器中清除我的消息,不用说这需要很长时间。我想知道是否有人能够帮助我创建一个命令,只要我调用它就会自动执行此操作?

谢谢, 克

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    我会设置一个递归函数来检查频道中是否有消息(每次最多 100 条):如果没有消息它会停止,否则它会删除它们并重新启动。

    function clean(channel, limit = 100) {
      return channel.fetchMessages({limit}).then(async collected => {
        let mine = collected.filter(m => m.author.id == 'your_id_here'); // this gets only your messages
        if (mine.size > 0) {
          await channel.bulkDelete(mine, true);
          clean(channel);
        } else channel.send("The channel is now empty!").delete(5000); // this message is deleted after 5 s
      });
    }
    

    您可以将此想法应用到您现有的命令解析器中,或者,如果您不知道如何实现,请尝试:

    client.on('message', msg => {
      if (msg.author.bot || msg.author != YOU) return; 
      // with YOU i mean your User object, to check permissions
    
      let command = 'clean', // the name of your command
        args = msg.content.split(' ');
      if (args[0].toLowerCase() == command) 
        clean(msg.channel, !isNaN(args[1]) ? args[1] : undefined); //<-- THIS is how to use the function
      // used a ternary operator to check if the other arg is a number
    });
    

    这只是一个非常基本的实现,还有很多更好的检测命令的方法。

    【讨论】:

    • 这会删除所有邮件,我只是想让它搜索我自己的并删除它们,即使它们超过 2 周。
    【解决方案2】:

    我刚刚找到了一种过滤消息的方法。 您可以获取消息,然后检查每条消息是否是您的

    await message.channel.fetchMessages({
      limit: 100
    }).then((msgCollection) => {
      msgCollection.forEach((msg) => {
        if(msg.author.id == message.author.id) {
          msg.delete();
        }
      })
    });
    

    【讨论】:

      猜你喜欢
      • 2021-01-26
      • 2018-02-02
      • 1970-01-01
      • 2020-09-07
      • 2018-06-05
      • 2020-07-15
      • 1970-01-01
      • 2018-08-26
      • 2020-07-04
      相关资源
      最近更新 更多