mintea
mintea

Reputation: 63

Discord.js - Guild ID is undefined even though definition is there

Making a discord bot. Everything works fine, except the one command of -!prefix. -! being the prefix, and the command taking the args and changing the prefix of the server.

Before I go into detail about this while thing, here's the bot's code, maybe I simply did something wrong in the consts: Hastebin Link

The error here is in line 52, according to the console: Console Log

I'm confused as to what to do with this being "undefined." I've tried using the message.guild.id property as the variable, and also the one shown on the code. I've tried moving it to multiple places, although the only place it even registers is INSIDE of the prefix command (which is currently broken because of this error.)

Anyone have any fixes? I'm not that much of an expert in the whole JavaScript thing, as I came over from Python and regular Java.

Upvotes: 3

Views: 56899

Answers (2)

Raymond Zhang
Raymond Zhang

Reputation: 730

Your code at line 52 is currently:

var server = bot.guilds.get(message.author).id

You're currently passing a User object into the .get() which should recieve an id or snowflake.

With this in mind, your code should look like:

var server = bot.guilds.get(message.guild.id).id;

However, this is a bit excessive because you can simply shorten it to:

var server = message.guild.id;

Now you said that you've

tried using the message.guild.id property as the variable, and also the one shown on the code.

I'm not sure if you mean what I just suggested, but if so please notify me.

Upvotes: 9

EKW
EKW

Reputation: 2135

According to the discord.js documentation, bot.guilds is indexed by each guild's ID. message.author is a User object, not a guild id. You cannot put a User object into bot.guilds.get and expect it to work; it only accepts guild ids.

To get the server that the message was sent in, just do message.guild.

Upvotes: 3

Related Questions