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

313
Views
How can I define guild(discord.js)

I am trying to make a discord bot that when you type !mute @user it gives them the muted role and if there is no muted role it makes one and adds it to the user. Here is my code: else if (message.content.startsWith(${prefix}mute)) {

let role = message.guild.roles.cache.find(x => x.name == 'mute');
if(!role) {
    guild.roles.create({
        data: {
          name: 'mute haha',
          color: 'GRAY',
        },
        reason: 'you probably spammed',
      })
        .then(console.log)
        .catch(console.error);
    }
const user = message.mentions.users.first();
if (user) {
    const member = message.guild.members.resolve(user);
    if (member) {
        let mute = message.guild.roles.cache.find(role => role.name === "mute");
        member.addRole(mute);
    } else {
        message.channel.send("That user isn't in this server");
    }
} else {
    message.channel.send("You didn't mention the user to kick");
}

and this is the error that i get when i try to run it:

C:\Users\COOKIE\Desktop\bot\main.js:157
                        guild.roles.create({
                              ^

TypeError: Cannot read properties of undefined (reading 'roles')
    at Client.<anonymous> (C:\Users\COOKIE\Desktop\bot\main.js:157:10)
    at Client.emit (node:events:390:28)
    at MessageCreateAction.handle (C:\Users\COOKIE\Desktop\bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:18)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\COOKIE\Desktop\bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\COOKIE\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:350:31)
    at WebSocketShard.onPacket (C:\Users\COOKIE\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)
    at WebSocketShard.onMessage (C:\Users\COOKIE\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10)
    at WebSocket.onMessage (C:\Users\COOKIE\Desktop\bot\node_modules\ws\lib\event-target.js:199:18)
    at WebSocket.emit (node:events:390:28)
    at Receiver.receiverOnMessage (C:\Users\COOKIE\Desktop\bot\node_modules\ws\lib\websocket.js:1022:20)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The error states that guild is not defined, that is because guild is actually a property of the message object, so you should be doing

message.guild.roles.create()

And make sure you check that the command is being run in a guild and not inside the user's DM, otherwise guild will be undefined

if (!message.guild) return console.log('command is being run outside a server');
message.guild.roles.create(/* do ur stuff*/);
// rest of the code
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!