error: val is not a non-empty string or a valid number. val=undefined

Viewed 1878

I'm making a mute command but I got this error

error: val is not a non-empty string or a valid number. val=undefined

I don't quite know why I got that error and I don't know how to fix it Here is my code:

bot.on('message', async (bot, message, args) => {
    if (!message.content.startsWith(PREFIX)) return;
    if (message.author.bot) return;
    switch (args[0]) {
        case 'mute':
            let toMute = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
            if (!toMute) return message.reply('It looks like you didnt specify the user!');
            if (toMute.hasPermission('MANAGE_MESSAGES')) return message.reply("can't mute them");
            let muterole = message.guild.roles.cache.find(r => r.name === 'muted');
            if (!muterole) {
                try {
                    muterole = await message.guild.roles.create({
                        name: "muted",
                        color: "#000000",
                        permissions: []
                    })
                    message.guild.channels.cache.forEach(async (channel, id) => {
                        await channel.overwritePermission(muterole, {
                            SEND_MESSAGES: false,
                            ADD_REACTIONS: false
                        });
                    });
                } catch (e) {
                    console.log(e.stack);
                }
            } return message.channel.send('Cant')


            let mutetime = args[1];
            if (!mutetime) return message.reply('You didnt specify the time');

            await (toMute.addRole(muterole.id));
            message.reply(`Successfully muted <@${toMute.id}> for ${ms(mutetime)}`);

            setTimeout(function () {
                toMute.removeRole(muterole.id);
                message.channel.send(`<@${toMute.id}> has been unmuted!`);
            }, ms(mutetime));

    }
});

If you can help me that will be great. Also, I am new to coding so it would be great if you could teach me in baby steps Thank you.

1 Answers

So i am gonna answer this on how I did my timed mute command. It is kinda different to yours but still does the same job. An example of this command being used is !mute @test#6969 10m For swearing

        const target = message.mentions.members.first();
        const tag = message.member.user.tag;
        const targettag = target.user.tag;
        const author = message.author;
        args[2] = args.splice(2, args.length).join(" ");

        const modlogEmbed = {
    
            color: 0xFF5733,
            title: 'MUTE ||',  
            description: 'Muted Person : ' + targettag + '<@'+ target + '>' + "\n" + 'Reason : ' + args[2] + "\n" + 'Moderator : ' + tag + '<@' + author.id + '>' + "\n" + 'Time : ' + ms(ms(args[1])),
            timestamp: new Date(),
            footer: {
                text: 'Lonely Guy is the best',
                },
            };

            const unmuteEmbed = {
    
                color: 0xA6D785,
                title: 'UNMUTE ||',  
                description: 'Unmuted Person : ' + targettag + '<@'+ target + '>' + "\n" + 'Moderator : ' + 'Respectrum#7228' + '<@785234591568887829>',
                timestamp: new Date(),
                footer: {
                    text: 'Lonely Guy is the best',
                    },
                };    

            const modlog = message.guild.channels.cache.find(channel => channel.id === 'your modlog id');

        if(target){
            if(message.member.permissions.has('ADMINISTRATOR')){

                target.roles.add('the mute role id');
                message.reply('U have muted <@' + target + '> for '+ ms(ms(args[1])) + '. Personally, thats an excellent idea.');
                modlog.send({ embed: modlogEmbed});

                setTimeout(function() {
                target.roles.remove('the mute role id');
                modlog.send({ embed: unmuteEmbed});
                }, ms(args[1]));


            }
            else{
                 
                message.reply('you don`t have the facilities for that, big man');
            }
        }
        else{
            message.reply('Ping the member you want to mute.');
        }
        

If you want know what each does just ask here.

Related