简体   繁体   English

Discord.js v13 为什么我的 tempmute 命令不起作用?

[英]Discord.js v13 why is my tempmute command not working?

I've made a tempmute command for my discord bot and it almost works.我为我的 discord 机器人做了一个临时静音命令,它几乎可以工作。 It has quite a few foolproofing measures like preventing the bot from muting themselves, not working if the time isn't specified and such.它有很多万无一失的措施,比如防止机器人自己静音,如果没有指定时间就不工作等等。 I am using the npm ms package to deal with the mute duration ( https://www.npmjs.com/package/ms ).我正在使用 npm ms package 来处理静音持续时间 ( https://www.npmjs.com/package/ms )。 when instead of specifying an amount of time I type in gibberish it works as intended and replies with the correct message.当我输入乱码而不是指定时间时,它会按预期工作并回复正确的消息。 The problem is that when I type in a 100% correct command it responds asthough I didn't specify the time correctly instead of muting the user for that amount of time.问题是,当我输入 100% 正确的命令时,它的响应就像我没有正确指定时间一样,而不是让用户在这段时间内静音。 here's how it looks .这是它的样子 Any ideas as to why that is?关于为什么会这样的任何想法? My code is here:我的代码在这里:

const ms = require('ms');
const { Permissions, MessageActionRow, UserFlags } = require('discord.js');

module.exports = {
    name: 'tempmute',
    description: "Temporarily mutes a user",
    execute(message, args)
    {
        const target = message.mentions.members.first();
        let muteRole = message.guild.roles.cache.find(role => role.name === "muted");
        if(message.member.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS))
        {
            if(target)
            {
                let memberTarget = message.guild.members.cache.get(target.id);
                if(target.id == 'myBotsID')
                {
                    message.reply("I can't mute myself.")
                }
                else if(message.member == target)
                {
                    message.reply("You can't mute yourself!")
                }
                else if(memberTarget.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS))
                {
                    message.channel.send(`<@${memberTarget.user.id}> has been muted for ${ms(ms(args[1]))}`);                
                }
                else 
                {
                    if(!args[1]) 
                    {
                        return message.reply("Time not specified.")
                    }
                    else
                    {
                        let time = ms(args[1])
                        memberTarget.roles.add(muteRole.id); 
                        try {
                            message.reply("<@" + memberTarget.user.id + ">" + "has been muted for " + ms(ms(args[1])).catch(console.error))
                            setTimeout(function () {
                                memberTarget.roles.remove(muteRole.id);
                            }, time);
                        } catch (err) {
                            message.reply("Can't transform that into milliseconds `"+args[1]+"`")
                            return
                        }
                    }
                }
            }
            else
            {
                message.reply("You have to mention a valid member of this server.")
            }
        }
        else
        {
            message.reply("You can't use that.")
        }
    }
}

Okay so I figured it out.好的,所以我想通了。 Here's the problematic code:这是有问题的代码:

try {
    message.reply("<@" + memberTarget.user.id + ">" + "has been muted for " + ms(ms(args[1])).catch(console.error))
    setTimeout(function () {
        memberTarget.roles.remove(muteRole.id);
    }, time);
} catch (err) {
    message.reply("Can't transform that into milliseconds `"+args[1]+"`")
    return
}

IDK why but the ".catch(console.error))" (which is a leftover code and shouldn't be there in the first place) caused it to behave differently aka treat everything as an incorrectly specified time and returned the corresponding message instead of muting the member for the specified amount of time. IDK 为什么但是“.catch(console.error))”(这是一个剩余的代码,一开始就不应该存在)导致它的行为不同,也就是将所有内容都视为错误指定的时间并返回相应的消息在指定的时间内将成员静音。 After removing that short part of code everything is working as intended.删除那一小段代码后,一切都按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM