UNPKG

shadow-core

Version:

A modular core framework for Discord bot development, providing commands, buttons, menus, middleware, and more.

37 lines (31 loc) 1.05 kB
import { Client, ButtonInteraction } from "discord.js"; import { Button } from "./button"; import { splitSpecialId } from "../util"; export class ButtonManager { private client: Client; private static buttons: Map<string, Button> = new Map(); constructor(client: Client) { this.client = client; } registerButton(button: Button) { ButtonManager.buttons.set(button.customId, button); } async handleInteraction(interaction: ButtonInteraction) { let button: Button | undefined; if (interaction.customId.includes(":")) { const parsedId = splitSpecialId(interaction.customId); const newId = `${parsedId.feature}:${parsedId.action}:{id}`; button = ButtonManager.buttons.get(newId); } else { button = ButtonManager.buttons.get(interaction.customId); } if (button) { await button.run(interaction, this.client); } } static LogAllButtons(): void { for (const [id, button] of ButtonManager.buttons) { console.log(`Button ID: ${id}, Button Data:`, button); } } }