feat(timezone): Adds timezone as option
This commit is contained in:
parent
0b9089ffae
commit
b852c06f83
11 changed files with 278 additions and 22 deletions
|
|
@ -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
|
||||
)
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -29,6 +29,8 @@ import {PermissionError} from "../PermissionError";
|
|||
import {EmbedLibrary, EmbedType} from "../EmbedLibrary";
|
||||
import {GroupConfigurationModel} from "../../Database/Models/GroupConfigurationModel";
|
||||
import parser from "any-date-parser";
|
||||
import {TimezoneHandler} from "../../Configuration/TimezoneHandler";
|
||||
import _ from "lodash";
|
||||
|
||||
export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInteractionCommand {
|
||||
definition(): SlashCommandBuilder {
|
||||
|
|
@ -123,8 +125,18 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
|
|||
)
|
||||
}
|
||||
|
||||
const fromDate = parser.fromString(<string>interaction.options.get("from")?.value ?? '');
|
||||
const toDate = parser.fromString(<string>interaction.options.get("to")?.value ?? '');
|
||||
const timezoneHandler = new TimezoneHandler(
|
||||
interaction.guildId ?? '',
|
||||
group
|
||||
);
|
||||
|
||||
const [fromDate, toDate] = timezoneHandler.use(() => {
|
||||
return [
|
||||
parser.fromString(<string>interaction.options.get("from")?.value ?? ''),
|
||||
parser.fromString(<string>interaction.options.get("to")?.value ?? '')
|
||||
]
|
||||
})
|
||||
|
||||
|
||||
if (!fromDate.isValid()) {
|
||||
throw new UserError("No date or invalid date format for the from parameter.");
|
||||
|
|
@ -184,13 +196,19 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
|
|||
|
||||
const group = GroupSelection.getGroup(interaction);
|
||||
|
||||
const timezone = new TimezoneHandler(
|
||||
interaction.guildId ?? '',
|
||||
group
|
||||
);
|
||||
const playdates = Container.get<PlaydateRepository>(PlaydateRepository.name).findFromGroup(group);
|
||||
await interaction.respond(
|
||||
playdates.map(playdate => {
|
||||
return {
|
||||
name: `${playdate.from_time.toLocaleString()} - ${playdate.to_time.toLocaleString()}`,
|
||||
value: <number>playdate.id
|
||||
}
|
||||
timezone.use(() => {
|
||||
return _.slice(playdates, 0, 25).map(playdate => {
|
||||
return {
|
||||
name: `${playdate.from_time.toLocaleString()} - ${playdate.to_time.toLocaleString()}`,
|
||||
value: <number>playdate.id
|
||||
}
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,24 @@
|
|||
import {CacheType, ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder, Snowflake} from "discord.js";
|
||||
import {
|
||||
CacheType,
|
||||
ChatInputCommandInteraction,
|
||||
hyperlink,
|
||||
inlineCode,
|
||||
PermissionFlagsBits,
|
||||
SlashCommandBuilder,
|
||||
Snowflake
|
||||
} from "discord.js";
|
||||
import {ChatInteractionCommand, Command} from "./Command";
|
||||
import {GroupSelection} from "../CommandPartials/GroupSelection";
|
||||
import {MenuHandler} from "../../Configuration/MenuHandler";
|
||||
import {ConfigurationHandler} from "../../Configuration/ConfigurationHandler";
|
||||
import {GroupConfigurationProvider} from "../../Configuration/Groups/GroupConfigurationProvider";
|
||||
import {Container} from "../../Container/Container";
|
||||
import {GroupConfigurationRepository} from "../../Database/Repositories/GroupConfigurationRepository";
|
||||
import {MenuRenderer} from "../../Menu/MenuRenderer";
|
||||
import {MenuTraversal} from "../../Menu/MenuTraversal";
|
||||
import {MenuItemType} from "../../Menu/MenuRenderer.types";
|
||||
import {ServerConfigurationProvider} from "../../Configuration/Server/ServerConfigurationProvider";
|
||||
import {ServerConfigurationRepository} from "../../Database/Repositories/ServerConfigurationRepository";
|
||||
import {TimezoneHandler, TimezoneSaveTarget} from "../../Configuration/TimezoneHandler";
|
||||
import {EmbedLibrary, EmbedType} from "../EmbedLibrary";
|
||||
import {UserError} from "../UserError";
|
||||
|
||||
export class ServerCommand implements Command, ChatInteractionCommand {
|
||||
definition(): SlashCommandBuilder {
|
||||
|
|
@ -22,6 +30,15 @@ export class ServerCommand implements Command, ChatInteractionCommand {
|
|||
.setName("config")
|
||||
.setDescription("Starts the configurator for the server settings")
|
||||
)
|
||||
.addSubcommand(command => command
|
||||
.setName("timezone")
|
||||
.setDescription("Sets the timezone for the server, if a value is provided. If not, the current timezone is displayed.")
|
||||
.addStringOption(option => option
|
||||
.setName('timezone')
|
||||
.setDescription("The timezone the server should use.")
|
||||
.setRequired(false)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
async execute(interaction: ChatInputCommandInteraction<CacheType>): Promise<void> {
|
||||
|
|
@ -29,6 +46,9 @@ export class ServerCommand implements Command, ChatInteractionCommand {
|
|||
case "config":
|
||||
await this.startConfiguration(interaction);
|
||||
break
|
||||
case "timezone":
|
||||
await this.handleTimezone(interaction);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -79,4 +99,46 @@ export class ServerCommand implements Command, ChatInteractionCommand {
|
|||
|
||||
menu.display(interaction);
|
||||
}
|
||||
|
||||
private async handleTimezone(interaction: ChatInputCommandInteraction<CacheType>) {
|
||||
const enteredTimezone = interaction.options.getString('timezone');
|
||||
|
||||
const timezoneHandler = new TimezoneHandler(
|
||||
interaction.guildId ?? ''
|
||||
);
|
||||
if (!enteredTimezone) {
|
||||
await interaction.reply(
|
||||
{
|
||||
embeds: [
|
||||
EmbedLibrary.base(
|
||||
"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.Server);
|
||||
await interaction.reply(
|
||||
{
|
||||
embeds: [
|
||||
EmbedLibrary.base(
|
||||
"Timezone changed",
|
||||
`The server now uses the timezone ${inlineCode(enteredTimezone)}.`,
|
||||
EmbedType.Success
|
||||
)
|
||||
]
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ export class UserError extends Error {
|
|||
public getEmbed(e: UserError): EmbedBuilder {
|
||||
const embed = EmbedLibrary.base(
|
||||
"Please validate your request!",
|
||||
inlineCode(e.message),
|
||||
e.message,
|
||||
EmbedType.Error
|
||||
).setFooter({
|
||||
text: "Type: Request"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue