javascript Discord.js - 即使有定义,公会 ID 也未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48273185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:56:10  来源:igfitidea点击:

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

javascriptdiscorddiscord.js

提问by Zetari

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.

制作一个不和谐的机器人。一切正常,除了-!prefix. -!作为前缀,命令采用 args 并更改服务器的前缀。

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

在我详细讨论这个 while 之前,这是机器人的代码,也许我只是在常量中做错了:Hastebin Link

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

根据控制台,这里的错误在第 52 行:Console Log

I'm confused as to what to do with this being "undefined." I've tried using the message.guild.idproperty 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.)

我对如何处理“未定义”感到困惑。我试过使用该message.guild.id属性作为变量,以及代码中显示的那个。我试过将它移到多个地方,尽管它唯一注册的地方是前缀命令的 INSIDE(由于此错误,它目前已损坏。)

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.

有人有任何修复吗?我不是整个 JavaScript 方面的专家,因为我是从 Python 和常规 Java 过来的。

回答by Raymond Zhang

Your code at line 52is currently:

您的代码line 52目前是:

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

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

您当前正在将一个User对象传递给.get()应该接收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.idproperty as the variable, and also the one shown on the code.

尝试使用该message.guild.id属性作为变量,以及代码中显示的那个。

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

我不确定你的意思是否是我刚刚建议的,但如果是这样,请通知我。

回答by EKW

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

根据discord.js 文档bot.guilds由每个公会的 ID 索引。message.author是一个User对象,而不是公会ID。您不能将 User 对象放入bot.guilds.get并期望它工作;它只接受公会ID。

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

要获取发送消息的服务器,只需执行message.guild.