Skip to content

Commit

Permalink
refactor(decorators): decorated command methods now utilize this cl…
Browse files Browse the repository at this point in the history
…ass property
  • Loading branch information
dev-737 committed Jul 26, 2024
1 parent c36f01e commit 8af50e7
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 132 deletions.
53 changes: 24 additions & 29 deletions src/commands/context-menu/blacklist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { stripIndents } from 'common-tags';
import {
type ModalSubmitInteraction,
type RESTPostAPIApplicationCommandsJSONBody,
ActionRowBuilder,
ApplicationCommandType,
ButtonBuilder,
Expand All @@ -7,24 +10,21 @@ import {
MessageComponentInteraction,
MessageContextMenuCommandInteraction,
ModalBuilder,
ModalSubmitInteraction,
RESTPostAPIApplicationCommandsJSONBody,
TextInputBuilder,
TextInputStyle,
time,
} from 'discord.js';
import db from '../../utils/Db.js';
import parse from 'parse-duration';
import BaseCommand from '../../core/BaseCommand.js';
import { t } from '../../utils/Locale.js';
import { RegisterInteractionHandler } from '../../decorators/Interaction.js';
import { deleteConnections } from '../../utils/ConnectedList.js';
import { colors, emojis } from '../../utils/Constants.js';
import { CustomID } from '../../utils/CustomID.js';
import { RegisterInteractionHandler } from '../../decorators/Interaction.js';
import { checkIfStaff, getUserLocale, simpleEmbed } from '../../utils/Utils.js';
import { stripIndents } from 'common-tags';
import db from '../../utils/Db.js';
import { logBlacklist } from '../../utils/HubLogger/ModLogs.js';
import { deleteConnections } from '../../utils/ConnectedList.js';
import { t } from '../../utils/Locale.js';
import Logger from '../../utils/Logger.js';
import { checkIfStaff, getUserLocale } from '../../utils/Utils.js';

export default class Blacklist extends BaseCommand {
readonly data: RESTPostAPIApplicationCommandsJSONBody = {
Expand All @@ -48,14 +48,11 @@ export default class Blacklist extends BaseCommand {
const isStaffOrHubMod = checkIfStaff(interaction.user.id) || isHubMod;

if (!messageInDb || !isStaffOrHubMod) {
await interaction.reply({
embeds: [
simpleEmbed(
t({ phrase: 'errors.messageNotSentOrExpired', locale }, { emoji: emojis.info }),
),
],
ephemeral: true,
});
await this.replyEmbed(
interaction,
t({ phrase: 'errors.messageNotSentOrExpired', locale }, { emoji: emojis.info }),
{ ephemeral: true },
);
return;
}

Expand Down Expand Up @@ -106,15 +103,16 @@ export default class Blacklist extends BaseCommand {
}

@RegisterInteractionHandler('blacklist')
static override async handleComponents(interaction: MessageComponentInteraction): Promise<void> {
override async handleComponents(interaction: MessageComponentInteraction): Promise<void> {
const customId = CustomID.parseCustomId(interaction.customId);
const locale = await getUserLocale(interaction.user.id);

if (interaction.user.id !== customId.args[0]) {
await interaction.reply({
embeds: [simpleEmbed(t({ phrase: 'errors.notYourAction', locale }, { emoji: emojis.no }))],
ephemeral: true,
});
await this.replyEmbed(
interaction,
t({ phrase: 'errors.notYourAction', locale }, { emoji: emojis.no }),
{ ephemeral: true },
);
return;
}

Expand Down Expand Up @@ -190,14 +188,11 @@ export default class Blacklist extends BaseCommand {
const user = await interaction.client.users.fetch(originalMsg.authorId).catch(() => null);

if (!user) {
await interaction.reply({
embeds: [
simpleEmbed(
`${emojis.neutral} Unable to fetch user. They may have deleted their account?`,
),
],
ephemeral: true,
});
await this.replyEmbed(
interaction,
`${emojis.neutral} Unable to fetch user. They may have deleted their account?`,
{ ephemeral: true },
);
return;
}

Expand Down
10 changes: 5 additions & 5 deletions src/commands/context-menu/editMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class EditMessage extends BaseCommand {
}

@RegisterInteractionHandler('editMsg')
static async handleModals(interaction: ModalSubmitInteraction): Promise<void> {
async handleModals(interaction: ModalSubmitInteraction): Promise<void> {
await interaction.deferReply({ ephemeral: true });

const customId = CustomID.parseCustomId(interaction.customId);
Expand Down Expand Up @@ -144,7 +144,7 @@ export default class EditMessage extends BaseCommand {
const hubSettings = new HubSettingsBitField(originalMsg.hub.settings);
const newMessage = hubSettings.has('HideLinks') ? replaceLinks(userInput) : userInput;
const { newEmbed, censoredEmbed, compactMsg, censoredCmpctMsg } =
await EditMessage.fabricateNewMsg(interaction.user, target, newMessage, originalMsg.serverId);
await this.fabricateNewMsg(interaction.user, target, newMessage, originalMsg.serverId);

if (hubSettings.has('BlockInvites') && containsInviteLinks(newMessage)) {
await interaction.editReply(
Expand Down Expand Up @@ -201,7 +201,7 @@ export default class EditMessage extends BaseCommand {
);
}

static async getImageUrls(target: Message, newMessage: string) {
private async getImageUrls(target: Message, newMessage: string) {
// get image from embed
// get image from content
const oldImageUrl = target.content
Expand All @@ -211,7 +211,7 @@ export default class EditMessage extends BaseCommand {
return { oldImageUrl, newImageUrl };
}

static async buildNewEmbed(
private async buildNewEmbed(
user: User,
target: Message,
newMessage: string,
Expand Down Expand Up @@ -246,7 +246,7 @@ export default class EditMessage extends BaseCommand {
.setFooter({ text: `Server: ${guild?.name}` });
}

static async fabricateNewMsg(user: User, target: Message, newMessage: string, serverId: string) {
private async fabricateNewMsg(user: User, target: Message, newMessage: string, serverId: string) {
const { oldImageUrl, newImageUrl } = await this.getImageUrls(target, newMessage);
const newEmbed = await this.buildNewEmbed(user, target, newMessage, serverId, {
oldImageUrl,
Expand Down
15 changes: 7 additions & 8 deletions src/commands/context-menu/messageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ export default class MessageInfo extends BaseCommand {
.setThumbnail(`https://cdn.discordapp.com/icons/${server?.id}/${server?.icon}.png`)
.setColor('Random');

const components = MessageInfo.buildButtons(target.id, locale);

const components = this.buildButtons(target.id, locale);
const guildConnected = (await getAllConnections())?.find(
(c) => c.serverId === originalMsg.serverId && c.hubId === originalMsg.hub?.id,
);
Expand All @@ -97,7 +96,7 @@ export default class MessageInfo extends BaseCommand {
}

@RegisterInteractionHandler('msgInfo')
static override async handleComponents(interaction: MessageComponentInteraction) {
override async handleComponents(interaction: MessageComponentInteraction) {
// create a variable to store the profile card buffer
const customId = CustomID.parseCustomId(interaction.customId);
const [messageId] = customId.args;
Expand Down Expand Up @@ -187,7 +186,7 @@ export default class MessageInfo extends BaseCommand {
.setFooter({ text: `ID: ${server.id}` });

// disable the server info button
MessageInfo.greyOutButton(components[0], 1);
this.greyOutButton(components[0], 1);

await interaction.update({ embeds: [serverEmbed], components, files: [] });
break;
Expand Down Expand Up @@ -218,7 +217,7 @@ export default class MessageInfo extends BaseCommand {
.setTimestamp();

// disable the user info button
MessageInfo.greyOutButton(components[0], 2);
this.greyOutButton(components[0], 2);

await interaction.editReply({
// attach the profile card to the message
Expand Down Expand Up @@ -262,7 +261,7 @@ export default class MessageInfo extends BaseCommand {
)
.setColor('Random');

MessageInfo.greyOutButton(components[0], 0);
this.greyOutButton(components[0], 0);

await interaction.update({ embeds: [embed], components, files: [] });
break;
Expand Down Expand Up @@ -352,12 +351,12 @@ export default class MessageInfo extends BaseCommand {
}

// utility methods
static greyOutButton(buttons: ActionRowBuilder<ButtonBuilder>, disableElement: number) {
private greyOutButton(buttons: ActionRowBuilder<ButtonBuilder>, disableElement: number) {
buttons.components.forEach((c) => c.setDisabled(false));
buttons.components[disableElement].setDisabled(true);
}

static buildButtons(messageId: string, locale: supportedLocaleCodes = 'en') {
private buildButtons(messageId: string, locale: supportedLocaleCodes = 'en') {
return [
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
Expand Down
2 changes: 1 addition & 1 deletion src/commands/context-menu/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default class Translate extends BaseCommand {
}

@RegisterInteractionHandler('translate')
static override async handleComponents(interaction: ButtonInteraction): Promise<void> {
override async handleComponents(interaction: ButtonInteraction): Promise<void> {
const modal = new ModalBuilder()
.setCustomId(new CustomID('translate_modal').toString())
.setTitle('Specify Language')
Expand Down
8 changes: 4 additions & 4 deletions src/commands/slash/Information/about.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
ButtonStyle,
Client,
} from 'discord.js';
import { badgeEmojis, emojis, LINKS } from '../../../utils/Constants.js';
import { getCredits, simpleEmbed } from '../../../utils/Utils.js';
import { badgeEmojis, emojis, LINKS } from '#main/utils/Constants.js';
import { getCredits, simpleEmbed } from '#main/utils/Utils.js';
import { stripIndents } from 'common-tags';
import BaseCommand, { CommandBody } from '../../../core/BaseCommand.js';
import BaseCommand, { CmdData } from '#main/core/BaseCommand.js';

export default class About extends BaseCommand {
public readonly data: CommandBody = {
public readonly data: CmdData = {
name: 'about',
description: 'Learn more about the InterChat team and project.',
};
Expand Down
2 changes: 1 addition & 1 deletion src/commands/slash/Information/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class Stats extends BaseCommand {
}

@RegisterInteractionHandler('stats')
static override async handleComponents(interaction: ButtonInteraction) {
override async handleComponents(interaction: ButtonInteraction) {
const customId = CustomID.parseCustomId(interaction.customId);

const allCusterData = await interaction.client.cluster.broadcastEval((client) =>
Expand Down
6 changes: 3 additions & 3 deletions src/commands/slash/Main/connection/customize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class Customize extends Connection {
}

@RegisterInteractionHandler('connectionModal')
static override async handleModals(interaction: ModalSubmitInteraction): Promise<void> {
async handleModals(interaction: ModalSubmitInteraction): Promise<void> {
const customId = CustomID.parseCustomId(interaction.customId);
const locale = await getUserLocale(interaction.user.id);
if (customId.suffix === 'invite') {
Expand Down Expand Up @@ -150,7 +150,7 @@ export default class Customize extends Connection {
}

@RegisterInteractionHandler('connection')
static async handleStringSelects(interaction: StringSelectMenuInteraction) {
async handleStringSelects(interaction: StringSelectMenuInteraction) {
if (!interaction.isStringSelectMenu()) return;

const customId = CustomID.parseCustomId(interaction.customId);
Expand Down Expand Up @@ -244,7 +244,7 @@ export default class Customize extends Connection {
}

@RegisterInteractionHandler('connection', 'change_channel')
static async handleChannelSelects(interaction: ChannelSelectMenuInteraction) {
async handleChannelSelects(interaction: ChannelSelectMenuInteraction) {
if (!interaction.isChannelSelectMenu()) return;
await interaction.deferUpdate();

Expand Down
2 changes: 1 addition & 1 deletion src/commands/slash/Main/hub/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class Delete extends Hub {
}

@RegisterInteractionHandler('hub_delete')
static override async handleComponents(interaction: ButtonInteraction) {
override async handleComponents(interaction: ButtonInteraction) {
const customId = CustomID.parseCustomId(interaction.customId);
const [userId, hubId] = customId.args;
const locale = await getUserLocale(interaction.user.id);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/slash/Main/hub/leave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class Leave extends Hub {
}

@RegisterInteractionHandler('hub_leave')
static override async handleComponents(interaction: MessageComponentInteraction): Promise<void> {
override async handleComponents(interaction: MessageComponentInteraction): Promise<void> {
const customId = CustomID.parseCustomId(interaction.customId);
const [channelId] = customId.args;

Expand Down
28 changes: 14 additions & 14 deletions src/commands/slash/Main/hub/manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ export default class Manage extends Hub {
}

@RegisterInteractionHandler('hub_manage', 'settingsSelect')
static async handleSettingsSelect(interaction: MessageComponentInteraction) {
async handleSettingsSelect(interaction: MessageComponentInteraction) {
if (!interaction.isStringSelectMenu()) return;

const initialData = await Manage.componentChecks(interaction);
const initialData = await this.componentChecks(interaction);
if (!initialData) return;

const { hubInDb, customId } = initialData;
Expand Down Expand Up @@ -152,10 +152,10 @@ export default class Manage extends Hub {
}

@RegisterInteractionHandler('hub_manage', 'logsSelect')
static async handleLogsSelect(interaction: MessageComponentInteraction) {
async handleLogsSelect(interaction: MessageComponentInteraction) {
if (!interaction.isStringSelectMenu()) return;

const initialData = await Manage.componentChecks(interaction);
const initialData = await this.componentChecks(interaction);
if (!initialData) return;

const { hubInDb, customId, locale } = initialData;
Expand Down Expand Up @@ -254,10 +254,10 @@ export default class Manage extends Hub {
}

@RegisterInteractionHandler('hub_manage', 'actions')
static async handleActionsSelect(interaction: MessageComponentInteraction) {
async handleActionsSelect(interaction: MessageComponentInteraction) {
if (!interaction.isStringSelectMenu()) return;

const initialData = await Manage.componentChecks(interaction);
const initialData = await this.componentChecks(interaction);
if (!initialData) return;

const { hubInDb, customId, locale } = initialData;
Expand Down Expand Up @@ -387,10 +387,10 @@ export default class Manage extends Hub {
}

@RegisterInteractionHandler('hub_manage', 'logsChSel')
static async handleChannelSelects(interaction: MessageComponentInteraction) {
async handleChannelSelects(interaction: MessageComponentInteraction) {
if (!interaction.isChannelSelectMenu()) return;

const initialData = await Manage.componentChecks(interaction);
const initialData = await this.componentChecks(interaction);
if (!initialData) return;

const { hubInDb, customId, locale } = initialData;
Expand Down Expand Up @@ -421,10 +421,10 @@ export default class Manage extends Hub {
}

@RegisterInteractionHandler('hub_manage')
static async handleRoleSelects(interaction: MessageComponentInteraction) {
async handleRoleSelects(interaction: MessageComponentInteraction) {
if (!interaction.isRoleSelectMenu()) return;

const initialData = await Manage.componentChecks(interaction);
const initialData = await this.componentChecks(interaction);
if (!initialData) return;

const { hubInDb, customId, locale } = initialData;
Expand Down Expand Up @@ -469,7 +469,7 @@ export default class Manage extends Hub {
}

@RegisterInteractionHandler('hub_manage_modal')
static override async handleModals(interaction: ModalSubmitInteraction<CacheType>) {
async handleModals(interaction: ModalSubmitInteraction<CacheType>) {
const customId = CustomID.parseCustomId(interaction.customId);
const [hubId] = customId.args;
const locale = await getUserLocale(interaction.user.id);
Expand Down Expand Up @@ -590,10 +590,10 @@ export default class Manage extends Hub {
}

@RegisterInteractionHandler('hub_manage')
static async handleButtons(interaction: MessageComponentInteraction) {
async handleButtons(interaction: MessageComponentInteraction) {
if (!interaction.isButton()) return;

const initialData = await Manage.componentChecks(interaction);
const initialData = await this.componentChecks(interaction);
if (!initialData) return;

const { hubInDb, customId, locale } = initialData;
Expand Down Expand Up @@ -696,7 +696,7 @@ export default class Manage extends Hub {
}
}

static async componentChecks(interaction: MessageComponentInteraction) {
private async componentChecks(interaction: MessageComponentInteraction) {
const customId = CustomID.parseCustomId(interaction.customId);
const locale = await getUserLocale(interaction.user.id);

Expand Down
Loading

0 comments on commit 8af50e7

Please sign in to comment.