feat(timezone): Adds timezone as option

This commit is contained in:
Michel Fedde 2025-06-29 21:52:53 +02:00
parent 0b9089ffae
commit b852c06f83
11 changed files with 278 additions and 22 deletions

View file

@ -1,9 +1,10 @@
import {
AutocompleteInteraction,
ChatInputCommandInteraction,
EmbedBuilder,
GuildMember,
GuildMemberRoleManager,
hyperlink,
inlineCode,
InteractionReplyOptions,
MessageFlags,
PermissionFlagsBits,
@ -35,8 +36,8 @@ import {PermissionError} from "../PermissionError";
import {EmbedLibrary, EmbedType} from "../EmbedLibrary";
import {EventHandler} from "../../Events/EventHandler";
import {ElementChangedEvent} from "../../Events/EventClasses/ElementChangedEvent";
import GroupConfiguration from "../../Database/tables/GroupConfiguration";
import Groups from "../../Database/tables/Groups";
import {TimezoneHandler, TimezoneSaveTarget} from "../../Configuration/TimezoneHandler";
export class GroupCommand implements Command, ChatInteractionCommand, AutocompleteCommand {
private static GOODBYE_MESSAGES: string[] = [
@ -95,6 +96,16 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
.setDescription("The member, that is the new leader")
.setRequired(true)
)
)
.addSubcommand(command => command
.setName("timezone")
.setDescription("Sets the timezone for the group, if a value is provided. If not, the current timezone is displayed.")
.addIntegerOption(GroupSelection.createOptionSetup())
.addStringOption(option => option
.setName('timezone')
.setDescription("The timezone the group should use.")
.setRequired(false)
)
);
}
@ -115,6 +126,9 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
case "transfer":
await this.transferLeadership(interaction);
break;
case "timezone":
await this.handleTimezone(interaction);
break;
default:
throw new Error("Unsupported command");
}
@ -395,4 +409,51 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
flags: MessageFlags.Ephemeral,
})
}
private async handleTimezone(interaction: ChatInputCommandInteraction) {
const group = GroupSelection.getGroup(interaction);
const enteredTimezone = interaction.options.getString('timezone');
const timezoneHandler = new TimezoneHandler(
interaction.guildId ?? '',
group
);
if (!enteredTimezone) {
await interaction.reply(
{
embeds: [
EmbedLibrary.withGroup(
group,
"Timezone",
`The group currently uses the timezone ${inlineCode(timezoneHandler.getCurrentTimezone())}.`,
EmbedType.Info
)
]
}
)
return;
}
if (!TimezoneHandler.ALL_TIMEZONES.includes(enteredTimezone)) {
throw new UserError(
`Invalid timezone provided: ${enteredTimezone}`,
`Try using timezones found in this list using the 'TZ Identifier'. ${hyperlink("List", 'https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List')}`
)
}
timezoneHandler.save(enteredTimezone, TimezoneSaveTarget.Group);
await interaction.reply(
{
embeds: [
EmbedLibrary.withGroup(
group,
"Timezone changed",
`The group now uses the timezone ${inlineCode(enteredTimezone)}.`,
EmbedType.Info
)
]
}
)
}
}