Finalisation Command Handler + Ajout commande ping

This commit is contained in:
Melaine Gérard 2024-09-19 13:55:57 +02:00
parent a9b91137a1
commit e95074ec29
No known key found for this signature in database
GPG Key ID: 89A9588EB2726A53
6 changed files with 78 additions and 9 deletions

View File

@ -11,7 +11,7 @@ export class Command implements ICommand {
category: Category; category: Category;
options: object; options: object;
default_member_permissions: bigint; default_member_permissions: bigint;
dm_permissions: boolean; dm_permission: boolean;
cooldown: number; cooldown: number;
constructor(client: CustomClient, options: ICommandOptions) { constructor(client: CustomClient, options: ICommandOptions) {
@ -21,7 +21,7 @@ export class Command implements ICommand {
this.category = options.category; this.category = options.category;
this.options = options.options; this.options = options.options;
this.default_member_permissions = options.default_member_permissions; this.default_member_permissions = options.default_member_permissions;
this.dm_permissions = options.dm_permissions; this.dm_permission = options.dm_permission;
this.cooldown = options.cooldown; this.cooldown = options.cooldown;
} }

View File

@ -9,7 +9,7 @@ export interface ICommand {
category: Category; category: Category;
options: object; options: object;
default_member_permissions: bigint; default_member_permissions: bigint;
dm_permissions: boolean; dm_permission: boolean;
cooldown: number; cooldown: number;
execute(interaction: ChatInputCommandInteraction): void; execute(interaction: ChatInputCommandInteraction): void;

View File

@ -6,6 +6,6 @@ export interface ICommandOptions {
category: Category; category: Category;
options: object; options: object;
default_member_permissions: bigint; default_member_permissions: bigint;
dm_permissions: boolean; dm_permission: boolean;
cooldown: number; cooldown: number;
} }

View File

@ -0,0 +1,25 @@
import {Command} from "../base/classes/Command";
import {CustomClient} from "../base/classes/CustomClient";
import {Category} from "../base/enums/Category";
import {PermissionsBitField} from "discord.js";
export class PingCommand extends Command {
constructor(client: CustomClient) {
super(client, {
name: 'ping',
description: ' Pong!',
category: Category.UTILITIES,
options: [],
default_member_permissions: PermissionsBitField.Flags.UseApplicationCommands,
dm_permission: true,
cooldown: 5
});
}
execute(interaction: any): void {
interaction.reply({
content: `Pong!`,
ephemeral: true
});
}
}

View File

@ -1,6 +1,7 @@
import {Event} from '../../base/classes/Event'; import {Event} from '../../base/classes/Event';
import {CustomClient} from "../../base/classes/CustomClient"; import {CustomClient} from "../../base/classes/CustomClient";
import {Events} from "discord.js"; import {Collection, Events, REST, Routes} from "discord.js";
import {Command} from "../../base/classes/Command";
export class Ready extends Event { export class Ready extends Event {
constructor(client: CustomClient) { constructor(client: CustomClient) {
super(client, { super(client, {
@ -10,7 +11,38 @@ export class Ready extends Event {
}); });
} }
execute(...args: any[]): void { async execute(...args: any[]): Promise<void> {
console.log(`Connecté en tant que ${this.client.user?.tag}!`); console.log(`Connecté en tant que ${this.client.user?.tag}!`);
const commands: object[] = this.getJson(this.client.commands);
const rest = new REST({version: '10'}).setToken(this.client.config.token);
const setCommands: any = await rest.put(
Routes.applicationCommands(this.client.user?.id!),
{
body: commands
}
);
console.log(`${setCommands.length} Commandes mises à jours avec succès !`);
}
private getJson(commands: Collection<string, Command>): object[] {
const data: object[] = [];
commands.forEach((command: Command) => {
data.push({
name: command.name,
description: command.description,
options: command.options,
default_member_permissions: command.default_member_permissions.toString(),
dm_permission: command.dm_permission,
})
});
return data;
} }
} }

View File

@ -33,12 +33,24 @@ export class CommandHandler extends Event {
const cooldownAmount = (command.cooldown || 3) * 1000; const cooldownAmount = (command.cooldown || 3) * 1000;
if (timestamps.has(interaction.user.id) && now < (timestamps.get(interaction.user.id) || 0) + cooldownAmount) { if (timestamps.has(interaction.user.id) && now < (timestamps.get(interaction.user.id) || 0) + cooldownAmount) {
const timeLeft = (((timestamps.get(interaction.user.id) || 0) + cooldownAmount - now) / 1000).toFixed(1);
await interaction.reply({ await interaction.reply({
content: `Veuillez patienter ${((timestamps.get(interaction.user.id) || 0) + cooldownAmount - now) / 1000} secondes avant de réutiliser la commande \`${command.name}\`` content: `Veuillez patienter ${timeLeft} secondes avant de réutiliser la commande \`${command.name}\``,
}) ephemeral: true
});
return; return;
} }
timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
command.execute(interaction); try {
const subCommandGroup = interaction.options.getSubcommandGroup(false);
const subCommand = `${interaction.commandName}${subCommandGroup ? `.${subCommandGroup}` : ''}.${interaction.options.getSubcommand(false)}` || '';
this.client.subCommands.get(subCommand)?.execute(interaction) || command.execute(interaction);
} catch (error) {
console.error(error);
}
} }
} }