Skip to content
Advertisement

Temporary mute command returning error ‘Cannot read property ‘slice’ of undefined’

I am trying to create a temporary mute command, that will unmute the muted user in the given time.

Here’s my code:

const Discord = require("discord.js");
const ms = require("ms");

module.exports = {
    name: 'mute',
    description: 'mute a specific user',
    usage: '[tagged user] [mute time]',
    async execute(message, embed, args) {
        let tomute = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
        if (!tomute) return message.reply("Couldn't find user.");
        const reason = args.slice(1).join(' ');
        if (tomute.hasPermission("MANAGE_MESSAGES")) return message.reply("Can't mute them!");
        const muterole = message.guild.roles.cache.find(muterole => muterole.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.overwritePermissions(muterole, {
                        SEND_MESSAGES: false,
                        ADD_REACTIONS: false
                    });
                });
            } catch (e) {
                console.log(e.stack);
            }
        }
        const mutetime = args.slice(2).join(' ');
        //here is the start of the error 

        await (tomute.roles.add(muterole.id));
        message.reply(`<@${tomute.id}> has been muted for ${ms(ms(mutetime))} `);

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



    }
}

Currently getting the following error: Cannot read property 'slice' of undefined

Do you have any idea on how to fix the command?

edit

this after a year and this is for future people who come here

the issue was here

 async execute(message, embed, args) {

i never passed embed from my main file so args was undefined embed part was where the args should have been, I was stupid at that time and was new to coding, but as I now have some experience, I decided to edit this to show what wrong

Advertisement

Answer

It means that somewhere in your code, you have a value which is undefined and you try to use the string/array function slice on it but undefined does not have this function : so it is a error.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement