Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

discord.js - "Cannot read property 'cache' of undefined" when I check a member role

I want to make a mute command for my Discord bot but that create errors:

C:Program Files
odejs
ode.exe .index.js
|------ Bot on ------|
index.js:22
Uncaught TypeError: Cannot read property 'cache' of undefined
No debugger available, can not send 'variables'
Process exited with code 1

I want to check if the user who is mentionned has already the mute role and if the executor has an admin role. But that create this error.

My code:


bot.on("message", async message => {
    if(message.content.startsWith(prefix + "mute")){
        let User = message.mentions.users.first();
        let time = message.content.split(" ").slice(2)
        let reason = message.content.split(" ").slice(3)
        if(!reason){ let reason = "aucune"}      
        if(!time || !User) return message.reply("Veuillez entrer une commande valide !
" + prefix + "mute @user <temps> <raison>")
        
        let dUser = User.id
        if(dUser == message.author.id) return message.reply("Vous ne pouvez pas vous mute vous même !")
    

        if(isNaN(time[0]) || time < 1) return message.reply("Veuillez entrer une valeur chiffrée et supérieur à 1 !")
        let muterole = "793840735266013205"
che
        if(User.roles.cache.has(muterole)) return message.reply("Ce membre est déjà mute !")
        if(!message.author.roles.cache.has("783758067111428126" || "783758066138218577")) return message.reply("Vous n'avez pas la permission d'utiliser cette commande !")
        if(User.roles.cache.has("783758067111428126" || "783758066138218577")) return message.reply("Vous ne pouvez pas mute un membre du staff !")

        let emb = new Discord.MessageEmbed()
        .setTitle(Mute)
        .setDescription(User.username + " a bien été mute par " + message.author.username + " pendant " + time[0] + " secondes pour la raison suivante : " + reason)
        .setColor("#E74C3C")
pendant " + time[0] + " secondes pour la raison suivante : " + reason)
        User.roles.add(muterole)

        setTimeout(() => {
            User.roles.remove(muterole)
            let reply = new Discord.MessageEmbed()
            .setDescription(User + " a bien été unmute !")
            .setColor("#E74C3C")
            message.guild.channels.cache.get("795063422386569298").send(reply)

            let mp = new Discord.MessageEmbed()
            .setDescription("Vous avez été unmute de " + guild)
            .setColor("#E74C3C")
            message.author.send(mp)



        }, time[0] = 60000
        )}
})

Don’t worry about French words.

question from:https://stackoverflow.com/questions/65643980/cannot-read-property-cache-of-undefined-when-i-check-a-member-role

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code is not going to do what you want it to do, because you messed up some parts. User will be the first mentioned user that can be found in your arguments. So if you mention the user right at the first position of your arguments, it will be at index 0. That is because the arguments get stored in an array and arrays always starts at index 0. That means now your following arguments have to be at index 1 and 2. So you can change your time and reason into:

let time = message.content.split(" ").slice(1);
let reason = message.content.split(" ").slice(2).join(" ");

Make sure you use .join(" ") at your reason, that will allow you to add multiple words for the reason. The next mistake is in the if-statement where you ask if there is no reason. You create a new variable inside the statement, which makes no sense. You just have to do:

if(!reason){ reason = "aucune"; } 

Now if there is no reason provided the reason will be aucune.

If you want to ask if a user has the mute role already, you can use a GuildMember Object. That would look like this:

if(message.guild.member(User).roles.cache.has(muterole)) return message.reply("Ce membre est déjà mute !")

After that if-statement you ask if a user has certain roles and if he don't has this roles, he has no permission. Something like that should always be the first line of code of such a command and it should look like this:

if(!message.author.roles.cache.has("783758067111428126") || !message.author.roles.cache.has("783758066138218577")) return message.reply("Vous n'avez pas la permission d'utiliser cette commande !")

The same procedure with the following if-statement:

if(User.roles.cache.has("783758067111428126") || User.roles.cache.has("783758066138218577")) return message.reply("Vous ne pouvez pas mute un membre du staff !")

Then in your embed you are using time[0], although time is not an array. It just has to be time.

Your code should look like this now:

bot.on("message", async message => {
    if(message.content.startsWith(prefix + "mute")){
        if(!message.author.roles.cache.has("783758067111428126") || !message.author.roles.cache.has("783758066138218577")) return message.reply("Vous n'avez pas la permission d'utiliser cette commande !")
        let User = message.mentions.users.first();
        if(User.roles.cache.has("783758067111428126") || User.roles.cache.has("783758066138218577")) return message.reply("Vous ne pouvez pas mute un membre du staff !")
        let time = message.content.split(" ").slice(2)
        let reason = message.content.split(" ").slice(3)
        if(!reason){ reason = "aucune"; }      
        if(!time || !User) return message.reply("Veuillez entrer une commande valide !
" + prefix + "mute @user <temps> <raison>")
        
        let dUser = User.id
        if(dUser == message.author.id) return message.reply("Vous ne pouvez pas vous mute vous même !")
    

        if(isNaN(time) || time < 1) return message.reply("Veuillez entrer une valeur chiffrée et supérieur à 1 !")
        let muterole = "793840735266013205"

        if(message.guild.member(User).roles.cache.has(muterole)) return message.reply("Ce membre est déjà mute !")

        let emb = new Discord.MessageEmbed()
        .setTitle(Mute)
        .setDescription(User.username + " a bien été mute par " + message.author.username + " pendant " + time + " secondes pour la raison suivante : " + reason)
        .setColor("#E74C3C")

        User.roles.add(muterole)

        setTimeout(() => {
            User.roles.remove(muterole)
            let reply = new Discord.MessageEmbed()
            .setDescription(User + " a bien été unmute !")
            .setColor("#E74C3C")
            message.guild.channels.cache.get("795063422386569298").send(reply)

            let mp = new Discord.MessageEmbed()
            .setDescription("Vous avez été unmute de " + message.guild)
            .setColor("#E74C3C")
            message.author.send(mp)



        }, time
        )}
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...