-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor: command builder #21
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
base: main
Are you sure you want to change the base?
Changes from all commits
0f60701
6ac74bd
02e205a
f499b37
c94d1a9
74cefb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| CHANGELOG.md | ||
| cargo.toml | ||
| .husky | ||
| cliff.toml | ||
|
|
||
| # Test | ||
| examples | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "command.notfound": "Command not found or disabled1", | ||
| "command.disabled": "Command not found or disabled2" | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,40 @@ | ||
| import { Events, MessageFlags } from "discord.js"; | ||
| import { EventBuilder, Context } from "#structures"; | ||
| import { EventBuilder, Context } from "../structures/index"; | ||
|
|
||
| new EventBuilder(Events.InteractionCreate, false).onExecute( | ||
| async function (context, interaction) { | ||
| if (!interaction.isChatInputCommand()) return; | ||
|
|
||
| const command = context.client.commands.get(interaction.commandName); | ||
| if (!command || !command.supportsSlash) { | ||
| const ctx = new Context(context.client, { interaction }); | ||
|
|
||
| if (!command) { | ||
| await interaction.reply({ | ||
| content: "Command not found or disabled.", | ||
| content: ctx.t("error.command.notfound", { | ||
| defaultValue: "Command not found or disabled.", | ||
| }), | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| return; | ||
| } | ||
| if (!command.supportsSlash) { | ||
| await interaction.reply({ | ||
| content: ctx.t("error.command.disabled", { | ||
| defaultValue: "Command not found or disabled.", | ||
| }), | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| return; | ||
| } | ||
|
Comment on lines
+11
to
28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default values should be distinct for different error states. Same issue as in 💡 Proposed fix const ctx = new Context(context.client, { interaction });
+ ctx.locale = interaction.locale;
if (!command) {
await interaction.reply({
content: ctx.t("error.command.notfound", {
- defaultValue: "Command not found or disabled.",
+ defaultValue: "Command not found.",
}),
flags: MessageFlags.Ephemeral,
});
return;
}
if (!command.supportsSlash) {
await interaction.reply({
content: ctx.t("error.command.disabled", {
- defaultValue: "Command not found or disabled.",
+ defaultValue: "This command is not available as a slash command.",
}),
flags: MessageFlags.Ephemeral,
});
return;
}
try {
- ctx.locale = interaction.locale;
context.logger.debug(🤖 Prompt for AI Agents |
||
|
|
||
| try { | ||
| const ctx = new Context(context.client, { interaction }); | ||
| ctx.locale = interaction.locale; | ||
| context.logger.debug( | ||
| `${ctx.author?.tag ?? "Unknown"} used ${command.name}(interaction)` | ||
| `${ctx.author?.tag ?? "Unknown"} used ${command.data.name}(interaction)` | ||
| ); | ||
| if (command._onInteraction) await command._onInteraction(ctx.toJSON()); | ||
| } catch (error) { | ||
| context.client.logger.error( | ||
| `Error executing command ${command.name}:`, | ||
| `Error executing command ${command.data.name}:`, | ||
| error | ||
| ); | ||
| if (interaction.replied || interaction.deferred) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { Events } from "discord.js"; | ||
| import { EventBuilder, Context } from "#structures"; | ||
| import { deleteMessageAfterSent } from "#utils"; | ||
| import { EventBuilder, Context } from "../structures/index"; | ||
| import { deleteMessageAfterSent } from "../utils/index"; | ||
|
|
||
| new EventBuilder( | ||
| Events.MessageCreate, | ||
|
|
@@ -22,27 +22,42 @@ new EventBuilder( | |
| const commandAlias = context.client.aliases.findKey((cmd) => | ||
| cmd.has(commandName) | ||
| ); | ||
| const ctx = new Context(context.client, { message, args }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing locale assignment for message context. In 🤖 Prompt for AI Agents |
||
|
|
||
| let command = context.client.commands.get(commandAlias ?? commandName); | ||
| if (!command || !command.supportsPrefix) { | ||
|
|
||
| if (!command) { | ||
| await message | ||
| .reply({ | ||
| content: ctx.t("error.command.notfound", { | ||
| defaultValue: "Command not found or disabled.", | ||
| }), | ||
| allowedMentions: { repliedUser: false }, | ||
| }) | ||
| .then(deleteMessageAfterSent); | ||
| return; | ||
| } | ||
|
|
||
| if (!command.supportsPrefix) { | ||
| await message | ||
| .reply({ | ||
| content: "Command not found or disabled.", | ||
| content: ctx.t("error.command.disabled", { | ||
| defaultValue: "Command not found or disabled.", | ||
| }), | ||
| allowedMentions: { repliedUser: false }, | ||
| }) | ||
| .then(deleteMessageAfterSent); | ||
| return; | ||
| } | ||
|
Comment on lines
+29
to
51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default values should be distinct for different error states. Both 💡 Proposed fix with distinct messages if (!command) {
await message
.reply({
content: ctx.t("error.command.notfound", {
- defaultValue: "Command not found or disabled.",
+ defaultValue: "Command not found.",
}),
allowedMentions: { repliedUser: false },
})
.then(deleteMessageAfterSent);
return;
}
if (!command.supportsPrefix) {
await message
.reply({
content: ctx.t("error.command.disabled", {
- defaultValue: "Command not found or disabled.",
+ defaultValue: "This command is not available for prefix usage.",
}),
allowedMentions: { repliedUser: false },
})
.then(deleteMessageAfterSent);
return;
}🤖 Prompt for AI Agents |
||
|
|
||
| try { | ||
| const ctx = new Context(context.client, { message, args }); | ||
| context.logger.debug( | ||
| `${ctx.author?.tag ?? "Unknown"} used ${command.name}(message)` | ||
| `${ctx.author?.tag ?? "Unknown"} used ${command.data.name}(message)` | ||
| ); | ||
| if (command._onMessage) await command._onMessage(ctx.toJSON()); | ||
| } catch (error) { | ||
| context.client.logger.error( | ||
| `Error executing command ${command.name}:`, | ||
| `Error executing command ${command.data.name}:`, | ||
| error | ||
| ); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| export * from "#structures"; | ||
| export * from "./structures"; | ||
| export * from "./utils/logger/Logger"; | ||
| export * from "#ctx"; | ||
| export * from "./context"; | ||
|
|
||
| export const version = "[VI]{{version}}[/VI]"; |
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| SlashCommandBuilder, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RESTPostAPIChatInputApplicationCommandsJSONBody, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from "discord.js"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { normalizeArray } from "../../utils/normalizeArray"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Client } from "../core"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface ApplicationJSONBody extends RESTPostAPIChatInputApplicationCommandsJSONBody { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| prefix_support: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| slash_support: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| aliases: string[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| export class ApplicationCommandBuilder extends SlashCommandBuilder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| constructor() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| super(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+13
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Redundant constructor can be removed. The constructor only calls 🔧 Proposed fix export class ApplicationCommandBuilder extends SlashCommandBuilder {
- constructor() {
- super();
- }
protected prefix_support: boolean = true;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| protected prefix_support: boolean = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| protected slash_support: boolean = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| protected aliases: string[] = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| setAliases(...alias: string[]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Reflect.set(this, "aliases", normalizeArray(alias)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| addAliases(...alias: string[]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const currentAliases = Reflect.get(this, "aliases") || []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Reflect.set( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "aliases", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| normalizeArray([...currentAliases, ...normalizeArray(alias)]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| setPrefixSupport(support: boolean) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Reflect.set(this, "prefix_support", support); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| setSlashSupport(support: boolean) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Reflect.set(this, "slash_support", support); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+21
to
+44
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Unnecessary use of These properties are defined directly on the class. Using ♻️ Proposed refactor for direct property access setAliases(...alias: string[]) {
- Reflect.set(this, "aliases", normalizeArray(alias));
+ this.aliases = normalizeArray(alias);
return this;
}
addAliases(...alias: string[]) {
- const currentAliases = Reflect.get(this, "aliases") || [];
- Reflect.set(
- this,
- "aliases",
- normalizeArray([...currentAliases, ...normalizeArray(alias)])
- );
+ this.aliases = [...this.aliases, ...normalizeArray(alias)];
return this;
}
setPrefixSupport(support: boolean) {
- Reflect.set(this, "prefix_support", support);
+ this.prefix_support = support;
return this;
}
setSlashSupport(support: boolean) {
- Reflect.set(this, "slash_support", support);
+ this.slash_support = support;
return this;
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| override toJSON(): ApplicationJSONBody { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return super.toJSON() as ApplicationJSONBody; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+45
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: The override casts 🐛 Proposed fix to include custom fields override toJSON(): ApplicationJSONBody {
- return super.toJSON() as ApplicationJSONBody;
+ return {
+ ...super.toJSON(),
+ prefix_support: this.prefix_support,
+ slash_support: this.slash_support,
+ aliases: this.aliases,
+ };
}🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| toClientJSON( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| client: Client | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): ReturnType<ApplicationCommandBuilder["toJSON"]> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...this.toJSON(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
vrdons marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix placeholder digits in user-facing strings.
The trailing “1/2” looks like accidental placeholders and will surface in UI.
✅ Suggested correction
{ - "command.notfound": "Command not found or disabled1", - "command.disabled": "Command not found or disabled2" + "command.notfound": "Command not found.", + "command.disabled": "Command is disabled." }🤖 Prompt for AI Agents