Repository navigation
Mr Folio gets a Get release pings button #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.' | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When Discord's role API takes more than three seconds due to transient latency, this branch sends no initial response until
togglePingscompletes. 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 👍 / 👎.