简体   繁体   中英

TypeError: Cannot read property 'roles' of undefined || Discord.js

I'm trying to make a mute command for my discord bot but when I run:

let mutedRole = message.guild.roles.cache.find(r => r.name === 'Muted');

I get an error in the console saying:

TypeError: Cannot read property 'roles' of undefined

as if message.guild is undefined, the command works but I get this error in the console. If anyone knows how to get rid of this error any help is appreciated.

lmao I finally found the issue and it was something completely unrelated to the mute command, when the mute command is executed it dm's the user that they have been muted and that calls the client.on("channelCreate"), which I was not checking for if it was dm's so it was trying to get the guild.roles attribute of a dm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM