Skip to content

Commit 608261b

Browse files
feat: birthday upgrades (#69 nice)
* updates * make user optional
1 parent af64d5a commit 608261b

File tree

6 files changed

+109
-77
lines changed

6 files changed

+109
-77
lines changed

package-lock.json

+59-59
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/BirthdayCommand.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from "discord.js";
77
import cron from "node-cron";
88
import { Command } from "../interactions/interactionClasses";
9-
import { myBirthday, setBirthday } from "../util/birthday/manageBirthday";
9+
import { getBirthday, setBirthday } from "../util/birthday/manageBirthday";
1010
import { upcomingCommand } from "../util/birthday/upcomingCommand";
1111
import { birthdayMessageTick } from "../util/birthday/loop";
1212
import { birthdayDb } from "../db";
@@ -35,8 +35,8 @@ class BirthdayCommand extends Command {
3535

3636
if (sub == "set") {
3737
await setBirthday(interaction);
38-
} else if (sub == "my") {
39-
await myBirthday(interaction);
38+
} else if (sub == "get") {
39+
await getBirthday(interaction);
4040
} else if (sub == "upcoming") {
4141
await upcomingCommand(interaction);
4242
}
@@ -66,7 +66,12 @@ class BirthdayCommand extends Command {
6666
.addBooleanOption((opt) => opt.setName("delete").setDescription("Delete your birthday entry."))
6767
)
6868
.addSubcommand((option) =>
69-
option.setName("my").setDescription("Show what date is stored for your birthday.")
69+
option
70+
.setName("get")
71+
.setDescription("Show what date is stored for a members birthday. (default: your own)")
72+
.addUserOption((opt) =>
73+
opt.setName("user").setDescription("The user to get the birthday of.").setRequired(false)
74+
)
7075
)
7176
.addSubcommand((option) =>
7277
option.setName("upcoming").setDescription("Lists the upcoming birthdays in the next 30 days.")

src/util/birthday/loop.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ export async function birthdayMessageTick(client: Client) {
1919
return null;
2020
});
2121
if (!guildUser) continue;
22-
const birthYear = birthdayDb.get(user)!.year;
23-
const age = birthYear == 0 ? null : date.year - birthYear;
22+
const age = getAge(date);
2423
const embed = addEmbedFooter(
2524
new EmbedBuilder()
2625
.setTitle(`🎉🎊🎆 Happy Birthday ${guildUser.displayName}!!! 🎈🎇🎉`)
@@ -37,3 +36,7 @@ export async function birthdayMessageTick(client: Client) {
3736
else logger.warn("Failed to send birthday message because system channel isn't set!");
3837
}
3938
}
39+
40+
export function getAge(date: DateTime): number | null {
41+
return date.year == 0 ? null : Math.floor(date.diffNow().as("years") * -1);
42+
}

src/util/birthday/manageBirthday.ts

+27-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { ChatInputCommandInteraction, inlineCode } from "discord.js";
22
import { birthdayDb } from "../../db";
33
import { DateTime } from "luxon";
44
import { getTimezoneFromRole } from "../misc/role";
5-
import { longDateFormatWithTimezone } from "../misc/time";
5+
import { longDateFormatWithTimezone, shortDateFormatWithTimezone } from "../misc/time";
66
import { config } from "../../config";
7+
import { getAge } from "./loop";
78

89
export async function setBirthday(interaction: ChatInputCommandInteraction) {
910
const oldDate = birthdayDb.get(interaction.user.id);
@@ -55,9 +56,9 @@ export async function setBirthday(interaction: ChatInputCommandInteraction) {
5556

5657
if (oldDate) {
5758
await interaction.reply({
58-
content: `Changed your birthday from *${oldDate.toFormat(
59+
content: `Changed your birthday from ${oldDate.toFormat(
5960
longDateFormatWithTimezone
60-
)}* to **${date.toFormat(longDateFormatWithTimezone)}**!`,
61+
)} to **${date.toFormat(longDateFormatWithTimezone)}**!`,
6162
});
6263
} else {
6364
await interaction.reply({
@@ -66,18 +67,36 @@ export async function setBirthday(interaction: ChatInputCommandInteraction) {
6667
}
6768
}
6869

69-
export async function myBirthday(interaction: ChatInputCommandInteraction) {
70-
if (!birthdayDb.has(interaction.user.id)) {
70+
export async function getBirthday(interaction: ChatInputCommandInteraction) {
71+
const user = interaction.options.getUser("user") ?? interaction.user;
72+
const member = interaction.guild!.members.cache.get(user.id);
73+
74+
if (!member) {
75+
await interaction.reply({
76+
content: "This user is not on this server!",
77+
ephemeral: true,
78+
});
79+
return;
80+
}
81+
82+
const isUserItself = interaction.user.id == user.id;
83+
84+
if (!birthdayDb.has(user.id)) {
7185
await interaction.reply({
72-
content: "You haven't set your birthday yet!\nYou can do this by using `/birthday set`",
86+
content: isUserItself
87+
? "You don't have a birthday set yet!\nYou can set it with `/birthday set`!"
88+
: `${member.displayName} hasn't set their birthday yet!`,
7389
ephemeral: true,
7490
});
7591
return;
7692
}
7793

78-
const date = birthdayDb.get(interaction.user.id)!;
94+
const date = birthdayDb.get(user.id)!;
95+
const age = getAge(date);
7996

8097
await interaction.reply({
81-
content: `Your birthday is set for the **${date.toFormat(longDateFormatWithTimezone)}**!`,
98+
content: `${isUserItself ? "Your" : member.displayName + "s"} birthday is set for the ${date.toFormat(
99+
age != null ? longDateFormatWithTimezone : shortDateFormatWithTimezone
100+
)}!${age != null ? ` That means ${isUserItself ? "you" : "they"} are ${age} years old!` : ""}`,
82101
});
83102
}

src/util/birthday/upcomingCommand.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { ChatInputCommandInteraction, EmbedBuilder } from "discord.js";
22
import { birthdayDb } from "../../db";
33
import { addEmbedColor } from "../misc/embeds";
44
import { DateTime } from "luxon";
5-
import { longDateFormatWithTimezone } from "../misc/time";
5+
import { longDateFormatWithTimezone, shortDateFormatWithTimezone } from "../misc/time";
6+
import { getAge } from "./loop";
67

78
export async function upcomingCommand(interaction: ChatInputCommandInteraction) {
89
const now = DateTime.now();
@@ -24,9 +25,12 @@ export async function upcomingCommand(interaction: ChatInputCommandInteraction)
2425
return daysA - daysB;
2526
});
2627

27-
const answer = entries.map(
28-
(entry) => `${entry.date.toFormat(longDateFormatWithTimezone)} ⁘ <@${entry.user}>`
29-
);
28+
const answer = entries.map((entry) => {
29+
const age = getAge(entry.date);
30+
return `${entry.date.toFormat(
31+
entry.date.year != 0 ? longDateFormatWithTimezone : shortDateFormatWithTimezone
32+
)}${age ? ` (${age + 1}th)` : ""} ⁘ <@${entry.user}>`;
33+
});
3034

3135
if (entries.length == 0) {
3236
const embed = new EmbedBuilder()

src/util/misc/time.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export function durationToReadable(duration: Duration, short = false): string {
1818
}
1919

2020
export const longDateFormatWithTimezone = "dd MMMM yyyy 'UTC'Z";
21+
export const shortDateFormatWithTimezone = "dd MMMM 'UTC'Z";
2122
export const longDateTimeFormat = "dd.MM.yyyy HH:mm:ss 'UTC'Z";
2223
export const shortDateTimeFormat = "dd.MM.yyyy HH:mm 'UTC'Z";
2324
export const longTimeFormat = "HH:mm:ss 'UTC'Z";

0 commit comments

Comments
 (0)