Recipe: a button panel for your crew
A message with buttons: members tap "I am in" or "Maybe" for tonight's run, and the message keeps the list up to date. It shows custom_id state, updating a message on a click, and ephemeral answers.
What it needs
- Intents:
GUILD_MESSAGES(for the!runcommand that posts the panel). Button clicks need no intent. - Permissions: View channels and Send messages where the panel lives.
The code
import { actionRow, button, ButtonStyle, Client, Intents } from '@mnema-xyz/bot-sdk'
const client = new Client({
baseUrl: process.env.MNEMA_URL ?? 'https://talk.example.org',
token: process.env.MNEMA_TOKEN ?? '',
intents: Intents.GuildMessages,
})
// One panel per message: who is in, who is a maybe. In memory, like a whiteboard.
const panels = new Map<string, { yes: Set<string>; maybe: Set<string> }>()
const rows = [
actionRow(
button({ customId: 'run:yes', label: 'I am in', style: ButtonStyle.Success }),
button({ customId: 'run:maybe', label: 'Maybe', style: ButtonStyle.Secondary }),
button({ customId: 'run:out', label: 'Not tonight', style: ButtonStyle.Danger }),
),
]
function text(p: { yes: Set<string>; maybe: Set<string> }) {
const list = (s: Set<string>) => (s.size ? [...s].join(', ') : 'nobody yet')
return `**Boss rush tonight, 21:00**\nIn: ${list(p.yes)}\nMaybe: ${list(p.maybe)}`
}
client.on('messageCreate', async (m) => {
if (m.is_bot || m.content !== '!run') return
const p = { yes: new Set<string>(), maybe: new Set<string>() }
const msg = await client.rest.createMessage(m.channel_id, { content: text(p), components: rows })
panels.set(msg.id, p)
})
client.on('interactionCreate', async (i) => {
if (!i.isButton() || !i.customId.startsWith('run:') || !i.message) return
const p = panels.get(i.message.id)
if (!p) return i.reply({ content: 'This panel is from before a restart. Ask for a new one with !run.', ephemeral: true })
const name = i.user.display_name
p.yes.delete(name)
p.maybe.delete(name)
if (i.customId === 'run:yes') p.yes.add(name)
if (i.customId === 'run:maybe') p.maybe.add(name)
await i.update({ content: text(p) })
})
client.on('error', (err) => console.error(err))
await client.connect()
Notes
custom_idis your state: it comes back exactly with every click. Here it names the choice; it could also carry an id (run:yes:<event>).i.update()is callback type7: it edits the message the button is on, and members see the change at once.- The panel lives in memory. For a bot that must survive restarts, keep
panelsin a file or a database; the buttons keep working because every click is a new interaction.