@umx-bot/core 0.7.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,8 @@ import type {
2
2
  ChatScope,
3
3
  CommandSummary,
4
4
  Dispose,
5
+ IncomingMessage,
6
+ MemberRole,
5
7
  NavigationItemDefinition,
6
8
  NavigationPageDefinition,
7
9
  NavigationPageSummary,
@@ -20,6 +22,35 @@ const allScopes: ChatScope[] = ["group", "direct", "guild"];
20
22
  const defaultSurfaces: NavigationSurface[] = ["message"];
21
23
  const allSurfaces: NavigationSurface[] = ["message", "menu", "panel"];
22
24
  const idPattern = /^[a-z0-9][a-z0-9._-]*$/u;
25
+ const roleWeight: Record<MemberRole, number> = {
26
+ member: 0,
27
+ admin: 1,
28
+ owner: 2
29
+ };
30
+
31
+ /**
32
+ * Keeps the items `message`'s author can use in its chat and drops pages left
33
+ * empty. With `available`, items must also point at one of those commands.
34
+ */
35
+ export function filterNavigation(
36
+ pages: NavigationPageSummary[],
37
+ available: readonly CommandSummary[] | undefined,
38
+ message: IncomingMessage
39
+ ): NavigationPageSummary[] {
40
+ const commands = available
41
+ ? new Set(available.map((command) => command.name))
42
+ : undefined;
43
+ return pages.flatMap((page) => {
44
+ const items = page.items.filter(
45
+ (item) =>
46
+ (commands === undefined || commands.has(item.commandName)) &&
47
+ item.scopes.includes(message.scope) &&
48
+ roleWeight[message.author.role] >=
49
+ roleWeight[item.permission ?? "member"]
50
+ );
51
+ return items.length > 0 ? [{ ...page, items }] : [];
52
+ });
53
+ }
23
54
 
24
55
  export class BotNavigationRegistry {
25
56
  private readonly pages = new Map<string, RegisteredPage>();
@@ -31,10 +62,22 @@ export class BotNavigationRegistry {
31
62
  }
32
63
  }
33
64
 
34
- public forPlugin(plugin: PluginHelpSummary): NavigationRegistry {
65
+ public forPlugin(
66
+ plugin: PluginHelpSummary,
67
+ /** Without it, `available` filters by scope and role only. */
68
+ commands?: {
69
+ available(message: IncomingMessage): Promise<CommandSummary[]>;
70
+ }
71
+ ): NavigationRegistry {
35
72
  return {
36
73
  register: (page) => this.register(plugin, page),
37
- list: () => this.list()
74
+ list: () => this.list(),
75
+ available: async (message) =>
76
+ filterNavigation(
77
+ this.list(),
78
+ await commands?.available(message),
79
+ message
80
+ )
38
81
  };
39
82
  }
40
83
 
@@ -209,8 +209,10 @@ export class PluginRuntime {
209
209
  };
210
210
  const pluginHelp = this.helpSummary(plugin);
211
211
  const commandRegistry = this.commands.forPlugin(pluginHelp);
212
- const navigationRegistry: NavigationRegistry =
213
- this.navigation.forPlugin(pluginHelp);
212
+ const navigationRegistry: NavigationRegistry = this.navigation.forPlugin(
213
+ pluginHelp,
214
+ commandRegistry
215
+ );
214
216
  const middlewareRegistry = this.middleware.forPlugin(plugin.name);
215
217
  const outboundRegistry = this.options.outbound.forPlugin(plugin.name);
216
218
  const serviceRegistry = this.services.forPlugin(plugin.name);
@@ -249,11 +251,13 @@ export class PluginRuntime {
249
251
  commands: {
250
252
  register: (command) => track(commandRegistry.register(command)),
251
253
  list: () => commandRegistry.list(),
254
+ available: (message) => commandRegistry.available(message),
252
255
  format: (name, args) => commandRegistry.format(name, args)
253
256
  },
254
257
  navigation: {
255
258
  register: (page) => track(navigationRegistry.register(page)),
256
- list: () => navigationRegistry.list()
259
+ list: () => navigationRegistry.list(),
260
+ available: (message) => navigationRegistry.available(message)
257
261
  },
258
262
  middleware: {
259
263
  use: (handler, options) => {
@@ -21,6 +21,11 @@ export class ServiceContainer {
21
21
  };
22
22
  }
23
23
 
24
+ /** The service behind `token`, or undefined while nobody provides it. */
25
+ public lookup<T>(token: ServiceToken<T>): T | undefined {
26
+ return this.services.get(token.id)?.value as T | undefined;
27
+ }
28
+
24
29
  private provide<T>(
25
30
  plugin: string,
26
31
  token: ServiceToken<T>,