Home > other >  Cannot read properties of undefined (reading 'id') when trying to mute a member
Cannot read properties of undefined (reading 'id') when trying to mute a member

Time:05-24

i'm wrting a code to mute members in my server so,

i get an error saying "Cannot read properties of undefined (reading 'id')" when i try to mute a member

code:

const ms = require('ms');

module.exports = {
    name: "mute",
    description: "this command mutes a member",
    execute(message, args) {
        const target = message.mentions.users.first();
        if (target) {
            let mainRole = message.guild.roles.cache.find('941742955910275153');
            let muteRole = message.guild.roles.cache.find(role => role.name === 'Muted');

            let memberTarget = message.guild.members.cache.get(target.id);

            if (!args[1]) {
                // memberTarget.roles.remove(mainRole.id)
                memberTarget.roles.add(muteRole.id);
                message.channel.send(`<@${memberTarget.user.id}> has been muted`);
                return
            }

            // memberTarget.roles.remove(mainRole.id)
            memberTarget.roles.add(muteRole.id);
            message.channel.send(`<@${memberTarget.user.id}> has been muted for ${ms(ms(args[1]))}`);

            setTimeout(function () {
                // memberTarget.roles.remove(mainRole.id)
                memberTarget.roles.remove(muteRole.id)
                message.channel.send(`<@${memberTarget.user.id}> has been muted`);
            }, ms(args[1]));
        } else {
            message.channel.send("can't find the user!");
        }
    }
}

CodePudding user response:

const ms = require('ms');

const mute = {
name: 'mute',
description: 'this command mutes a member',
execute(message, args) {
    const target = message.mentions.users.first();
    if (target) {
        const mainRole = message.guild.roles.cache.find('941742955910275153');
        const muteRole = message.guild.roles.cache.find(
            (role) => role.name === 'Muted',
        );

        const memberTarget = message.guild.members.cache.get(target.id);

        if (!args[1]) {
            // memberTarget.roles.remove(mainRole.id)
            memberTarget.roles.add(muteRole.id);
            message.channel.send(`<@${memberTarget.user.id}> has been muted`);
            return;
        }

        // memberTarget.roles.remove(mainRole.id)
        memberTarget.roles.add(muteRole.id);
        message.channel.send(
            `<@${memberTarget.user.id}> has been muted for ${ms(ms(args[1]))}`,
        );

        setTimeout(function () {
            // memberTarget.roles.remove(mainRole.id)
            memberTarget.roles.remove(muteRole.id);
            message.channel.send(`<@${memberTarget.user.id}> has been muted`);
        }, ms(args[1]));
    } else {
        message.channel.send("can't find the user!");
    }
},
};

module.exports = mute;

CodePudding user response:

try to read your code carefully!

You should define muteRole, and the code should work now!

Additionally, it's always good to check with console.log in webpage!

  • Related