Skip to content

Commit

Permalink
Improve hidden group and competition embeds (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
rorro authored Jul 21, 2024
1 parent 9794c9d commit 593645d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
9 changes: 4 additions & 5 deletions src/events/instances/HiddenCompetitionCreated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
Expand Down
8 changes: 5 additions & 3 deletions src/events/instances/HiddenGroupCreated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
76 changes: 47 additions & 29 deletions src/utils/buttonInteractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
setCompetitionVisible,
setGroupVisible
} from '../services/wiseoldman';
import { CommandError } from './commands';

enum Actions {
DELETE = 'delete',
Expand All @@ -29,7 +28,7 @@ export enum ModerationType {

export async function handleButtonInteraction(interaction: ButtonInteraction): Promise<void> {
const [action, type, id, confirmation] = interaction.customId.split('/');
console.log(type, action, id, confirmation);
let notFound;

if (action === PATREON_TRIGGER_ID) {
handlePatreonTrigger(interaction);
Expand All @@ -42,70 +41,89 @@ 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;
}
}
} else if (type === ModerationType.COMPETITION) {
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))]
});
}
}

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<ButtonBuilder>();

Expand Down

0 comments on commit 593645d

Please sign in to comment.