Skip to content

Commit

Permalink
Adding Delete Message Interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
exatombe committed Jan 1, 2024
1 parent d87ea28 commit f024524
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/bot/commands/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class IaCommand extends CommandsBase {
super(client, commandData);
}

async run(interaction: CommandInteraction): Promise<void> {
async run(interaction: CommandInteraction){
let options = interaction.options
if (options instanceof CommandInteractionOptionResolver) {
let subcommand = options.getSubcommand(true);
Expand Down
16 changes: 4 additions & 12 deletions src/bot/commands/baseCommands.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { ApplicationCommand, AutocompleteInteraction, CommandInteraction, RESTPostAPIApplicationCommandsJSONBody } from "discord.js";
import { ApplicationCommand, AutocompleteInteraction, CacheType, CommandInteraction, Interaction, MessageContextMenuCommandInteraction, RESTPostAPIApplicationCommandsJSONBody, UserContextMenuCommandInteraction } from "discord.js";
import Bot from "../index";

type Commande = RESTPostAPIApplicationCommandsJSONBody;
export default abstract class CommandsBase {
export default abstract class baseCommands {
name: string;
client: Bot;
command: ApplicationCommand;
autocomplete?(interaction: AutocompleteInteraction): Promise<void>
constructor(client: Bot, data: Commande, guildId?: string) {
if (guildId) {
client.application?.commands.create(data, guildId).then((cmd) => {
console.log(`[command] ${data.name} created`);
this.command = cmd;
}).catch((err) => {
console.log(`[command] ${data.name} not created`);
console.log(err);
});
} else {
if (!guildId) {
client.application?.commands.create(data).then((cmd) => {
console.log(`[command] ${data.name} created`);
this.command = cmd;
Expand All @@ -30,5 +22,5 @@ export default abstract class CommandsBase {
this.client = client;
}

abstract run(interaction: CommandInteraction): Promise<void>;
abstract run(interaction: CommandInteraction<CacheType>);
}
37 changes: 37 additions & 0 deletions src/bot/commands/deleteMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Bot from "..";
import { ApplicationCommandType, CacheType, ContextMenuCommandBuilder, ContextMenuCommandInteraction, Routes } from "discord.js";
import baseCommands from "./baseCommands";
const MessageCommand = new ContextMenuCommandBuilder()
.setName("Delete Message")
.setType(ApplicationCommandType.Message)
.toJSON();

export class DeleteMessage extends baseCommands {
constructor(client: Bot) {
super(client, MessageCommand);
}

async run(interaction: ContextMenuCommandInteraction<CacheType>) {
if (interaction.isMessageContextMenuCommand()) {
if (interaction.targetMessage.interaction.user.id === interaction.user.id) {
const i = await interaction.deferReply({
ephemeral: true
});
this.client.rest.delete(Routes.webhookMessage(this.client.user.id, interaction.token, interaction.targetMessage.id)).then(() => {
i.edit({
content: "Message supprimé",
});
}).catch((err) => {
i.edit({
content: "Une erreur est survenue",
});
});
} else {
interaction.reply({
content: "Vous ne pouvez pas supprimer ce message",
ephemeral: true
});
}
}
}
}
9 changes: 5 additions & 4 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActivityType, Client, Events, GatewayIntentBits, ModalSubmitInteraction, Partials } from "discord.js";
import { ActivityType, Client, ContextMenuCommandInteraction, Events, GatewayIntentBits, MessageContextMenuCommandInteraction, ModalSubmitInteraction, Partials } from "discord.js";
import AIHorde from "../internal_libs/aihorde";
import * as commandList from "./list.commands";
import CommandsBase from "./commands/baseCommands";
Expand Down Expand Up @@ -44,11 +44,12 @@ export default class Bot extends Client {
setTimeout(() => {
this.application.commands.fetch().then((commands) => {
commands.forEach((command) => {
if (!this.commands.has(command.name)) {
if(!this.commands.has(command.name)){
command.delete().then(() => {
console.log(`Command ${command.name} deleted`);
console.log(`[ApplicationCommand] ${command.name} deleted`);
}).catch((err) => {
console.error(`Error while deleting command ${command.name}: ${err}`);
console.log(`[ApplicationCommand] ${command.name} not deleted`);
console.log(err);
});
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/bot/list.commands.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./commands/ai"
export * from "./commands/ai"
export * from "./commands/deleteMessage"

0 comments on commit f024524

Please sign in to comment.