Adds Event system and automatic messages

This commit is contained in:
Michel Fedde 2025-05-25 16:07:09 +02:00
parent 0e10ea3cab
commit 2f826fbf36
20 changed files with 428 additions and 18 deletions

View file

@ -6,11 +6,15 @@ import {GroupConfigurationResult, GroupConfigurationTransformers} from "./GroupC
import setPath from 'object-path-set';
import deepmerge from "deepmerge";
import {Nullable} from "../types/Nullable";
import {isPlainObject} from "is-plain-object";
export class GroupConfigurationHandler {
private static DEFAULT_CONFIGURATION: RuntimeGroupConfiguration = {
channels: null,
locale: new Intl.Locale('en-GB'),
permissions: {
allowMemberManagingPlaydates: false
}
}
private readonly transformers: GroupConfigurationTransformers = new GroupConfigurationTransformers();
@ -42,7 +46,9 @@ export class GroupConfigurationHandler {
}
public getConfiguration(): RuntimeGroupConfiguration {
return deepmerge(GroupConfigurationHandler.DEFAULT_CONFIGURATION, this.getDatabaseConfiguration());
return deepmerge(GroupConfigurationHandler.DEFAULT_CONFIGURATION, this.getDatabaseConfiguration(), {
isMergeableObject: isPlainObject
});
}
public getConfigurationByPath(path: string): Nullable<GroupConfigurationResult> {

View file

@ -34,6 +34,7 @@ import {UserError} from "../Discord/UserError";
import {RuntimeGroupConfiguration} from "./RuntimeGroupConfiguration";
import {ChannelId} from "../types/DiscordTypes";
import {IconCache} from "../Icons/IconCache";
import {ifError} from "node:assert";
type UIElementCollection = Record<string, UIElement>;
type UIElement = {
@ -76,6 +77,19 @@ export class GroupConfigurationRenderer {
key: 'locale',
description: "Provides locale to be used for this group. This mostly sets how the dates are displayed, but this can also be later used for translations.",
isConfiguration: true
},
permissions: {
label: "Permissions",
key: "permissions",
description: "Allows customization, how the members are allowed to interact with the data stored in the group.",
childrenElements: {
allowMemberManagingPlaydates: {
label: "Manage Playdates",
key: "allowMemberManagingPlaydates",
description: "Defines if the members are allowed to manage playdates like adding or deleting them.",
isConfiguration: true
}
}
}
}
@ -220,6 +234,8 @@ export class GroupConfigurationRenderer {
return displaynames.of((<Intl.Locale>value).baseName) ?? "Unknown";
case TransformerType.Channel:
return channelMention(<ChannelId>value);
case TransformerType.PermissionBoolean:
return value ? "Allowed" : "Disallowed"
default:
return "None";
@ -279,6 +295,21 @@ export class GroupConfigurationRenderer {
.setCustomId(GroupConfigurationRenderer.SETVALUE_COMMAND + breadcrumbPath)
.setChannelTypes(ChannelType.GuildText)
.setPlaceholder("New Value");
case TransformerType.PermissionBoolean:
return new StringSelectMenuBuilder()
.setCustomId(GroupConfigurationRenderer.SETVALUE_COMMAND + breadcrumbPath)
.setOptions(
[
{
label: "Allow",
value: "1"
},
{
label: "Disallow",
value: "0"
}
]
)
default:
return new StringSelectMenuBuilder()
@ -309,6 +340,7 @@ export class GroupConfigurationRenderer {
switch (transformerType) {
case TransformerType.Locale:
case TransformerType.Channel:
case TransformerType.PermissionBoolean:
return interaction.values.join('; ');
default:
throw new Error("Unhandled select menu");

View file

@ -8,6 +8,7 @@ import {ArrayUtils} from "../Utilities/ArrayUtils";
export enum TransformerType {
Locale,
Channel,
PermissionBoolean,
}
type GroupConfigurationTransformer = {
@ -16,7 +17,7 @@ type GroupConfigurationTransformer = {
}
export type GroupConfigurationResult =
ChannelId | Intl.Locale
ChannelId | Intl.Locale | boolean
export class GroupConfigurationTransformers {
static TRANSFORMERS: GroupConfigurationTransformer[] = [
@ -31,6 +32,10 @@ export class GroupConfigurationTransformers {
{
path: ['locale'],
type: TransformerType.Locale,
},
{
path: ['permissions', 'allowMemberManagingPlaydates'],
type: TransformerType.PermissionBoolean
}
];
@ -45,6 +50,8 @@ export class GroupConfigurationTransformers {
return new Intl.Locale(configValue.value)
case TransformerType.Channel:
return <ChannelId>configValue.value;
case TransformerType.PermissionBoolean:
return configValue.value === '1';
}
}

View file

@ -1,9 +1,17 @@
import {ChannelId} from "../types/DiscordTypes";
import {Nullable} from "../types/Nullable";
export type RuntimeGroupConfiguration = {
channels: Nullable<ChannelRuntimeGroupConfiguration>,
locale: Intl.Locale,
permissions: PermissionRuntimeGroupConfiguration
};
export type ChannelRuntimeGroupConfiguration = {
newPlaydates: ChannelId,
playdateReminders: ChannelId
}
export type PermissionRuntimeGroupConfiguration = {
allowMemberManagingPlaydates: boolean
}