From 593645d54bec810d3bd1468d36650bc7d45656a1 Mon Sep 17 00:00:00 2001 From: Robin <5983417+rorro@users.noreply.github.com> Date: Sun, 21 Jul 2024 20:52:58 +0200 Subject: [PATCH] Improve hidden group and competition embeds (#257) --- .../instances/HiddenCompetitionCreated.ts | 9 +-- src/events/instances/HiddenGroupCreated.ts | 8 +- src/utils/buttonInteractions.ts | 76 ++++++++++++------- 3 files changed, 56 insertions(+), 37 deletions(-) diff --git a/src/events/instances/HiddenCompetitionCreated.ts b/src/events/instances/HiddenCompetitionCreated.ts index affa0b55..d28bbfcd 100644 --- a/src/events/instances/HiddenCompetitionCreated.ts +++ b/src/events/instances/HiddenCompetitionCreated.ts @@ -12,16 +12,15 @@ class HiddenCompetitionCreated implements Event { } async execute(data: CompetitionListItem, client: Client) { - const { id, title, groupId, participantCount } = data['competition']; - + const { id, title, metric, groupId, participantCount } = data['competition']; const actions = createModerationButtons(ModerationType.COMPETITION, id); const message = new EmbedBuilder() .setColor(config.visuals.blue) - .setTitle(`A hidden competition was created`) + .setTitle(`Hidden competition created`) .setDescription( - `Id: ${id}\nTitle: ${title}\nParticipants: ${participantCount}\nGroup Id: ${ - groupId ? '[' + groupId + '](https://wiseoldman.net/groups/' + groupId + ')' : groupId + `Id: ${id}\nTitle: ${title}\nMetric: ${metric}\nParticipants: ${participantCount}${ + groupId ? `\nGroup Id: [${groupId}](https://wiseoldman.net/groups/${groupId})` : '' }` ) .setURL(encodeURL(`https://wiseoldman.net/competitions/${id}`)); diff --git a/src/events/instances/HiddenGroupCreated.ts b/src/events/instances/HiddenGroupCreated.ts index 5468e8de..bb05c92e 100644 --- a/src/events/instances/HiddenGroupCreated.ts +++ b/src/events/instances/HiddenGroupCreated.ts @@ -13,14 +13,16 @@ class HiddenGroupCreated implements Event { } async execute(data: GroupListItem, client: Client) { - const { id, name, description, memberCount } = data['group']; + const { id, name, description, clanChat, memberCount } = data['group']; const actions = createModerationButtons(ModerationType.GROUP, id); const message = new EmbedBuilder() .setColor(config.visuals.blue) - .setTitle(`A hidden group was created`) - .setDescription(`Id: ${id}\nName: ${name}\nDescription: ${description}\nMembers: ${memberCount}`) + .setTitle(`Hidden group created`) + .setDescription( + `Id: ${id}\nName: ${name}\nDescription: ${description}\nClan chat: ${clanChat}\nMembers: ${memberCount}` + ) .setURL(encodeURL(`https://wiseoldman.net/groups/${id}`)); const reviewChannel = client.channels?.cache.get(config.discord.channels.underAttackModeFeed); diff --git a/src/utils/buttonInteractions.ts b/src/utils/buttonInteractions.ts index 3a7980a8..249a4dd0 100644 --- a/src/utils/buttonInteractions.ts +++ b/src/utils/buttonInteractions.ts @@ -13,7 +13,6 @@ import { setCompetitionVisible, setGroupVisible } from '../services/wiseoldman'; -import { CommandError } from './commands'; enum Actions { DELETE = 'delete', @@ -29,7 +28,7 @@ export enum ModerationType { export async function handleButtonInteraction(interaction: ButtonInteraction): Promise { const [action, type, id, confirmation] = interaction.customId.split('/'); - console.log(type, action, id, confirmation); + let notFound; if (action === PATREON_TRIGGER_ID) { handlePatreonTrigger(interaction); @@ -42,21 +41,21 @@ export async function handleButtonInteraction(interaction: ButtonInteraction): P if (confirmation === Actions.DELETE) { try { await deleteGroup(parseInt(id)).catch(e => { - if (e.statusCode === 404) throw new CommandError('Group not found.'); - throw e; + if (e.statusCode === 404) notFound = true; + else throw e; }); } catch (error) { - await interaction.reply({ ephemeral: true, content: `${error}` }); + await interaction.reply({ ephemeral: false, content: `${error}` }); return; } } else if (confirmation === Actions.APPROVE) { try { await setGroupVisible(parseInt(id)).catch(e => { - if (e.statusCode === 404) throw new CommandError('Group not found.'); - throw e; + if (e.statusCode === 404) notFound = true; + else throw e; }); } catch (error) { - await interaction.reply({ ephemeral: true, content: `${error}` }); + await interaction.reply({ ephemeral: false, content: `${error}` }); return; } } @@ -64,41 +63,30 @@ export async function handleButtonInteraction(interaction: ButtonInteraction): P if (confirmation === Actions.DELETE) { try { await deleteCompetition(parseInt(id)).catch(e => { - if (e.statusCode === 404) throw new CommandError('Competition not found.'); - throw e; + if (e.statusCode === 404) notFound = true; + else throw e; }); } catch (error) { - await interaction.reply({ ephemeral: true, content: `${error}` }); + await interaction.reply({ ephemeral: false, content: `${error}` }); return; } } else if (confirmation === Actions.APPROVE) { try { await setCompetitionVisible(parseInt(id)).catch(e => { - if (e.statusCode === 404) throw new CommandError('Competition not found.'); - throw e; + if (e.statusCode === 404) notFound = true; + else throw e; }); } catch (error) { - await interaction.reply({ ephemeral: true, content: `${error}` }); + await interaction.reply({ ephemeral: false, content: `${error}` }); return; } } } - const message = interaction.message; - const oldEmbed = message.embeds[0]; - - const editedEmbed = new EmbedBuilder() - .setTitle(oldEmbed.title) - .setDescription(oldEmbed.description) - .setURL(oldEmbed.url) - .setFooter({ - text: `${confirmation == Actions.DELETE ? 'Deleted ' : 'Approved '} by ${ - interaction.user.username - }` - }) - .setColor(confirmation == Actions.DELETE ? config.visuals.red : config.visuals.green); - - interaction.update({ embeds: [editedEmbed], components: [] }); + interaction.update({ + embeds: [await updateEmbed(confirmation, type, interaction, notFound)], + components: [] + }); } else if (action === Actions.CANCEL) { await interaction.update({ components: [createModerationButtons(type as ModerationType, parseInt(id))] @@ -106,6 +94,36 @@ export async function handleButtonInteraction(interaction: ButtonInteraction): P } } +async function updateEmbed( + confirmation: string, + type: string, + interaction: ButtonInteraction, + notFound: boolean +) { + const message = interaction.message; + const oldEmbed = message.embeds[0]; + + const editedEmbed = new EmbedBuilder() + .setTitle(oldEmbed.title) + .setDescription(oldEmbed.description) + .setURL(oldEmbed.url) + .setColor(confirmation == Actions.DELETE ? config.visuals.red : config.visuals.green); + + notFound + ? editedEmbed + .setFooter({ + text: `${type === ModerationType.GROUP ? 'Group' : 'Competition'} not found` + }) + .setColor(config.visuals.red) + : editedEmbed.setFooter({ + text: `${confirmation == Actions.DELETE ? 'Deleted ' : 'Approved '} by ${ + interaction.user.username + }` + }); + + return editedEmbed; +} + export function createModerationButtons(type: ModerationType, id: number) { const actions = new ActionRowBuilder();