import type { VoidyClient } from "./VoidyClient"; import type { Resource } from "../types/Resource"; export interface Context { client: VoidyClient; resource: Resource; interaction: I; buttonId(name: string, ...args: string[]): string; modalId(name: string, ...args: string[]): string; args(customId: string): string[]; } export interface EventContext { client: VoidyClient; resource: Resource; buttonId(name: string, ...args: string[]): string; modalId(name: string, ...args: string[]): string; } function buildId(origin: string, name: string, ...args: string[]): string { const segments = [`${origin}.${name}`, ...args]; return segments.join("-"); } function parseArgs(resourceId: string, customId: string): string[] { const prefix = resourceId + "-"; if (customId.startsWith(prefix)) { return customId.slice(prefix.length).split("-"); } if (customId === resourceId) return []; return customId.split("-"); } export function createContext( client: VoidyClient, resource: Resource, interaction: I, ): Context { return { client, resource, interaction, buttonId: (name, ...args) => buildId(resource.origin, name, ...args), modalId: (name, ...args) => buildId(resource.origin, name, ...args), args: (customId) => parseArgs(resource.id, customId), }; } export function createEventContext( client: VoidyClient, resource: Resource, ): EventContext { return { client, resource, buttonId: (name, ...args) => buildId(resource.origin, name, ...args), modalId: (name, ...args) => buildId(resource.origin, name, ...args), }; }