【问题标题】:How to write the clear command in discord.js v12.5.3 USING ARGS[]如何使用 ARGS[] 在 discord.js v12.5.3 中编写 clear 命令
【发布时间】:2021-04-27 10:53:44
【问题描述】:

我正在尝试为我的自定义 discord 机器人制定一个明确的命令,并且我已经编写了一些代码并且它运行良好!问题是该命令删除的消息比它被告知的少 1 条消息,例如:>clear 5 删除 4 条消息,我知道这是因为为启动批量删除而编写的命令本身是一条消息,但我不想要这发生了,args[0] + 1 似乎不起作用!

这是我当前的代码:

module.exports = {
    name: 'clear',
    description: "Delete/Clear messages with a simple command",
    async execute(message, args) {
        if (message.member.permissions.has("KICK_MEMBERS")) {
            if (!args[0]) return message.reply("Please enter the amount of messages to clear!");

            if (isNaN(args[0])) return message.reply("Please type a real number!");

            if (args[0] > 100) return message.reply("You can't remove more than 100 messages at once!");

            if (args[0] < 1) return message.reply("You have to delete at least one message!");

            await message.channel.messages.fetch({ limit: args[0] }).then(messages => {
                message.channel.bulkDelete(messages);
                message.channel.send("<a:ces_Tick:774694116042342411> **Successfully deleted " + "`" + args[0] + " messages`**")
                    .then(msg => {
                        msg.delete({ timeout: 3000 })
                    })
            });
        } else {
            message.channel.send("You don't have sufficient permissions to use this command!");
        }
    }
}

提前致谢! :)

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    在您的情况下,您正在删除包含您的命令的消息。例如:如果是&gt;clear 3,则清除包含您的命令消息的消息。因此,除了命令消息之外,为了删除消息,有 3 种情况。

    第一个是像你说的那样删除 +1 消息。如果您不想为消息数量添加 +1,那么您可以按照 第二 在发送命令消息后立即删除消息。 例如:

    if(args[0]==='clear'){
        message.delete(); //deletes the message that has been sent by user.
        message.channel.bulkDelete(messages);
    }
    

    第三个是您可以使用before 参数,以便在特定消息之前删除消息。

    例如:

    if(args[0]==='clear'){
    message.channel.messages.fetch({ limit: args[0],before:message.id }).then(messages => {
                    message.channel.bulkDelete(messages);
            })
    }
    

    第三种方法是唯一不删除命令而在其他两种方法中删除命令的方法。 此外,如果您使用then() 来解决promise,则不需要使用await。了解有关promise 的更多信息。 超时参数将在即将发布的版本中弃用。所以最好使用setTimeout()方法。

    编辑:就像 Lioness100 所说,除非您打算在 fetch 中使用 before 选项,否则您不需要获取消息,因为 bulkDelelte() 已经获取了消息。

    【讨论】:

    • 另外,除非您使用 before 选项,否则实际获取消息是没有意义的,因为如果您可以直接将然后编号传递到 bulkDelete 会花费很多时间跨度>
    • 是的,你是对的。我的错。让我编辑一下。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    • 2019-08-31
    • 2020-04-29
    相关资源
    最近更新 更多