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

僤鯓⒐⒋嵵緔 提交于 2019-12-11 15:21:00

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/48273185/discord-js-guild-id-is-undefined-even-though-definition-is-there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!