From 8ce39488eff73d86241562859f48c5a093c05080 Mon Sep 17 00:00:00 2001 From: Ben Williams Date: Sun, 28 Jan 2024 10:52:30 +0000 Subject: [PATCH] defer interaction on birthday list command --- .github/CHANGELOG.md | 1 + .../utils/birthday/functions/listBirthday.ts | 75 ++++++++++--------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 85c51f19..9455117f 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -2,6 +2,7 @@ Update _ January 2024 +- fix: defer interaction on birthday list command (28/01/2024) - fix: command names (28/01/2024) - fix: ignore Dyno bot (27/01/2024) - fix: use - instead of _ in command names (27/01/2024) diff --git a/src/commands/utils/birthday/functions/listBirthday.ts b/src/commands/utils/birthday/functions/listBirthday.ts index bf650518..82369cad 100644 --- a/src/commands/utils/birthday/functions/listBirthday.ts +++ b/src/commands/utils/birthday/functions/listBirthday.ts @@ -1,5 +1,5 @@ import { ChatInputCommandInteraction } from 'discord.js'; -import { Birthday, makeEmbed } from '../../../../lib'; +import { Birthday, Logger, makeEmbed } from '../../../../lib'; const birthdayListEmbed = (fields: Array) => makeEmbed({ title: 'Birthday - Birthday List', @@ -8,42 +8,49 @@ const birthdayListEmbed = (fields: Array) => makeEmbed({ }); export async function handleListBirthday(interaction: ChatInputCommandInteraction<'cached'>) { - const birthdays = await Birthday.find({}).sort({ day: 1 }); // Only day sort required, months are bucketized - const members = await interaction.guild!.members.fetch(); - - const monthBuckets: Array> = [ - ['January', []], - ['February', []], - ['March', []], - ['April', []], - ['May', []], - ['June', []], - ['July', []], - ['August', []], - ['September', []], - ['October', []], - ['November', []], - ['December', []], - ]; - - for (const birthday of birthdays) { - const member = members.get(birthday.userID!); - - if (member) { - monthBuckets[birthday.utcDatetime!.getUTCMonth()][1].push(`${member.displayName} - ${birthday.day}/${birthday.month} (Z${birthday.timezone! < 0 ? '' : '+'}${birthday.timezone})`); + await interaction.deferReply(); + + try { + const birthdays = await Birthday.find({}).sort({ day: 1 }); // Only day sort required, months are bucketized + const members = await interaction.guild!.members.fetch(); + + const monthBuckets: Array> = [ + ['January', []], + ['February', []], + ['March', []], + ['April', []], + ['May', []], + ['June', []], + ['July', []], + ['August', []], + ['September', []], + ['October', []], + ['November', []], + ['December', []], + ]; + + for (const birthday of birthdays) { + const member = members.get(birthday.userID!); + + if (member) { + monthBuckets[birthday.utcDatetime!.getUTCMonth()][1].push(`${member.displayName} - ${birthday.day}/${birthday.month} (Z${birthday.timezone! < 0 ? '' : '+'}${birthday.timezone})`); + } } - } - const fields = []; + const fields = []; - for (const monthBucket of monthBuckets) { - if (monthBucket[1].length > 0) { - fields.push({ - name: monthBucket[0], - value: monthBucket[1].join('\n'), - }); + for (const monthBucket of monthBuckets) { + if (monthBucket[1].length > 0) { + fields.push({ + name: monthBucket[0], + value: monthBucket[1].join('\n'), + }); + } } - } - await interaction.reply({ embeds: [birthdayListEmbed(fields)] }); + await interaction.editReply({ embeds: [birthdayListEmbed(fields)] }); + } catch (error) { + Logger.error(error); + await interaction.followUp({ content: 'An error occurred while processing this command.', ephemeral: true }); + } }