Skip to content

Commit f024524

Browse files
committed
Adding Delete Message Interaction
1 parent d87ea28 commit f024524

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

src/bot/commands/ai.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class IaCommand extends CommandsBase {
140140
super(client, commandData);
141141
}
142142

143-
async run(interaction: CommandInteraction): Promise<void> {
143+
async run(interaction: CommandInteraction){
144144
let options = interaction.options
145145
if (options instanceof CommandInteractionOptionResolver) {
146146
let subcommand = options.getSubcommand(true);

src/bot/commands/baseCommands.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
import { ApplicationCommand, AutocompleteInteraction, CommandInteraction, RESTPostAPIApplicationCommandsJSONBody } from "discord.js";
1+
import { ApplicationCommand, AutocompleteInteraction, CacheType, CommandInteraction, Interaction, MessageContextMenuCommandInteraction, RESTPostAPIApplicationCommandsJSONBody, UserContextMenuCommandInteraction } from "discord.js";
22
import Bot from "../index";
33

44
type Commande = RESTPostAPIApplicationCommandsJSONBody;
5-
export default abstract class CommandsBase {
5+
export default abstract class baseCommands {
66
name: string;
77
client: Bot;
88
command: ApplicationCommand;
99
autocomplete?(interaction: AutocompleteInteraction): Promise<void>
1010
constructor(client: Bot, data: Commande, guildId?: string) {
11-
if (guildId) {
12-
client.application?.commands.create(data, guildId).then((cmd) => {
13-
console.log(`[command] ${data.name} created`);
14-
this.command = cmd;
15-
}).catch((err) => {
16-
console.log(`[command] ${data.name} not created`);
17-
console.log(err);
18-
});
19-
} else {
11+
if (!guildId) {
2012
client.application?.commands.create(data).then((cmd) => {
2113
console.log(`[command] ${data.name} created`);
2214
this.command = cmd;
@@ -30,5 +22,5 @@ export default abstract class CommandsBase {
3022
this.client = client;
3123
}
3224

33-
abstract run(interaction: CommandInteraction): Promise<void>;
25+
abstract run(interaction: CommandInteraction<CacheType>);
3426
}

src/bot/commands/deleteMessage.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Bot from "..";
2+
import { ApplicationCommandType, CacheType, ContextMenuCommandBuilder, ContextMenuCommandInteraction, Routes } from "discord.js";
3+
import baseCommands from "./baseCommands";
4+
const MessageCommand = new ContextMenuCommandBuilder()
5+
.setName("Delete Message")
6+
.setType(ApplicationCommandType.Message)
7+
.toJSON();
8+
9+
export class DeleteMessage extends baseCommands {
10+
constructor(client: Bot) {
11+
super(client, MessageCommand);
12+
}
13+
14+
async run(interaction: ContextMenuCommandInteraction<CacheType>) {
15+
if (interaction.isMessageContextMenuCommand()) {
16+
if (interaction.targetMessage.interaction.user.id === interaction.user.id) {
17+
const i = await interaction.deferReply({
18+
ephemeral: true
19+
});
20+
this.client.rest.delete(Routes.webhookMessage(this.client.user.id, interaction.token, interaction.targetMessage.id)).then(() => {
21+
i.edit({
22+
content: "Message supprimé",
23+
});
24+
}).catch((err) => {
25+
i.edit({
26+
content: "Une erreur est survenue",
27+
});
28+
});
29+
} else {
30+
interaction.reply({
31+
content: "Vous ne pouvez pas supprimer ce message",
32+
ephemeral: true
33+
});
34+
}
35+
}
36+
}
37+
}

src/bot/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ActivityType, Client, Events, GatewayIntentBits, ModalSubmitInteraction, Partials } from "discord.js";
1+
import { ActivityType, Client, ContextMenuCommandInteraction, Events, GatewayIntentBits, MessageContextMenuCommandInteraction, ModalSubmitInteraction, Partials } from "discord.js";
22
import AIHorde from "../internal_libs/aihorde";
33
import * as commandList from "./list.commands";
44
import CommandsBase from "./commands/baseCommands";
@@ -44,11 +44,12 @@ export default class Bot extends Client {
4444
setTimeout(() => {
4545
this.application.commands.fetch().then((commands) => {
4646
commands.forEach((command) => {
47-
if (!this.commands.has(command.name)) {
47+
if(!this.commands.has(command.name)){
4848
command.delete().then(() => {
49-
console.log(`Command ${command.name} deleted`);
49+
console.log(`[ApplicationCommand] ${command.name} deleted`);
5050
}).catch((err) => {
51-
console.error(`Error while deleting command ${command.name}: ${err}`);
51+
console.log(`[ApplicationCommand] ${command.name} not deleted`);
52+
console.log(err);
5253
});
5354
}
5455
});

src/bot/list.commands.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from "./commands/ai"
1+
export * from "./commands/ai"
2+
export * from "./commands/deleteMessage"

0 commit comments

Comments
 (0)