Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: defer interaction on birthday list command #13

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Update <small>_ January 2024</small>

- fix: defer interaction on birthday list command (28/01/2024)
- fix: handle errors if message doesn't exist for delete logs (28/01/2024)
- fix: clearer logging for the role assignment handler (28/01/2024)
- fix: interaction already replied in faq print-all (28/01/2024)
Expand Down
75 changes: 41 additions & 34 deletions src/commands/utils/birthday/functions/listBirthday.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChatInputCommandInteraction } from 'discord.js';
import { Birthday, makeEmbed } from '../../../../lib';
import { Birthday, Logger, makeEmbed } from '../../../../lib';

const birthdayListEmbed = (fields: Array<any>) => makeEmbed({
title: 'Birthday - Birthday List',
Expand All @@ -8,42 +8,49 @@ const birthdayListEmbed = (fields: Array<any>) => 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<string | Array<any>> = [
['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<string | Array<any>> = [
['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})`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test that if a birthday has no utcDateTime? Does it then just add another array in the month buckets (which it can't due to it being const) or fail in some way?

Copy link
Member Author

@benw202 benw202 Jan 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Birthday will always have a utcDateTime. It is populated in the setBrithday command. To be fair, these birthday commands could do with an update as they're just a port from the old bot.

Perhaps an update can be done in a future PR.

The whole thing is in a try catch, so if it happened it would be caught

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just asking because of the !, since that's implying that there could be no utcDateTime.just don't know how the array is behaving with that, but since it's just copied from the old bot, it's fine with me

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if I remember the linting thought it could not exist. But, its ensured to exist in the other command therefore it does always exist. But in this file it doesn't know that

}
}
}

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 });
}
}
Loading