This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Qreepex/main
v1.4.1
- Loading branch information
Showing
7 changed files
with
188 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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,19 @@ | ||
import CommandButton from "./CommandButton"; | ||
|
||
export default class ButtonArgs { | ||
command: string; | ||
button: CommandButton; | ||
|
||
constructor(button: CommandButton) { | ||
this.button = button; | ||
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; | ||
} | ||
} |
This file contains 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 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 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,145 @@ | ||
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<CommandButton> { | ||
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<CommandButton> { | ||
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<CommandButton> { | ||
try { | ||
await this.interaction.followUp({ | ||
ephemeral: true, | ||
content: emoji, | ||
}); | ||
} catch (e) { | ||
this.Logger.error(e as string); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
async delete(): Promise<CommandButton | void> { | ||
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<CommandButton> { | ||
try { | ||
await this.interaction.editReply(payload).catch(this.Logger.error); | ||
} catch (e) { | ||
this.Logger.error(e as string); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
async continue(ephemeral: boolean = true): Promise<CommandButton> { | ||
try { | ||
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); | ||
} | ||
|
||
return this; | ||
} | ||
} |
This file contains 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