Build interactive Discord today dashboard and check-ins
This commit is contained in:
@@ -28,10 +28,21 @@ export function discordFixture(setup?: boolean | ((f: ReturnType<typeof fixture>
|
||||
return { ...f, alice, bob, changed, payload, requestDiscord: request, run, restart: () => { app = createDiscordRoutes(f.db, { publicKey: key, applicationId, origin: f.origin, remindersAvailable }, () => time, id => changed.push(id), dispatch); }, setDiscordTime: (value: string) => { time = Date.parse(value); f.setTime(value); } };
|
||||
}
|
||||
export function control(reply: any, label: string): string {
|
||||
const item = reply.data.components.flatMap((r: any) => r.components ?? []).find((c: any) => c.label === label || c.placeholder === label);
|
||||
const item = components(reply.data.components).find((c: any) => c.label === label || c.placeholder === label);
|
||||
if (!item) throw new Error(`Missing control ${label}: ${JSON.stringify(reply)}`);
|
||||
return item.custom_id;
|
||||
}
|
||||
export function submit(custom_id: string, values: Record<string, string>) {
|
||||
return { type: 5, message: { id: '123' }, data: { custom_id, components: Object.entries(values).map(([custom_id, value]) => ({ type: 1, components: [{ type: 4, custom_id, value }] })) } };
|
||||
}
|
||||
|
||||
export function components(items: any[] = []): any[] {
|
||||
return items.flatMap(item => [item, ...components(item.components), ...(item.accessory ? components([item.accessory]) : [])]);
|
||||
}
|
||||
export function messageText(reply: any): string {
|
||||
return [reply.data.content, ...components(reply.data.components).map(item => item.content)].filter(Boolean).join('\n');
|
||||
}
|
||||
export function habitControl(reply: any, name: string, label = 'Details'): string {
|
||||
const item = reply.data.components.find((item: any) => item.type === 17 && messageText({ data: { components: [item] } }).includes(`**${name}**`));
|
||||
return control({ data: { components: [item] } }, label);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user