-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: ajout du support des menus contextuels #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { | ||
| ContextMenuCommandBuilder, | ||
| ContextMenuCommandInteraction, | ||
| MessageFlags, | ||
| } from "discord.js"; | ||
| import BotClient from "../BotClient"; | ||
|
|
||
| type ContextRunFn = ( | ||
| client: BotClient, | ||
| interaction: ContextMenuCommandInteraction, | ||
| ) => unknown; | ||
| export default class ContextMenu extends ContextMenuCommandBuilder { | ||
| private runFn?: ContextRunFn; | ||
| constructor() { | ||
| super(); | ||
| } | ||
|
|
||
| run(fn: ContextRunFn) { | ||
| this.runFn = fn; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * DO NOT USE | ||
| * Internal method to execute the function | ||
| */ | ||
| execute(client: BotClient, interaction: ContextMenuCommandInteraction) { | ||
| if (!this.runFn) { | ||
| client.logger.error( | ||
| new Error(`The command ${this.name} has no function to execute`), | ||
| ); | ||
| return interaction.reply({ | ||
| content: `The command ${this.name} has no function to execute!`, | ||
| flags: [MessageFlags.Ephemeral], | ||
| }); | ||
| } | ||
| return this.runFn(client, interaction); | ||
| } | ||
|
|
||
| getDiscordCommand() { | ||
| return this.toJSON(); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /** | ||
| * Copyright (c) 2025 Cleboost | ||
| * External contributor can be found on the GitHub | ||
| * Licence: on the GitHub | ||
| */ | ||
|
|
||
| import { | ||
| Collection, | ||
| ContextMenuCommandInteraction, | ||
| MessageFlags, | ||
| } from "discord.js"; | ||
| import { underline } from "chalk"; | ||
| import BotClient from "../class/BotClient"; | ||
| import { pushToApi } from "./loader"; | ||
| import ContextMenu from "../class/interactions/ContextMenu"; | ||
|
|
||
| export default class ContetxtMenuHandler { | ||
| private client: BotClient; | ||
| private contextMenus: Collection<string, ContextMenu> = new Collection(); | ||
|
|
||
| constructor(client: BotClient) { | ||
| this.client = client; | ||
| } | ||
|
|
||
| addInteraction(command: ContextMenu) { | ||
| if (this.contextMenus.has(command.name)) { | ||
| this.client.logger.warn( | ||
| `The command ${underline(command.name)} is already loaded! Skipping...`, | ||
| ); | ||
| return; | ||
| } | ||
| this.contextMenus.set(command.name, command); | ||
| return; | ||
| } | ||
|
|
||
| removeInteraction(command: ContextMenu) { | ||
| if (!this.contextMenus.has(command.name)) { | ||
| this.client.logger.warn( | ||
| `The command ${underline(command.name)} is not loaded! Skipping...`, | ||
| ); | ||
| return; | ||
| } | ||
| this.contextMenus.delete(command.name); | ||
| return; | ||
| } | ||
|
|
||
| reloadInteraction(command: ContextMenu) { | ||
| const existingCommand = this.contextMenus.find( | ||
| (cmd) => cmd.name === command.name, | ||
| ); | ||
|
|
||
| if (!existingCommand) { | ||
| this.client.logger.warn( | ||
| `The command ${underline(command.name)} is not loaded! Adding instead...`, | ||
| ); | ||
| this.addInteraction(command); | ||
| pushToApi(this.client); | ||
| this.client.logger.info( | ||
| `Command ${underline(command.name)} added successfully, and pushed to API. You may reload your Discord client to see the changes.`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (existingCommand.name !== command.name) { | ||
| this.client.logger.info( | ||
| `Command name changed: ${underline(existingCommand.name)} -> ${underline(command.name)}`, | ||
| ); | ||
| } | ||
|
|
||
| this.removeInteraction(existingCommand); | ||
| this.addInteraction(command); | ||
| return; | ||
| } | ||
|
|
||
| async eventContextMenu(interaction: ContextMenuCommandInteraction) { | ||
| const cmd = this.contextMenus.get(interaction.commandName); | ||
| if (!cmd) return; | ||
| cmd.execute(this.client, interaction); | ||
|
|
||
| if (this.client.config?.logger?.logCmd) | ||
| this.client.logger.info( | ||
| `ContextMenu ${underline(interaction.commandName)} used by ${interaction.user.username} (${interaction.user.id})`, | ||
| ); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 2000)); | ||
| if (interaction.deferred || interaction.replied) return; | ||
|
|
||
| await interaction.reply({ | ||
| content: | ||
| "ContextMenu took too long to respond or an error occurred during execution (please report this to the bot developer)", | ||
| flags: [MessageFlags.Ephemeral], | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| listCommands(): ContextMenu[] { | ||
| return Array.from(this.contextMenus.values()); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * Copyright (c) 2025 Cleboost | ||
| * External contributor can be found on the GitHub | ||
| * Licence: on the GitHub | ||
| */ | ||
|
|
||
| import { ApplicationCommandType, MessageFlags } from "discord.js"; | ||
| import { ContextMenu } from "djs-core"; | ||
|
|
||
| export default new ContextMenu() | ||
| .setName("Test Context Menu") | ||
| .setType(ApplicationCommandType.User) | ||
| .run((client, interaction) => { | ||
| interaction.reply({ | ||
| content: "Hello from the context meenu!", | ||
| flags: [MessageFlags.Ephemeral], | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.