最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Jan 31, 2018 at 22:03 mintea asked Jan 16, 2018 at 2:14 minteamintea 631 gold badge2 silver badges7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 9

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.

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.

发布评论

评论列表(0)

  1. 暂无评论