Search code examples
node.jsdiscord.jscommando

TypeError in Discord.JS-Commando Discord Bot


Ok, so I have a bot built in discord.js-commando. I';ve been doing some tweaking to it, mainly trying to add some more functionality, such as logging commands and such to a channel by name as well as adding console.error logging.

Now my attempts to get channel logging are visible in the commands, however the bot keeps throwing a TypeError.... I have no idea what is causing this error or for that matter how to fix it.

Can someone please take a look at my code and help me figure out what is causing the TypeError?

This is the error I keep getting:

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)

Here is my main bot file:

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

Here is my package.json file:

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"
  }
}

My dependencies and their versions can be seen in my package.json, however my node.js version is ^12.0.0, my discord.js version is ^12.0.1, and my discord.js-commando version is ^0.10.0 if that helps.


Solution

  • discord.js-commando@0.10.0 isn't compatible with discord.js@12. It's package.json specifies that Discord.js ^11.2.0 must be used.

    The error is because Collection#filterArray was removed in v12, and therefore this.commands.filterArray is undefined and not a function in the Commando code.

    You have 2 options:

    1. Downgrade to Discord.js v11.
    2. Use the master branch of discord.js-commando, which is compatible with Discord.js v12. However, this branch may be unstable and/or have bugs.
      To use this, change "discord.js-commando": "^0.10.0" in your package.json to "discord.js-commando": "discordjs/Commando". See GitHub URLs in npm's package.json documentation for more details on this.