首页 > 解决方案 > Discord 机器人删除命令

问题描述

我的主要目标是让具有消息管理权限的人键入删除消息的命令,但能够指定他们要删除的消息数量。

我试过弄乱变量,但我对它们知之甚少,通常会以一些错误结束。我尝试用成功的变量替换已删除消息的值(2),但在使用消息更改变量时我一无所知。

 if(message.member.hasPermission('MANAGE_MESSAGES')) {
                        if(message.content.startsWith(`${prefix}delete`)) {
                        message.channel.bulkDelete(2)
                        }
                    }

标签: javascriptnode.jsdiscord.js

解决方案


我假设您在消息事件中拥有它。

这是您可以做到的众多方法之一:

if(message.content.startsWith(`${prefix}delete`)) {
      const user = message.mentions.users.first();
      // Parse Amount
      const amount = !!parseInt(message.content.split(' ')[1]) ? parseInt(message.content.split(' ')[1]) : parseInt(message.content.split(' ')[2])
      //Check if it the amount for message to delete where declared
      if (!amount) return message.reply('Must specify an amount to delete!').then(msg => msg.delete(15000));
      // Fetch 100 messages (will be filtered and lowered up to max amount requested)
      message.channel.fetchMessages({
        limit: 100,
      }).then((messages) => {
          //I declare the messages like that with amount + 1 to delete the command itself
          messages = messages.array().slice(0, amount + 1);
          //And finally buldDelete deletes the desired amount
          message.channel.bulkDelete(messages).then(messages => console.log(`Bulk deleted ${args[0]} messages`))
            .catch(console.error);


      });

推荐阅读