Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from Qreepex/main
Browse files Browse the repository at this point in the history
v1.4.1
  • Loading branch information
Qreepex authored Jun 12, 2022
2 parents 93bb201 + cb8bb6e commit 51e0606
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 19 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eazyautodelete/eazyautodelete-core",
"version": "1.4.0",
"version": "1.5.2",
"description": "🧰 Core Package used by the EazyAutodelete Discord Bot",
"main": "build/index.js",
"scripts": {
Expand All @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ 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 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";
Expand Down
19 changes: 19 additions & 0 deletions src/structures/ButtonArgs.ts
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;
}
}
17 changes: 9 additions & 8 deletions src/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "./Args";

export default class Command {
client: Bot;
Expand Down Expand Up @@ -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<void> {
interaction.error("An Error occured - Please contact staff: Core.Command.run");
async run(client: Bot, message: CommandMessage, args: CommandArgs): Promise<void> {
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 +
" ]"
);
}
Expand All @@ -132,9 +133,9 @@ export default class Command {
return;
}

async buttonHandler(interaction: ButtonInteraction): Promise<void> {
async buttonHandler(button: CommandButton, args: ButtonArgs): Promise<void> {
this.Logger.warn(
"Ended up in command.js [ " + this.config.name + " - " + interaction.id + " ]"
"Ended up in command.js [ " + this.config.name + " - " + button.id + " ]"
);

return;
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Args.ts → src/structures/CommandArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
145 changes: 145 additions & 0 deletions src/structures/CommandButton.ts
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;
}
}
15 changes: 8 additions & 7 deletions src/structures/CommandMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,7 +54,9 @@ export default class CommandMessage {
public async error(message: string, ...args: string[]): Promise<CommandMessage> {
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) {
Expand All @@ -70,22 +71,22 @@ export default class CommandMessage {
ephemeral: boolean | undefined = false,
components: MessageActionRow | MessageActionRow[] = []
): Promise<CommandMessage> {
// 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)],
ephemeral,
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;
}

Expand Down

0 comments on commit 51e0606

Please sign in to comment.