Skip to content

Commit

Permalink
command handler update
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSerge01 committed Nov 9, 2024
1 parent b98a4ca commit 213cbe3
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 64 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"devDependencies": {
"@types/ms": "^0.7.34",
"bun-types": "^1.1.33",
"bun-types": "^1.1.34",
"typescript": "^5.6.3"
}
}
2 changes: 1 addition & 1 deletion src/commands/About.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class About {
{
name: "📃 • General",
value: [
"Version **0.1**, *Kaishi*",
"Version **0.1.1**, *Kaishi*",
`**${members}** members • **${guilds.size}** guild${guilds.size == 1 ? "" : "s"} ${
!shards ? "" : `• **${shards}** shard${shards == 1 ? "" : "s"}`
}`
Expand Down
7 changes: 6 additions & 1 deletion src/commands/news/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ export default class View {
.setStyle(ButtonStyle.Primary)
);

const reply = await interaction.reply({ embeds: [getEmbed()], components: [row] });
const reply = await interaction.reply({
embeds: [getEmbed()],
components: page >= 1 ? [row] : []
});

if (page < 1) return;
const collector = reply.createMessageComponentCollector({ time: 30000 });
collector.on("collect", async (i: ButtonInteraction) => {
if (i.message.id != (await reply.fetch()).id)
Expand Down
2 changes: 1 addition & 1 deletion src/events/easterEggs/fireship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default async function run(message: Message) {
if (
content.startsWith("this has been") &&
content.endsWith("in 100 seconds") &&
message.content != "this has been in 100 seconds"
message.content.toLowerCase() != "this has been in 100 seconds"
)
await (message.channel as TextChannel).send(
"hit the like button and subscribe if you want to see more short videos like this thanks for watching and I will see you in the next one"
Expand Down
53 changes: 9 additions & 44 deletions src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,14 @@
import { file } from "bun";
import type { AutocompleteInteraction, CommandInteraction } from "discord.js";
import { join } from "path";
import { pathToFileURL } from "url";
import { capitalize } from "../utils/capitalize";
import { Event } from "../utils/types";
import { Commands } from "../handlers/commands";
import { Event } from "../utils/types";

async function getCommand(
interaction: CommandInteraction | AutocompleteInteraction,
options: any
): Promise<any> {
const commandName = capitalize(interaction.commandName)!;
const subcommandName = capitalize(options.getSubcommand(false));
const commandGroupName = capitalize(options.getSubcommandGroup(false));
let commandImportPath = join(
join(process.cwd(), "src", "commands"),
`${
subcommandName
? `${commandName.toLowerCase()}/${
commandGroupName ? `${commandGroupName}/${subcommandName}` : subcommandName
}`
: commandName
}.ts`
export default (async function run(interaction) {
if (!interaction.isChatInputCommand() && !interaction.isAutocomplete()) return;
const command = await new Commands(interaction.client).getCommand(
interaction.commandName,
interaction.options
);
if (!command) return;

if (!(await file(commandImportPath).exists()))
commandImportPath = join(join(process.cwd(), "src", "commands", `${commandName}.ts`));

return new (await import(pathToFileURL(commandImportPath).toString())).default();
}

export default (async function run(interaction) {
if (interaction.isChatInputCommand()) {
console.log(
new Commands(interaction.client).getCommand(interaction.commandName, interaction.options)
);
console.log(await getCommand(interaction, interaction.options));
const command = await getCommand(interaction, interaction.options);
if (!command) return;
if (command.deferred) await interaction.deferReply();
command.run(interaction);
} else if (interaction.isAutocomplete()) {
const command = await getCommand(interaction, interaction.options);
if (!command) return;
if (!command.autocomplete) return;
command.autocomplete(interaction);
}
if (interaction.isChatInputCommand()) command.run(interaction);
if (command.autocomplete) command.autocomplete(interaction);
} as Event<"interactionCreate">);
47 changes: 31 additions & 16 deletions src/handlers/commands.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import {
Guild,
SlashCommandBuilder,
SlashCommandSubcommandBuilder,
SlashCommandSubcommandGroupBuilder,
type Client
} from "discord.js";
import { readdirSync } from "fs";
import { join } from "path";
import { pathToFileURL } from "url";
import { capitalize } from "../utils/capitalize";
import { getDisabledCommands } from "../utils/database/disabledCommands";

export let commands: SlashCommandBuilder[] = [];
export let commands: { data: SlashCommandBuilder; run: any; autocomplete: any }[] = [];
let subcommands: { data: SlashCommandSubcommandBuilder; run: any; autocomplete: any }[] = [];
export class Commands {
client: Client;
constructor(client: Client) {
this.client = client;
}

private async createSubCommand(
name: string,
...disabledCommands: string[]
): Promise<SlashCommandBuilder> {
private async createSubCommand(name: string, ...disabledCommands: string[]) {
const commandsPath = join(process.cwd(), "src", "commands");
const run = [];
const autocomplete = [];
const command = new SlashCommandBuilder()
.setName(name.toLowerCase())
.setDescription("This command has no description.");
Expand All @@ -44,7 +44,17 @@ export class Commands {
const subCommand = new subCommandModule.default();

command.addSubcommand(subCommand.data);
if ("autocompleteHandler" in subCommand) subCommand.autocompleteHandler(this.client);
run.push(subCommand.run);
subcommands.push({
data: subCommand.data,
run: subCommand.run,
autocomplete: subCommand.autocomplete
});

if ("autocompleteHandler" in subCommand) {
subCommand.autocompleteHandler(this.client);
autocomplete.push(subCommand.autocomplete);
}
continue;
}

Expand Down Expand Up @@ -77,7 +87,7 @@ export class Commands {
command.addSubcommandGroup(subCommandGroup);
}

return command;
return { data: command, run: run, autocomplete: autocomplete };
}

async loadCommands(...disabledCommands: string[]) {
Expand All @@ -90,7 +100,7 @@ export class Commands {

if (commandFile.isFile()) {
const commandImport = await import(pathToFileURL(join(commandsPath, name)).toString());
commands.push(new commandImport.default().data);
commands.push(new commandImport.default());
continue;
}

Expand All @@ -99,15 +109,20 @@ export class Commands {
join(commandsPath, name),
...disabledCommands
);
commands.push(subCommand);

commands.push({
data: subCommand.data,
run: subCommand.run,
autocomplete: subCommand.autocomplete
});
}

return commands;
}

async registerCommandsForGuild(guild: Guild, ...disabledCommands: string[]) {
await this.loadCommands(...disabledCommands);
await guild.commands.set(commands);
await guild.commands.set(commands.map(command => command.data));
}

async registerCommands(): Promise<any[]> {
Expand All @@ -117,16 +132,16 @@ export class Commands {
for (const guildID of guilds.keys()) {
const disabledCommands = getDisabledCommands(guildID);
if (disabledCommands.length > 0) await this.loadCommands(...disabledCommands);
await guilds.get(guildID)?.commands.set(commands);
await guilds.get(guildID)?.commands.set(commands.map(command => command.data));
}

return commands;
}

async getCommand(name: string, options: any) {
const subcommandName = capitalize(options.getSubcommand(false));
const commandGroupName = capitalize(options.getSubcommandGroup(false));
console.log(commands.filter(command => command.name == name)[0]);
return commands.filter(command => command.name == name)[0];
const subcommandName = options.getSubcommand(false);
return subcommandName
? subcommands.filter(subcommand => subcommand.data.name == subcommandName)[0]
: commands.filter(command => command.data.name == name)[0];
}
}

0 comments on commit 213cbe3

Please sign in to comment.