CommandHandler loads commands from subfolders several times
See original GitHub issueWhen 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:
- Created 3 years ago
- Comments:21 (10 by maintainers)
Top GitHub Comments
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 doingdel -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!