question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CommandHandler loads commands from subfolders several times

See original GitHub issue

When trying to loadAll() commands from folder, that contains subfolders of command files, command handler is trying to load those files second time. But before I will tell you what problem is, I would like to mention that I use TypeScript and custom Command class:

import { Command, CommandOptions } from "discord-akairo";
import { Repository, getRepository } from "typeorm";
import { Guild } from "../entity/Guild.entity";
import { Search } from "../entity/Search.entity";
import { Chat } from "../entity/Chat.entity";
import { User } from "../entity/User.entity";
import CustomClient from "./Client";
import { Report } from "../entity/Report.entity";
import { Message } from "discord.js";
import i18n from "i18n";

class CustomCommand extends Command {
  client: CustomClient;
  guildRepository: Repository<Guild>;
  searchRepository: Repository<Search>;
  chatRepository: Repository<Chat>;
  userRepository: Repository<User>;
  reportRepository: Repository<Report>;
  user: User;
  guild: Guild;
  chat: Chat;
  userChatId: string;

  constructor(id: string, options?: CommandOptions) {
    super(id, options);
    this.guildRepository = getRepository(Guild);
    this.searchRepository = getRepository(Search);
    this.chatRepository = getRepository(Chat);
    this.userRepository = getRepository(User);
    this.reportRepository = getRepository(Report);
  }

  async before(message: Message) {
    this.user = await this.userRepository.findOne({
      userId: message.author.id
    });
    i18n.setLocale(this.user.locale);

    this.chat = await this.chatRepository.findOne({
      where: [
        {
          user1Id: this.user.userId,
          endedAt: null
        },
        {
          user2Id: this.user.userId,
          endedAt: null
        }
      ]
    });

    if (this.chat) {
      this.userChatId =
        this.chat.user1Id === message.author.id ? "user1Id" : "user2Id";
    }

    if (message.guild) {
      this.guild = await this.guildRepository.findOne({
        where: { discordId: message.guild.id }
      });
    }
  }
}

export default CustomCommand;

The error log is:

(node:6340) UnhandledPromiseRejectionWarning: AkairoError [ALREADY_LOADED]: Command 'config-guild' is already loaded
    at CommandHandler.load (C:\Users\deris\Documents\Projects\AnonChats\node_modules\discord-akairo\src\struct\AkairoHandler.js:137:45)
    at CommandHandler.loadAll (C:\Users\deris\Documents\Projects\AnonChats\node_modules\discord-akairo\src\struct\AkairoHandler.js:156:40)
    at CustomClient.initHandlers (C:\Users\deris\Documents\Projects\AnonChats\dist\struct\Client.js:105:29)
    at CustomClient.init (C:\Users\deris\Documents\Projects\AnonChats\dist\struct\Client.js:53:14)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:6340) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

This is my folder structure for commands folder, that I specified in directory property for CommandHandler in my Client class:

commands
β”œβ”€β”€ bot
β”‚   β”œβ”€β”€ help.ts
β”‚   β”œβ”€β”€ info.ts
β”‚   β”œβ”€β”€ invite.ts
β”‚   └── ping.ts
β”œβ”€β”€ chat
β”‚   β”œβ”€β”€ deanon.ts
β”‚   β”œβ”€β”€ game.ts
β”‚   β”œβ”€β”€ report.ts
β”‚   β”œβ”€β”€ search.ts
β”‚   β”œβ”€β”€ stop.ts
β”‚   └── stopSearch.ts
└── config
    β”œβ”€β”€ config-guild.ts
    β”œβ”€β”€ config.ts
    β”œβ”€β”€ language.ts
    └── prefix.ts

And I checked several times that all of those files contains unique ids.

I’ve tried to do some experiment with those problem and write this.commandHandler.on("load", command => console.log(command.aliases[0])); in my Client class and that what I have:

help
stats
invite
ping
deanon
game
report
search
stop
stop-search
config-guild
config
language
prefix
(node:6340) UnhandledPromiseRejectionWarning: AkairoError [ALREADY_LOADED]: { error log that I have write above }

So, I can tell that CommandHandler is trying to read dirs with commands the second time, when all commands are already loaded.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:21 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
ridays2001commented, Oct 29, 2020

I had a problem similar to this. But, I figured out that it was because of the duplicate files. To avoid this problem in the future, I use a module called rimraf that deletes a folder. You can also use the basic delete command, but it didn’t work for me (flags didn’t work in npm scripts for some reason, so doing del -r dist was hard), so I am using rimraf. I set my npm scripts to "start": "rimraf dist && tsc && node ." That would delete the dist folder and compile everything again.

0reactions
D3risecommented, Jan 31, 2021

I had a problem similar to this. But, I figured out that it was because of the duplicate files. To avoid this problem in the future, I use a module called rimraf that deletes a folder. You can also use the basic delete command, but it didn’t work for me (flags didn’t work in npm scripts for some reason, so doing del -r dist was hard), so I am using rimraf. I set my npm scripts to "start": "rimraf dist && tsc && node ." That would delete the dist folder and compile everything again.

This was an actual solution. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use subfolders in a command handler - Stack Overflow
I want to make command sub folders but my bot doesn't read commands inside the folders. There is no error. const fs =...
Read more >
Discord JS Advanced Command Handler (2020) [Episode #22]
In this video we go over how to create an advanced command handler using Discord.JS.
Read more >
Additional features - Discord.js v12 Guide
The command handler you've been building so far doesn't do much ... You can create any number of subfolders and name them whatever...
Read more >
[Solved]-Discord.JS Command Handler Sub-Folders-discord.js
Move everything into its own function with the path as a parameter. Every time you find a folder, call the function with the...
Read more >
Implementing the microservice application layer using the ...
The class is a command handler, which will get covered in the next ... the same CreateOrder command reaches your system multiple times,Β ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found