From d9901cb8a51c463462bea389851a6d34af2d9584 Mon Sep 17 00:00:00 2001 From: Qreepex <65496825+Qreepex@users.noreply.github.com> Date: Sun, 12 Jun 2022 12:36:43 +0200 Subject: [PATCH 1/4] feat(buttons): add CommandButton & ButtonArgs add classes to handle button interactions (args & interactions it self) BREAKING CHANGE: rename Args to CommandArgs --- package.json | 3 +- src/index.ts | 3 +- src/structures/ButtonArgs.ts | 15 +++ src/structures/Command.ts | 2 +- src/structures/{Args.ts => CommandArgs.ts} | 2 +- src/structures/CommandButton.ts | 135 +++++++++++++++++++++ src/structures/CommandMessage.ts | 15 +-- 7 files changed, 164 insertions(+), 11 deletions(-) create mode 100644 src/structures/ButtonArgs.ts rename src/structures/{Args.ts => CommandArgs.ts} (98%) create mode 100644 src/structures/CommandButton.ts diff --git a/package.json b/package.json index fc2d1a5..4af49ad 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "lint:typings": "eslint typings/index.d.ts", "test": "npm run lint && npm run lint:typings && npm run test:typescript", "test:typescript": "tsc", - "build": "npm run format & tsc" + "build": "npm run format & tsc", + "publish": "git push --follow-tags origin main && npm publish" }, "files": [ "build/*.js", diff --git a/src/index.ts b/src/index.ts index 36674e8..35635ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,8 +4,9 @@ import { MessageEmbedOptions, } from "discord.js"; -export { default as Args } from "./structures/Args"; export { default as Bot } from "./structures/Bot"; +export { default as ButtonArgs } from "./structures/ButtonArgs"; +export { default as CommandArgs } from "./structures/CommandArgs"; export { default as Command } from "./structures/Command"; export { default as CommandMessage } from "./structures/CommandMessage"; export { default as CommandResponseHandler } from "./structures/discord/CommandResponseHandler"; diff --git a/src/structures/ButtonArgs.ts b/src/structures/ButtonArgs.ts new file mode 100644 index 0000000..b0cbac3 --- /dev/null +++ b/src/structures/ButtonArgs.ts @@ -0,0 +1,15 @@ +import CommandButton from "./CommandButton"; + +export default class ButtonArgs { + command: string; + button: CommandButton; + + constructor(button: CommandButton) { + this.button = button; + this.command = this.getCommand(); + } + + getCommand(): string { + return this.button.interaction.customId.split("_")[1] as string; + } +} diff --git a/src/structures/Command.ts b/src/structures/Command.ts index f1ced86..e5e9bfe 100644 --- a/src/structures/Command.ts +++ b/src/structures/Command.ts @@ -10,7 +10,7 @@ import { CommandConfig, CommandData, CommandHelp, CommandOptions } from "../"; import Bot from "./Bot"; import Logger from "./Logger"; import CommandMessage from "./CommandMessage"; -import Args from "./Args"; +import Args from "./CommandArgs"; export default class Command { client: Bot; diff --git a/src/structures/Args.ts b/src/structures/CommandArgs.ts similarity index 98% rename from src/structures/Args.ts rename to src/structures/CommandArgs.ts index 6ddb66c..42507db 100644 --- a/src/structures/Args.ts +++ b/src/structures/CommandArgs.ts @@ -2,7 +2,7 @@ import Discord from "discord.js"; import CommandMessage from "./CommandMessage.js"; import { ms } from "@eazyautodelete/bot-utils"; -export default class Args { +export default class CommandArgs { command: string; message: CommandMessage; diff --git a/src/structures/CommandButton.ts b/src/structures/CommandButton.ts new file mode 100644 index 0000000..8a4c1ba --- /dev/null +++ b/src/structures/CommandButton.ts @@ -0,0 +1,135 @@ +import Discord, { MessageActionRow, MessageEmbed, MessageEmbedOptions } from "discord.js"; +import Bot from "./Bot"; +import Logger from "./Logger"; +import { UserSettings, GuildSettings } from "@eazyautodelete/eazyautodelete-db-client"; +import { Locale } from "@eazyautodelete/eazyautodelete-lang"; + +export default class CommandButton { + channel!: Discord.TextBasedChannel; + member: Discord.GuildMember; + author: Discord.User; + client: Bot; + guild!: Discord.Guild; + id: Discord.Snowflake; + createdTimestamp: number; + locale: string; + Logger: Logger; + data!: { guild: GuildSettings; user: UserSettings }; + message: Discord.Message; + interaction: Discord.ButtonInteraction; + + constructor(interaction: Discord.ButtonInteraction, client: Bot) { + this.client = client; + this.message = interaction.message as Discord.Message; + this.interaction = interaction; + if (interaction.channel) this.channel = interaction.channel; + if (interaction.guild) this.guild = interaction.guild; + this.id = interaction.id; + this.createdTimestamp = interaction.createdTimestamp; + this.member = this.interaction.member as Discord.GuildMember; + this.author = this.interaction.user as Discord.User; + this.locale = this.interaction.locale; + this.Logger = client.Logger; + } + + public async loadData() { + this.data = { + guild: await this.client.database.getGuildSettings( + this.message.guild?.id as string + ), + user: await this.client.database.getUserSettings(this.message.id), + }; + } + + public translate(phrase: string, ...replace: string[]): string { + const data = this.client.translate( + { + phrase: phrase, + locale: (this.data.user.language as Locale) || "en", + }, + ...replace + ); + + return data || phrase; + } + + public async error(message: string, ...args: string[]): Promise { + try { + await this.send( + new MessageEmbed({ + description: this.translate(message, ...args) || message, + }).setColor("#ff0000"), + true + ).catch(this.Logger.error); + } catch (e) { + this.Logger.error(e as string); + } + + return this; + } + + public async send( + message: MessageEmbed | MessageEmbed[] | MessageEmbedOptions | MessageEmbedOptions[], + ephemeral: boolean | undefined = false, + components: MessageActionRow | MessageActionRow[] = [] + ): Promise { + try { + await this.client.response + .send( + this.interaction, + Array.isArray(message) + ? message.map((m: MessageEmbed | MessageEmbedOptions) => { + return m instanceof MessageEmbed ? m : new MessageEmbed(m); + }) + : [message instanceof MessageEmbed ? message : new MessageEmbed(message)], + ephemeral, + Array.isArray(components) ? components : [components] + ) + .catch(this.Logger.error); + } catch (e) { + this.Logger.error(e as string); + } + return this; + } + + async react(emoji: string): Promise { + try { + await this.interaction.followUp({ + ephemeral: true, + content: emoji, + }); + } catch (e) { + this.Logger.error(e as string); + } + + return this; + } + + async delete(): Promise { + try { + return await this.interaction.deleteReply().catch(this.Logger.error); + } catch (e) { + return this.Logger.error(e as string); + } + } + + async edit(payload: Discord.InteractionReplyOptions): Promise { + try { + await this.interaction.editReply(payload).catch(this.Logger.error); + } catch (e) { + this.Logger.error(e as string); + } + + return this; + } + + async continue(): Promise { + try { + await this.interaction.deferReply().catch(this.Logger.error); + } catch (e) { + this.Logger.error(e as string); + } + + return this; + } +} diff --git a/src/structures/CommandMessage.ts b/src/structures/CommandMessage.ts index 42598d5..1812ffb 100644 --- a/src/structures/CommandMessage.ts +++ b/src/structures/CommandMessage.ts @@ -3,7 +3,6 @@ import Bot from "./Bot"; import Logger from "./Logger"; import { UserSettings, GuildSettings } from "@eazyautodelete/eazyautodelete-db-client"; import { Locale } from "@eazyautodelete/eazyautodelete-lang"; -import { ResponseData } from "../"; export default class CommandMessage { message: Discord.CommandInteraction; @@ -55,7 +54,9 @@ export default class CommandMessage { public async error(message: string, ...args: string[]): Promise { try { await this.send( - new MessageEmbed({ description: this.translate(message, ...args) || message }).setColor("#ff0000"), + new MessageEmbed({ + description: this.translate(message, ...args) || message, + }).setColor("#ff0000"), true ).catch(this.Logger.error); } catch (e) { @@ -70,12 +71,12 @@ export default class CommandMessage { ephemeral: boolean | undefined = false, components: MessageActionRow | MessageActionRow[] = [] ): Promise { - // try { + try { await this.client.response .send( this.message, Array.isArray(message) - ? message.map(m => { + ? message.map((m: MessageEmbed | MessageEmbedOptions) => { return m instanceof MessageEmbed ? m : new MessageEmbed(m); }) : [message instanceof MessageEmbed ? message : new MessageEmbed(message)], @@ -83,9 +84,9 @@ export default class CommandMessage { Array.isArray(components) ? components : [components] ) .catch(this.Logger.error); - // } catch (e) { - // this.Logger.error(e as string); - // } + } catch (e) { + this.Logger.error(e as string); + } return this; } From 73a198996bfc1049ebeb75573f46738469082179 Mon Sep 17 00:00:00 2001 From: Qreepex <65496825+Qreepex@users.noreply.github.com> Date: Sun, 12 Jun 2022 12:37:34 +0200 Subject: [PATCH 2/4] v --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4af49ad..4f85dee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@eazyautodelete/eazyautodelete-core", - "version": "1.4.0", + "version": "1.4.1", "description": "🧰 Core Package used by the EazyAutodelete Discord Bot", "main": "build/index.js", "scripts": { From a7b28b9f11a076eca17ad63e987e963e9a2f45c4 Mon Sep 17 00:00:00 2001 From: Qreepex <65496825+Qreepex@users.noreply.github.com> Date: Sun, 12 Jun 2022 13:05:34 +0200 Subject: [PATCH 3/4] fix: fix something ig --- package.json | 2 +- src/index.ts | 1 + src/structures/Command.ts | 17 +++++++++-------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 4f85dee..125ac15 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@eazyautodelete/eazyautodelete-core", - "version": "1.4.1", + "version": "1.5.1", "description": "🧰 Core Package used by the EazyAutodelete Discord Bot", "main": "build/index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index 35635ac..d2f17a5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ export { default as Bot } from "./structures/Bot"; export { default as ButtonArgs } from "./structures/ButtonArgs"; export { default as CommandArgs } from "./structures/CommandArgs"; export { default as Command } from "./structures/Command"; +export { default as CommandButton } from "./structures/CommandButton"; export { default as CommandMessage } from "./structures/CommandMessage"; export { default as CommandResponseHandler } from "./structures/discord/CommandResponseHandler"; export { default as Logger } from "./structures/Logger"; diff --git a/src/structures/Command.ts b/src/structures/Command.ts index e5e9bfe..6b6208c 100644 --- a/src/structures/Command.ts +++ b/src/structures/Command.ts @@ -3,14 +3,15 @@ import { MessageActionRow, MessageButton, SelectMenuInteraction, - ButtonInteraction, ApplicationCommandOptionChoiceData, } from "discord.js"; import { CommandConfig, CommandData, CommandHelp, CommandOptions } from "../"; import Bot from "./Bot"; +import ButtonArgs from "./ButtonArgs"; import Logger from "./Logger"; +import CommandArgs from "./CommandArgs"; +import CommandButton from "./CommandButton"; import CommandMessage from "./CommandMessage"; -import Args from "./CommandArgs"; export default class Command { client: Bot; @@ -100,16 +101,16 @@ export default class Command { return this.urlButton("https://docs.eazyautodelete.xyz/" + url, "Help", "❓"); } - async run(client: Bot, interaction: CommandMessage, args: Args): Promise { - interaction.error("An Error occured - Please contact staff: Core.Command.run"); + async run(client: Bot, message: CommandMessage, args: CommandArgs): Promise { + message.error("An Error occured - Please contact staff: Core.Command.run"); return this.Logger.warn( "Ended up in command.js [ " + this.config.name + " - " + - interaction.guild?.id + + message.guild?.id + " - " + - interaction.channel?.id + + message.channel?.id + " ]" ); } @@ -132,9 +133,9 @@ export default class Command { return; } - async buttonHandler(interaction: ButtonInteraction): Promise { + async buttonHandler(button: CommandButton, args: ButtonArgs): Promise { this.Logger.warn( - "Ended up in command.js [ " + this.config.name + " - " + interaction.id + " ]" + "Ended up in command.js [ " + this.config.name + " - " + button.id + " ]" ); return; From cb8bb6e0ec802d5c498759ea5bb5ef6f8229f8fb Mon Sep 17 00:00:00 2001 From: Qreepex <65496825+Qreepex@users.noreply.github.com> Date: Sun, 12 Jun 2022 13:18:04 +0200 Subject: [PATCH 4/4] feat(buttons): add deferupdate to commandbutton --- package.json | 2 +- src/structures/ButtonArgs.ts | 4 ++++ src/structures/CommandButton.ts | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 125ac15..f67233b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@eazyautodelete/eazyautodelete-core", - "version": "1.5.1", + "version": "1.5.2", "description": "🧰 Core Package used by the EazyAutodelete Discord Bot", "main": "build/index.js", "scripts": { diff --git a/src/structures/ButtonArgs.ts b/src/structures/ButtonArgs.ts index b0cbac3..dd88093 100644 --- a/src/structures/ButtonArgs.ts +++ b/src/structures/ButtonArgs.ts @@ -9,6 +9,10 @@ export default class ButtonArgs { this.command = this.getCommand(); } + isCommand(): boolean { + return this.button.interaction.customId?.startsWith("cmd_") || false; + } + getCommand(): string { return this.button.interaction.customId.split("_")[1] as string; } diff --git a/src/structures/CommandButton.ts b/src/structures/CommandButton.ts index 8a4c1ba..ea8b4ee 100644 --- a/src/structures/CommandButton.ts +++ b/src/structures/CommandButton.ts @@ -123,9 +123,19 @@ export default class CommandButton { return this; } - async continue(): Promise { + async continue(ephemeral: boolean = true): Promise { try { - await this.interaction.deferReply().catch(this.Logger.error); + await this.interaction.deferReply({ ephemeral: ephemeral}).catch(this.Logger.error); + } catch (e) { + this.Logger.error(e as string); + } + + return this; + } + + async deferUpdate() { + try { + await this.interaction.deferUpdate().catch(this.Logger.error); } catch (e) { this.Logger.error(e as string); }