This is my first Discord bot, so please bear with me if this is dumb!
I've been trying to find a way to get a role, either by name or by ID. Most tutorials that I've found are in line with:
// get role by ID
let myRole = message.guild.roles.get('264410914592129025');
// get role by name
let myRole = message.guild.roles.find((role) => role.name === 'Moderators');
Source: https://anidiots.guide/understanding/roles
Yet I keep getting a ReferenceError: message is not defined
error.
Here is my code:
const token = '';
const prefix = '!!';
let role = message.guild.roles.find((role) => role.name === 'among us');
My goal is to just actually take the "among us" role and mute the mics for everyone with that role.
//prefix manager
bot.on('message', (message) => {
//exit and stop if no pref
if (!message.content.startsWith(prefix)) return;
var args = message.content.substring(prefix.length).split(' ');
//if !! is used
switch (args[0]) {
case 'bing':
message.reply('bong');
break;
case 'HEY':
message.reply(boulderHello[Math.floor(Math.random() * boulderHello.length)]);
break;
case 'muteall':
role.voice.setMute(true);
break;
}
});
Okay so if something is not defined just define it like so:
const token = '';
const prefix = "!!";
//if it says: can't read property of bot of undefined use client.on instead
bot.on("message", (message) => {
let role = message.guild.roles.find(role => role.name === "among us");
//your code here
})
The Message
is not defined by default. It is defined within the message
event of Client
as the first parameter.
Here's an example of how to get the role within the message
event:
client.on("message", message => {
const Role = message.guild.roles.cache.find(role => role.name == "among us");
// You can also get the role by ID like this:
const Role = message.guild.roles.cache.get("RoleID");
});
You need to put message
in a message
event.
bot.on(`message`, message => {
//here you can have your code
});
As for finding the role, it's been updated in v12, so you need to do:
let role = message.guild.roles.cache.get(`id`)
And to get it by name:
let role = message.guild.roles.cache.get(r => r.name === `name`)
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.