Discord.JS-Command Discord Bot 中的类型错误
TypeError in Discord.JS-Commando Discord Bot
好的,所以我在 discord.js-commando
中内置了一个机器人。我一直在对其进行一些调整,主要是尝试添加更多功能,例如按名称将命令记录到频道以及添加 console.error 日志记录。
现在我尝试获取频道日志记录的尝试在命令中可见,但是机器人不断抛出 TypeError...。我不知道是什么导致了这个错误,也不知道如何修复它。
有人可以看看我的代码并帮助我找出导致 TypeError 的原因吗?
这是我不断收到的错误:
console.error
2020-09-30T23:08:24.272196+00:00 app[worker.1]: TypeError: this.commands.filterArray is not a function
2020-09-30T23:08:24.272208+00:00 app[worker.1]: at CommandRegistry.findCommands (/app/node_modules/discord.js-commando/src/registry.js:438:41)
2020-09-30T23:08:24.272208+00:00 app[worker.1]: at CommandDispatcher.matchDefault (/app/node_modules/discord.js-commando/src/dispatcher.js:254:34)
2020-09-30T23:08:24.272209+00:00 app[worker.1]: at CommandDispatcher.parseMessage (/app/node_modules/discord.js-commando/src/dispatcher.js:238:21)
2020-09-30T23:08:24.272209+00:00 app[worker.1]: at CommandDispatcher.handleMessage (/app/node_modules/discord.js-commando/src/dispatcher.js:114:18)
2020-09-30T23:08:24.272210+00:00 app[worker.1]: at CommandoClient.<anonymous> (/app/node_modules/discord.js-commando/src/client.js:68:51)
2020-09-30T23:08:24.272210+00:00 app[worker.1]: at CommandoClient.emit (events.js:315:20)
2020-09-30T23:08:24.272211+00:00 app[worker.1]: at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
2020-09-30T23:08:24.272212+00:00 app[worker.1]: at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
2020-09-30T23:08:24.272213+00:00 app[worker.1]: at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
2020-09-30T23:08:24.272213+00:00 app[worker.1]: at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
这是我的主要机器人文件:
index.js
const { Client } = require('discord.js-commando');
const { RichEmbed } = require('discord.js')
const path = require('path');
const prefix = (process.env.BOT_PREFIX);
const twitch_url = (process.env.TWITCH_URL);
const embed_color = (process.env.EMBED_COLOR);
const embed_img = (process.env.EMBED_IMG);
const embed_url = (process.env.EMBED_URL);
const avatar_url = (process.env.AVATAR_URL);
const thumbnail_url = (process.env.THUMBNAIL_URL);
const welcome_channel = (process.env.WELCOME_CHANNEL_NAME);
const member_role = (process.env._MEMBER_ROLE_NAME);
require('dotenv').config();
const client = new Client({
commandPrefix: prefix
})
client.registry
.registerDefaultTypes()
.registerGroups([
['admin', 'Administration'],
['mod', 'Moderation'],
['fun', 'Fun'],
['misc', 'Miscellanious'],
['util', 'Utility']
])
.registerDefaultGroups()
.registerDefaultCommands()
.registerCommandsIn(path.join(__dirname, 'commands'))
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}(${client.user.id})`)
client.user.setActivity(`${prefix}help`, {
type: "STREAMING",
url: twitch_url
})
});
client.on("guildMemberAdd", (member) => {
console.log(`New User "${member.user.username}" has joined "${member.guild.name}"`);
guildMember.addRole(guildMember.guild.roles.find(role => role.name === member_role));
let channel = member.guild.channels.find(c => c.name === welcome_channel);
const embed = new RichEmbed()
.setAuthor("Yuzuki Yukari", avatar_url)
.setUrl(embed_url)
.setColor(embed_color)
.setImage(embed_img)
.setThumbnail(thumbnail_url)
.setFooter("Yuzuki Yukari", avatar_url)
.addField("Welcome", `${member.user.username} has joined ${member.guild.name}`)
return channel.send(embed).catch(console.error);
});
client.on('guildMemberRemove', (member) => {
let channel = member.guild.channels.find(c => c.name === welcome_channel);
const embed = new RichEmbed()
.setAuthor("Yuzuki Yukari", avatar_url)
.setUrl(embed_url)
.setColor(embed_color)
.setImage(embed_img)
.setThumbnail(thumbnail_url)
.setFooter("Yuzuki Yukari", avatar_url)
.addField("Goodbye", `${member.user.username} has left ${member.guild.name}`)
return channel.send(embed).catch(console.error);
});
client.on('error', console.error)
client.login(process.env.BOT_TOKEN);
这是我的 package.json 文件:
package.json
{
"name": "yuzuki",
"version": "1.0.0",
"description": "A Discord Moderation Bot built using Discord.js commando",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
"author": "Nimbi",
"license": "ISC",
"dependencies": {
"discord.js": "^12.0.1",
"discord.js-commando": "^0.10.0",
"path": "^0.12.7",
"snekfetch": "^4.0.4",
"sqlite": "^4.0.14",
"uws": "^100.0.1",
"nodemon": "^1.13.3",
"dotenv": "^8.2.0"
},
"engines": {
"node": "^12.0.0"
}
}
我的依赖项及其版本可以在我的 package.json
中看到,但是我的 node.js 版本是 ^12.0.0
,我的 discord.js 版本是 ^12.0.1
,如果有帮助的话,我的 discord.js-commando 版本是 ^0.10.0
。
discord.js-commando@0.10.0
与 discord.js@12
不兼容。它的package.json
指定必须使用Discord.js^11.2.0
错误是因为 Collection#filterArray
在 v12 中被删除,因此 this.commands.filterArray
是 undefined
而不是 Commando 代码中的函数。
您有 2 个选择:
- 降级到 Discord.js v11.
- 使用
discord.js-commando
的 master
分支,它与 Discord.js v12 兼容。但是,这个分支可能不稳定 and/or 有错误。
要使用它,请将 package.json
中的 "discord.js-commando": "^0.10.0"
更改为 "discord.js-commando": "discordjs/Commando"
。有关详细信息,请参阅 npm package.json
文档中的 GitHub URLs。
好的,所以我在 discord.js-commando
中内置了一个机器人。我一直在对其进行一些调整,主要是尝试添加更多功能,例如按名称将命令记录到频道以及添加 console.error 日志记录。
现在我尝试获取频道日志记录的尝试在命令中可见,但是机器人不断抛出 TypeError...。我不知道是什么导致了这个错误,也不知道如何修复它。
有人可以看看我的代码并帮助我找出导致 TypeError 的原因吗?
这是我不断收到的错误:
console.error
2020-09-30T23:08:24.272196+00:00 app[worker.1]: TypeError: this.commands.filterArray is not a function
2020-09-30T23:08:24.272208+00:00 app[worker.1]: at CommandRegistry.findCommands (/app/node_modules/discord.js-commando/src/registry.js:438:41)
2020-09-30T23:08:24.272208+00:00 app[worker.1]: at CommandDispatcher.matchDefault (/app/node_modules/discord.js-commando/src/dispatcher.js:254:34)
2020-09-30T23:08:24.272209+00:00 app[worker.1]: at CommandDispatcher.parseMessage (/app/node_modules/discord.js-commando/src/dispatcher.js:238:21)
2020-09-30T23:08:24.272209+00:00 app[worker.1]: at CommandDispatcher.handleMessage (/app/node_modules/discord.js-commando/src/dispatcher.js:114:18)
2020-09-30T23:08:24.272210+00:00 app[worker.1]: at CommandoClient.<anonymous> (/app/node_modules/discord.js-commando/src/client.js:68:51)
2020-09-30T23:08:24.272210+00:00 app[worker.1]: at CommandoClient.emit (events.js:315:20)
2020-09-30T23:08:24.272211+00:00 app[worker.1]: at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
2020-09-30T23:08:24.272212+00:00 app[worker.1]: at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
2020-09-30T23:08:24.272213+00:00 app[worker.1]: at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
2020-09-30T23:08:24.272213+00:00 app[worker.1]: at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
这是我的主要机器人文件:
index.js
const { Client } = require('discord.js-commando');
const { RichEmbed } = require('discord.js')
const path = require('path');
const prefix = (process.env.BOT_PREFIX);
const twitch_url = (process.env.TWITCH_URL);
const embed_color = (process.env.EMBED_COLOR);
const embed_img = (process.env.EMBED_IMG);
const embed_url = (process.env.EMBED_URL);
const avatar_url = (process.env.AVATAR_URL);
const thumbnail_url = (process.env.THUMBNAIL_URL);
const welcome_channel = (process.env.WELCOME_CHANNEL_NAME);
const member_role = (process.env._MEMBER_ROLE_NAME);
require('dotenv').config();
const client = new Client({
commandPrefix: prefix
})
client.registry
.registerDefaultTypes()
.registerGroups([
['admin', 'Administration'],
['mod', 'Moderation'],
['fun', 'Fun'],
['misc', 'Miscellanious'],
['util', 'Utility']
])
.registerDefaultGroups()
.registerDefaultCommands()
.registerCommandsIn(path.join(__dirname, 'commands'))
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}(${client.user.id})`)
client.user.setActivity(`${prefix}help`, {
type: "STREAMING",
url: twitch_url
})
});
client.on("guildMemberAdd", (member) => {
console.log(`New User "${member.user.username}" has joined "${member.guild.name}"`);
guildMember.addRole(guildMember.guild.roles.find(role => role.name === member_role));
let channel = member.guild.channels.find(c => c.name === welcome_channel);
const embed = new RichEmbed()
.setAuthor("Yuzuki Yukari", avatar_url)
.setUrl(embed_url)
.setColor(embed_color)
.setImage(embed_img)
.setThumbnail(thumbnail_url)
.setFooter("Yuzuki Yukari", avatar_url)
.addField("Welcome", `${member.user.username} has joined ${member.guild.name}`)
return channel.send(embed).catch(console.error);
});
client.on('guildMemberRemove', (member) => {
let channel = member.guild.channels.find(c => c.name === welcome_channel);
const embed = new RichEmbed()
.setAuthor("Yuzuki Yukari", avatar_url)
.setUrl(embed_url)
.setColor(embed_color)
.setImage(embed_img)
.setThumbnail(thumbnail_url)
.setFooter("Yuzuki Yukari", avatar_url)
.addField("Goodbye", `${member.user.username} has left ${member.guild.name}`)
return channel.send(embed).catch(console.error);
});
client.on('error', console.error)
client.login(process.env.BOT_TOKEN);
这是我的 package.json 文件:
package.json
{
"name": "yuzuki",
"version": "1.0.0",
"description": "A Discord Moderation Bot built using Discord.js commando",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
"author": "Nimbi",
"license": "ISC",
"dependencies": {
"discord.js": "^12.0.1",
"discord.js-commando": "^0.10.0",
"path": "^0.12.7",
"snekfetch": "^4.0.4",
"sqlite": "^4.0.14",
"uws": "^100.0.1",
"nodemon": "^1.13.3",
"dotenv": "^8.2.0"
},
"engines": {
"node": "^12.0.0"
}
}
我的依赖项及其版本可以在我的 package.json
中看到,但是我的 node.js 版本是 ^12.0.0
,我的 discord.js 版本是 ^12.0.1
,如果有帮助的话,我的 discord.js-commando 版本是 ^0.10.0
。
discord.js-commando@0.10.0
与 discord.js@12
不兼容。它的package.json
指定必须使用Discord.js^11.2.0
错误是因为 Collection#filterArray
在 v12 中被删除,因此 this.commands.filterArray
是 undefined
而不是 Commando 代码中的函数。
您有 2 个选择:
- 降级到 Discord.js v11.
- 使用
discord.js-commando
的master
分支,它与 Discord.js v12 兼容。但是,这个分支可能不稳定 and/or 有错误。
要使用它,请将package.json
中的"discord.js-commando": "^0.10.0"
更改为"discord.js-commando": "discordjs/Commando"
。有关详细信息,请参阅 npmpackage.json
文档中的 GitHub URLs。