【问题标题】:"Collection is not defined" when trying to create a discord bot尝试创建不和谐机器人时“未定义集合”
【发布时间】:2021-11-20 17:25:53
【问题描述】:

我是 discord.js 的新手,并且一直在关注一个使用过时版本的 discord.js 的 youtube 教程(链接在这里:https://www.youtube.com/watch?v=22BFybCwSFU&list=PL-Ux9UcyEUaNxn_TbByomT-01UUKHOoD1&index=3&ab_channel=Exane)(这给我带来了一些问题)

他们的代码:

const Discord = require('discord.js') 
const bot = new Discord.Client();
const fs = require("fs")
bot.commands = new Discord.Collection(); 

在最后一行(第 5 行),当我尝试输入 new Discord.Collection();我收到 ReferenceError:Discord 未定义,但当我删除“Discord”时。从这条线上,我得到了 ReferenceError: Collection is not defined。

我根据 cmets 更改了我的代码,人们在使用更新版本的 discord.js 和 node.js 时也遇到了困难,但到目前为止还没有找到解决办法。我的代码将放在下面,如果这是一个愚蠢/明显的修复,我真的很抱歉,非常感谢您的阅读!

const { Client, Intents, DiscordAPIError, CommandInteractionOptionResolver } = require("discord.js");
const bot = new Client({ intents: ["DIRECT_MESSAGES", "GUILD_MESSAGES"] });
const fs = require("fs")
bot.Commands = new Collection();

(我认为我应该使用名称“Discord”定义第一个“const”变量,但是当我尝试这样做时,我遇到了同样的错误)

编辑:在此之前我忘了提到我必须在第一行添加(窃取)代码以允许我在创建新客户端时添加意图,因为在旧版本中您以前不需要添加意图在这里,但是在 discord.js 13.3 版中,如果没有它,我的代码将无法运行

【问题讨论】:

    标签: discord discord.js constants client


    【解决方案1】:

    您告诉我们的 YouTube 视频作者正在使用 Discord.Collection(),因为这是“您说”您想要创建 discord.js 集合的方式。如果您不想使用它而只使用Collection(),则必须先使用const { Collection } = require('discord.js') 定义它

    【讨论】:

      猜你喜欢
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 2019-01-27
      • 2019-10-24
      • 2021-08-19
      • 1970-01-01
      相关资源
      最近更新 更多