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

2022-01-10 00:00:00 discord javascript discord.js

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

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.

在详细介绍这件事之前,这是机器人的代码,也许我只是在 consts 中做错了什么:Hastebin 链接

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

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

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

我对如何处理未定义"感到困惑.我尝试使用 message.guild.id 属性作为变量,以及代码中显示的变量.我试过把它移到多个地方,虽然它唯一注册的地方是前缀命令的内部(由于这个错误,它目前被破坏了.)

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.)

任何人有任何修复?我不是整个 JavaScript 方面的专家,因为我是从 Python 和普通 Java 过来的.

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.

推荐答案

您在 line 52 的代码目前是:

Your code at line 52 is currently:

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

您当前正在将 User 对象传递到 .get() 中,该对象应该会收到 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;

现在你说你已经

尝试使用 message.guild.id 属性作为变量,也是代码中显示的变量.

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.

相关文章