A powerful and extendable Discord bot, with it's own module system :3
thevoid.cafe/projects/voidy
1import type { VoidyClient } from "./VoidyClient";
2import type { Resource } from "../types/Resource";
3
4export interface Context<I> {
5 client: VoidyClient;
6 resource: Resource;
7 interaction: I;
8 buttonId(name: string, ...args: string[]): string;
9 modalId(name: string, ...args: string[]): string;
10 args(customId: string): string[];
11}
12
13export interface EventContext {
14 client: VoidyClient;
15 resource: Resource;
16 buttonId(name: string, ...args: string[]): string;
17 modalId(name: string, ...args: string[]): string;
18}
19
20function buildId(origin: string, name: string, ...args: string[]): string {
21 const segments = [`${origin}.${name}`, ...args];
22 return segments.join("-");
23}
24
25function parseArgs(resourceId: string, customId: string): string[] {
26 const prefix = resourceId + "-";
27 if (customId.startsWith(prefix)) {
28 return customId.slice(prefix.length).split("-");
29 }
30 if (customId === resourceId) return [];
31 return customId.split("-");
32}
33
34export function createContext<I>(
35 client: VoidyClient,
36 resource: Resource,
37 interaction: I,
38): Context<I> {
39 return {
40 client,
41 resource,
42 interaction,
43 buttonId: (name, ...args) => buildId(resource.origin, name, ...args),
44 modalId: (name, ...args) => buildId(resource.origin, name, ...args),
45 args: (customId) => parseArgs(resource.id, customId),
46 };
47}
48
49export function createEventContext(
50 client: VoidyClient,
51 resource: Resource,
52): EventContext {
53 return {
54 client,
55 resource,
56 buttonId: (name, ...args) => buildId(resource.origin, name, ...args),
57 modalId: (name, ...args) => buildId(resource.origin, name, ...args),
58 };
59}