pnp-scheduler/source/Discord/Commands/Server.ts

144 lines
No EOL
4.9 KiB
TypeScript

import {
CacheType,
ChatInputCommandInteraction,
hyperlink,
inlineCode,
PermissionFlagsBits,
SlashCommandBuilder,
Snowflake
} from "discord.js";
import {ChatInteractionCommand, Command} from "./Command";
import {MenuHandler} from "../../Configuration/MenuHandler";
import {ConfigurationHandler} from "../../Configuration/ConfigurationHandler";
import {Container} from "../../Container/Container";
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 {
return new SlashCommandBuilder()
.setName("server")
.setDescription("Allows server administrators to adjust things about the PnP Scheduler bot.")
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addSubcommand(command => command
.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> {
switch (interaction.options.getSubcommand()) {
case "config":
await this.startConfiguration(interaction);
break
case "timezone":
await this.handleTimezone(interaction);
break;
}
}
private async startConfiguration(interaction: ChatInputCommandInteraction) {
const menuHandler = new MenuHandler(
new ConfigurationHandler(
new ServerConfigurationProvider(
Container.get<ServerConfigurationRepository>(ServerConfigurationRepository.name),
<Snowflake>interaction.guildId
)
)
)
const menu = new MenuRenderer(
new MenuTraversal(
menuHandler.fillMenuItems(
[
{
traversalKey: "permissions",
label: "Permissions",
description: "Allows customization, how the server members are allowed to interact with the PnP Scheduler.",
type: MenuItemType.Collection,
children: [
{
traversalKey: "groupCreation",
label: "Group Creation",
description: "Sets the permissions, who is allowed to create groups.",
type: MenuItemType.Collection,
children: [
{
traversalKey: "allowEveryone",
label: "Allow Anyone",
description: "Defines if all members are allowed to create groups.",
}
]
},
]
},
]
),
'Server Configuration',
"This UI allows you to change settings for your server."
),
null,
null,
"Server"
)
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
)
]
}
)
}
}