Recipe: a scheduled announcement

Every Friday at 20:30 the bot reminds the raiders, pings their role on purpose, and (on 3.2 servers) lets the reminder disappear after the raid. Timers, role mentions and timed messages.

What it needs

  • Intents: none: the bot only posts.
  • Permissions: View channels and Send messages in the announcement channel. Mentioning a role needs the role to be mentionable, or Mention @all and @here.

The code

import { Client } from '@mnema-xyz/bot-sdk'

const CHANNEL = process.env.ANNOUNCE_CHANNEL ?? ''
const RAIDERS = process.env.RAIDERS_ROLE ?? '' // the role's id

const client = new Client({
  baseUrl: process.env.MNEMA_URL ?? 'https://talk.example.org',
  token: process.env.MNEMA_TOKEN ?? '',
  intents: 0,
})

/** The next Friday 20:30 in the server's local time zone. */
function nextFriday(now = new Date()): Date {
  const next = new Date(now)
  next.setHours(20, 30, 0, 0)
  next.setDate(now.getDate() + ((5 - now.getDay() + 7) % 7))
  if (next <= now) next.setDate(next.getDate() + 7)
  return next
}

async function announce() {
  await client.rest.createMessage(CHANNEL, {
    content: '@Raiders boss rush in 30 minutes. Talk: Playtest night.',
    allowed_mentions: { roles: [RAIDERS] },
    // From Mnema Talk 3.2: the server deletes the reminder three hours later.
    // lifetime_seconds: 3 * 3600,
  })
}

function schedule() {
  setTimeout(async () => {
    await announce().catch((err) => console.error(err))
    schedule()
  }, nextFriday().getTime() - Date.now())
}

client.on('ready', schedule)
client.on('error', (err) => console.error(err))
await client.connect()

Notes

  • allowed_mentions.roles names exactly the role that may be notified; without it, a role mention in a bot message stays plain text. See allowed_mentions.
  • On a 3.2 server, uncomment lifetime_seconds and the reminder cleans itself up: see Timed messages.
  • A bot keeps its schedule in memory; members can also schedule messages themselves in the app (Send later).

Something wrong on this page? Tell us.