This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/Fluorinebot/fluorine
- Loading branch information
Showing
30 changed files
with
106 additions
and
565 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,68 @@ | ||
import { readdirSync } from 'fs'; | ||
import { Command } from 'types/command'; | ||
import { ChatInputCommand, ChatInputSubcommand, ContextMenuCommand } from 'types/structures'; | ||
import { Collection } from 'discord.js'; | ||
import FluorineClient from '@classes/Client'; | ||
export default class CommandHandler extends Collection<string, Command> { | ||
client: FluorineClient; | ||
constructor(client) { | ||
super(); | ||
import { loadDirectory, loadParentDirectory } from '@util/files'; | ||
import { SlashCommandBuilder, SlashCommandSubcommandBuilder } from '@discordjs/builders'; | ||
|
||
export default class CommandHandler { | ||
chatInput = new Collection<string, ChatInputCommand | ChatInputSubcommand>(); | ||
contextMenu = new Collection<string, ContextMenuCommand>(); | ||
|
||
constructor(private client: FluorineClient) { | ||
this.client = client; | ||
} | ||
loadCommands() { | ||
const dir = readdirSync(`${__dirname}/../../cmds`); | ||
dir.forEach(async file => { | ||
if (!file.endsWith('.js')) { | ||
return; | ||
|
||
private addFullBuilder(command: ChatInputCommand | ChatInputSubcommand) { | ||
if ('category' in command) { | ||
const subcommandNames = [...this.chatInput.keys()].filter(c => c.startsWith(`${command.data.name}/`)); | ||
|
||
const subcommands = subcommandNames.map(subcommandName => { | ||
const subcommand = this.chatInput.get(subcommandName); | ||
if (!('category' in subcommand)) { | ||
return subcommand.data; | ||
} | ||
}); | ||
|
||
this.getMergedCommandData(command.data, subcommands); | ||
} | ||
} | ||
|
||
private getMergedCommandData(base: SlashCommandBuilder, data: SlashCommandSubcommandBuilder[] = []) { | ||
for (const subcommand of data) { | ||
if (subcommand) { | ||
base.addSubcommand(subcommand); | ||
} | ||
const [name] = file.split('.'); | ||
this.set(name, await import(`${__dirname}/../../cmds/${file}`)); | ||
}); | ||
this.client.logger.log(`Loaded ${dir.length} text commands`); | ||
return this; | ||
} | ||
|
||
return base; | ||
} | ||
|
||
async loadChatInput() { | ||
const [commands, subcommands] = await loadParentDirectory<ChatInputCommand, ChatInputSubcommand>('../commands'); | ||
|
||
for (const command of commands) { | ||
this.chatInput.set(command.data.name, command); | ||
} | ||
|
||
for (const subcommand of subcommands) { | ||
const [key] = subcommand.name.endsWith('index') ? subcommand.name.split('/') : [subcommand.name]; | ||
this.chatInput.set(key, subcommand.data); | ||
} | ||
|
||
this.chatInput.forEach(c => this.addFullBuilder(c)); | ||
|
||
const commandsLoaded = [...this.chatInput.keys()].filter(key => !key.includes('/')); | ||
this.client.logger.log(`Loaded ${commandsLoaded.length} chat input commands.`); | ||
return this.chatInput; | ||
} | ||
|
||
async loadContextMenu() { | ||
const files = await loadDirectory<ContextMenuCommand>('../context'); | ||
for (const file of files) { | ||
this.contextMenu.set(file.data.data.name, file.data); | ||
} | ||
|
||
this.client.logger.log(`Loaded ${files.length} context menu commands.`); | ||
return this.contextMenu; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.