Search code examples
javascriptdiscorddiscord.jsbots

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


I've made a tempmute command for my discord bot and it almost works. 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). 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. 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.")
        }
    }
}

Solution

  • 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. After removing that short part of code everything is working as intended.