pnp-scheduler/source/Groups/ConfigurationMenuHandler.ts

164 lines
No EOL
5.3 KiB
TypeScript

import {
AnyMenuItem, FieldMenuItem,
FieldMenuItemContext, FieldMenuItemSaveValue,
MenuItemType,
RowBuilderFieldMenuItemContext
} from "../Menu/MenuRenderer.types";
import {GroupConfigurationTransformers} from "./GroupConfigurationTransformers";
import {GroupConfigurationHandler} from "./GroupConfigurationHandler";
import {
channelMention,
ChannelSelectMenuBuilder,
ChannelType,
inlineCode,
italic,
Snowflake,
StringSelectMenuBuilder, StringSelectMenuOptionBuilder
} from "discord.js";
import {ChannelId} from "../types/DiscordTypes";
import {MessageActionRowComponentBuilder} from "@discordjs/builders";
export class ConfigurationMenuHandler {
constructor(
private readonly configuration: GroupConfigurationHandler,
private readonly transformer: GroupConfigurationTransformers
) {
}
public getMenuItems(): AnyMenuItem[] {
return [
{
traversalKey: "channels",
label: "Channels",
description: "Provides settings to define in what channels the bot sends messages, when not directly interacting with it.",
type: MenuItemType.Collection,
children: [
{
traversalKey: "newPlaydates",
label: "New Playdates",
description: "Sets the channel, where the group gets notified, when new Playdates are set.",
type: MenuItemType.Field,
getCurrentValue: this.getChannelValue.bind(this),
getActionRowBuilder: this.getChannelMenuBuilder.bind(this),
setValue: this.setValue.bind(this)
},
{
traversalKey: "playdateReminders",
label: 'Playdate Reminders',
description: "Sets the channel, where the group gets reminded of upcoming playdates.",
type: MenuItemType.Field,
getCurrentValue: this.getChannelValue.bind(this),
getActionRowBuilder: this.getChannelMenuBuilder.bind(this),
setValue: this.setValue.bind(this)
}
]
},
{
traversalKey: "locale",
label: "Locale",
description: "Provides locale to be used for this group.",
type: MenuItemType.Field,
getCurrentValue: this.getLocaleValue.bind(this),
getActionRowBuilder: this.getLocaleMenuBuilder.bind(this),
setValue: this.setValue.bind(this)
},
{
traversalKey: "permissions",
label: "Permissions",
description: "Allows customization, how the members are allowed to interact with the data stored in the group.",
type: MenuItemType.Collection,
children: [
{
traversalKey: "allowMemberManagingPlaydates",
label: "Manage Playdates",
description: "Defines if the members are allowed to manage playdates like adding or deleting them.",
type: MenuItemType.Field,
getCurrentValue: this.getPermissionBooleanValue.bind(this),
getActionRowBuilder: this.getPermissionBooleanBuilder.bind(this),
setValue: this.setValue.bind(this)
}
]
}
]
}
private getChannelValue(context: FieldMenuItemContext): string {
const value = this.configuration.getConfigurationByPath(context.path.join('.'));
if (value === undefined) {
return italic("None");
}
if (!value) {
return inlineCode("None");
}
return channelMention(<ChannelId>value);
}
private getChannelMenuBuilder(context: FieldMenuItemContext): MessageActionRowComponentBuilder {
return new ChannelSelectMenuBuilder()
.setChannelTypes(ChannelType.GuildText)
.setPlaceholder("New Value");
}
private getLocaleValue(context: FieldMenuItemContext): string {
const value = this.configuration.getConfigurationByPath(context.path.join('.'));
if (value === undefined) {
return italic("None");
}
if (!value) {
return inlineCode("Default");
}
const displaynames = new Intl.DisplayNames(["en"], {type: "language"});
return displaynames.of((<Intl.Locale>value)?.baseName) ?? "Unknown";
}
private getLocaleMenuBuilder(context: FieldMenuItemContext): MessageActionRowComponentBuilder {
const options = [
'en-US',
'fr-FR',
'it-IT',
'de-DE'
]
const displaynames = new Intl.DisplayNames(["en"], {type: "language"});
return new StringSelectMenuBuilder()
.setOptions(
options.map(intl => new StringSelectMenuOptionBuilder()
.setLabel(displaynames.of(intl) ?? '')
.setValue(intl)
)
)
}
private getPermissionBooleanValue(context: FieldMenuItemContext) {
const value = this.configuration.getConfigurationByPath(context.path.join('.'));
if (value === undefined) {
return italic("None");
}
return value ? 'Allowed' : "Disallowed";
}
private getPermissionBooleanBuilder(context: FieldMenuItemContext) {
return new StringSelectMenuBuilder()
.setOptions(
[
{
label: "Allow",
value: "1"
},
{
label: "Disallow",
value: "0"
}
]
)
}
private setValue(value: FieldMenuItemSaveValue[], context: FieldMenuItemContext): void {
this.configuration.saveConfiguration(context.path.join('.'), value.join('; '));
}
}