'How to check if a message author has a role?
I am trying to code a bot for a game idea my friends and I have. The command I'm working on is the score adjuster. Obviously though, I only want moderators to have access to this command.
I have read several threads and tried several methods to check for a role though, and all close the script with this error:
if (message.member.roles.has(modRole))
TypeError: Cannot read properties of null (reading 'roles')
This is the last code I tried:
bot.on("message", (message) => {
let args = message.content.toLowerCase().substring(PREFIX.length).split(" ");
if (args[0] === "adjust" && args[1] !== "help") {
if (message.member.roles.has("modRoleID")) {
// Adjust command here
}
}
});
Why is the bot not able to read .roles()?
Solution 1:[1]
So I never got .roles to work. And I wasn't about to recode the entire bot to match v13, so here's what I did:
I just set an array to contain all the moderator user id's, and then checked if the author was a moderator, like so:
const mods = ['userID_1','userID_2'];
bot.on("message", (message) => {
let args = message.content.toLowerCase().substring(PREFIX.length).split(" ");
if (args[0] === "adjust" && args[1] !== "help") {
if (!mods.includes(message.author.id)) return message.channel.send("You do not have permission to use that command!").then()
//Adjust command here
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | p_nlsn |