javascript - Discord.js 出现“TypeError:Client.Collection 不是构造函数”错误
问题描述
这是我第一次使用 Discord.js,因为我正在尝试为我朋友的服务器创建个人音乐机器人。首先,我正在阅读 CodeLyon 的教程,但我遇到了一个错误,我不知道如何修复它,因为视频是在网关意图添加到 Discord.js 之前发布的
我目前收到的错误状态:
TypeError: Client.Collection is not a constructor at Object.<anonymous> (C:\Users\Jewel Wildmoon\Downloads\My Discord Bots\Music Bots\Bot\main.js:9:19) 在 Module._compile(节点: internal/modules/cjs/loader:1101:14) 在 Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) 在 Module.load (node:internal/modules/cjs/loader :981:32) 在 Function.Module._load (node:internal/modules/cjs/loader:822:12) 在 Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) 在节点:内部/主/run_main_module:17:47
这是我的 main.js 文件中的代码:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = '!';
const fs = require('fs');
client.commands = new Client.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require('./commands/${file}');
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Bot is Online!');
});
client.on('message', message => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(message, args);
} else if(command == 'youtube'){
message.channel.send('LINK');
}
});
client.login('TOKEN');
这是我在 ping.js 文件中的当前代码:
module.exports = {
name: 'ping',
description: "This is a ping command",
execute(message, args){
message.channel.send('pong!');
}
}
不太清楚在这里做什么,我没有使用 Discord.js,也不熟悉网关意图。那我还是 JavaScript 新手。
解决方案
Collection
由库导出,它不是客户端属性。您需要在顶部与Client
和一起解构它Intents
。
const { Client, Collection, Intents } = require('discord.js');
client.commands = new Collection();
推荐阅读
- arrays - 使用 np.where 子集 3d 数组
- groovy - Groovy 多值哈希映射
- mongodb - 获取字符串日期之间的数据 MongoDB
- python - Python Pandas 正则表达式特定字符串
- python - 带有 SQLAlchemy 的 urllib request.urlopen(url).read() 存储的是十六进制字符串而不是 HTML
- drools - 当问题与 .drl 'or' 子句相关时,是什么原因导致 ConstraintMatchTotal 无法添加约束匹配?
- django - 如何为未经授权的用户禁用(几乎)所有表单中的所有字段?
- sql - 在 CATCH 块中记录错误后如何继续执行存储过程
- swift - 有什么方法可以在 Swift 中检测形状轮廓而无需扩展到 OpenCV?
- asp.net-core - 为什么此 Blazor 应用程序中的组件无法呈现?