Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
Basic discord.js lock command does not work

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!