I was trying to make a basic lock command for my discord.js bot. I gave it the code for the command, made it only usable by users with a specific role, and lastly added the code for the lock itself.
// lock channel
const role = guild.roles.find("name", "👤︱Member");
client.on('message', msg => {
if (msg.content === '.lock') {
msg.channel.send('Successfully locked the channel.');
if(!message.member.roles.some(r=>["👮︱Moderator", "🔱 ︱Trial Administrator"].includes(r.name))) return message.channel.send(`You dont have the moderator role!`)
message.channel.overwritePermissions(role,{ 'SEND_MESSAGES': false })
}
});
If I started my bot, it would automatically shut down with this error message:
ReferenceError: guild is not defined
I have discord.js v12 if that helps. If the answer is that my discord.js version is outdated, then can you at least tell me any advice on the code too?
As far as I know, you need to create guild first, using something like:
const guild = client.guilds.cache.get("YOUR_GUILD_ID");
And than, you need to use it:
client.on('message', msg => {
let guild = message.guild;
if (msg.content === '.lock') {
msg.channel.send('Successfully locked the channel.');
if(!message.member.roles.some(r=>["👮︱Moderator", "🔱 ︱Trial Administrator"].includes(r.name))) return message.channel.send(`You dont have the moderator role!`)
message.channel.overwritePermissions(role,{ 'SEND_MESSAGES': false })
}
});
P.S. Don't have access to IDE right now, so don't know if it's work
Also, the better way to let roles use command is:
let allowedRole = message.guild.roles.find("name", "Staff");
if (message.member.roles.has(allowedRole.id) {
// allowed access to command
} else { //aka members who arent staff
// not allowed access
}