Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tools/folio-bot/pings.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* The "Get release pings" button. Pressing it gives you the `Folio updates` role, which @-mentions you when a release
* is posted, and pressing it again takes the role back. The press is a component interaction (type 3), signed like a
* command, and the answer is private to the person who pressed.
*/
export const PINGS_BUTTON = 'folio-release-pings'

/** The message component to attach to a post: one row, one button. */
export const pingsComponents = () => [
{ type: 1, components: [{ type: 2, style: 2, label: 'Get release pings', emoji: { name: '🔔' }, custom_id: PINGS_BUTTON }] },
]

export async function togglePings(env, interaction, discord) {
if (!env.GUILD_ID || interaction.guild_id !== env.GUILD_ID || !env.ROLE_UPDATES) {
return 'Release pings are set up in the Folio Community server.'
}
const userId = interaction.member?.user?.id
if (!userId || !env.DISCORD_BOT_TOKEN) return 'That did not work. Try again in a little while.'
// The interaction carries the person's roles, so there is no second lookup to race.
const has = (interaction.member.roles ?? []).includes(env.ROLE_UPDATES)
const status = has
? await discord.removeRole(env.GUILD_ID, userId, env.ROLE_UPDATES, 'Turned release pings off')
: await discord.addRole(env.GUILD_ID, userId, env.ROLE_UPDATES, 'Turned release pings on')
if (status === 403) {
// Discord only lets a bot hand out roles below its own, and this is the one that reads like nothing without a hint.
return "I could not change that: Mr Folio's role has to sit above Folio updates in Server Settings › Roles. McCal can fix it."
}
if (status >= 300) return 'That did not work. Try again in a little while.'
return has ? 'Release pings are off.' : 'Done. You will be pinged when a new release is posted. Press the button again to stop.'
}
42 changes: 42 additions & 0 deletions tools/folio-bot/pings.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { PINGS_BUTTON, pingsComponents, togglePings } from './pings.mjs'

const env = { GUILD_ID: 'folio', ROLE_UPDATES: 'ping-role', DISCORD_BOT_TOKEN: 'token' }
const press = (roles, guild = 'folio') => ({ guild_id: guild, member: { user: { id: 'u1' }, roles } })
function fakeDiscord(status = 204) {
const calls = []
return { calls, addRole: async (...a) => (calls.push(['add', ...a]), status), removeRole: async (...a) => (calls.push(['remove', ...a]), status) }
}

test('the button is one row with one button that carries the id the worker listens for', () => {
const [row] = pingsComponents()
assert.equal(row.type, 1)
assert.equal(row.components.length, 1)
assert.equal(row.components[0].custom_id, PINGS_BUTTON)
assert.equal(row.components[0].type, 2)
})

test('pressing it without the role gives the role, and pressing it with the role takes it back', async () => {
const on = fakeDiscord()
assert.match(await togglePings(env, press(['other']), on), /pinged/)
assert.deepEqual(on.calls[0].slice(0, 4), ['add', 'folio', 'u1', 'ping-role'])
const off = fakeDiscord()
assert.match(await togglePings(env, press(['ping-role', 'other']), off), /are off/)
assert.deepEqual(off.calls[0].slice(0, 4), ['remove', 'folio', 'u1', 'ping-role'])
})

test('a bot below the role says what to fix instead of failing quietly', async () => {
assert.match(await togglePings(env, press([]), fakeDiscord(403)), /has to sit above Folio updates/)
})

test('another server, or a missing setting, changes nothing', async () => {
const none = fakeDiscord()
assert.match(await togglePings(env, press([], 'elsewhere'), none), /Folio Community server/)
assert.match(await togglePings({ ...env, ROLE_UPDATES: undefined }, press([]), none), /Folio Community server/)
assert.equal(none.calls.length, 0)
})

test('Discord failing is answered plainly', async () => {
assert.match(await togglePings(env, press([]), fakeDiscord(500)), /did not work/)
})
11 changes: 11 additions & 0 deletions tools/folio-bot/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
*/
import { PRIVATE, optionsOf, run } from './commands.mjs'
import { discordFor, expire, redeem } from './redeem.mjs'
import { PINGS_BUTTON, togglePings } from './pings.mjs'
import { sources as liveSources } from './sources.mjs'

const PING = 1
const APPLICATION_COMMAND = 2
const MESSAGE_COMPONENT = 3
const PONG = 1
const CHANNEL_MESSAGE = 4
const DEFERRED_CHANNEL_MESSAGE = 5
Expand Down Expand Up @@ -92,6 +94,15 @@ export function createWorker({

const interaction = JSON.parse(body)
if (interaction.type === PING) return json({ type: PONG })
if (interaction.type === MESSAGE_COMPONENT) {
// A button press. Only the release-pings button exists; anything else is acknowledged and ignored.
if (interaction.data?.custom_id !== PINGS_BUTTON) return json({ type: PONG })
const said = await togglePings(env, interaction, discord(env)).catch((error) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Defer slow component role updates

When Discord's role API takes more than three seconds due to transient latency, this branch sends no initial response until togglePings completes. Discord requires the initial interaction response within three seconds, so the user can see “interaction failed” even though the role change later succeeds; retrying may then reverse the successful change. Unlike the application-command path below, component interactions need the same timeout/defer-and-edit handling. See Discord's interaction response deadline.

Useful? React with 👍 / 👎.

console.error(`pings failed: ${error.message}`)
return 'That did not work. Try again in a little while.'
})
return json({ type: CHANNEL_MESSAGE, data: message(said, true) })
}
if (interaction.type !== APPLICATION_COMMAND) return json({ type: PONG })

const name = interaction.data?.name
Expand Down
14 changes: 14 additions & 0 deletions tools/folio-bot/worker.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,17 @@ test('the read-only answers stay public', async () => {
const answer = await (await worker.fetch(await signed(body), env)).json()
assert.equal(answer.data.flags & 64, 0)
})

test('a press of the release-pings button toggles the role and the answer is private', async () => {
const calls = []
const bot = createWorker({ discord: () => ({ addRole: async (...a) => (calls.push(a), 204), removeRole: async () => 204 }) })
const body = JSON.stringify({ type: 3, guild_id: 'folio', member: { user: { id: 'u1' }, roles: [] }, data: { custom_id: 'folio-release-pings' } })
const answer = await (await bot.fetch(await signed(body), { ...env, GUILD_ID: 'folio', ROLE_UPDATES: 'ping', DISCORD_BOT_TOKEN: 't' }, {})).json()
assert.equal(answer.type, 4)
assert.equal(answer.data.flags & 64, 64, 'ephemeral')
assert.match(answer.data.content, /pinged/)
assert.equal(calls.length, 1)
// A button this bot did not make is acknowledged and ignored.
const other = JSON.stringify({ type: 3, guild_id: 'folio', member: { user: { id: 'u1' }, roles: [] }, data: { custom_id: 'something-else' } })
assert.deepEqual(await (await bot.fetch(await signed(other), env, {})).json(), { type: 1 })
})
2 changes: 2 additions & 0 deletions tools/folio-bot/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ GUILD_ID = "1551382087985266838"
ROLE_COFFEE = "1552857170033508472"
ROLE_BACKER = "1552857199267545178"
ROLE_BUILDER = "1552857224194560141"
# The ping role the "Get release pings" button hands out and takes back.
ROLE_UPDATES = "1553093586705449062"

# The daily sweep that takes back roles whose codes have ended. 06:17 UTC, clear of the hour everyone else picks.
[triggers]
Expand Down
Loading