Skip to content

Commit 2215244

Browse files
authored
feat(buttons): add embed interactions (#58)
- When user posts x.com link, they're given visible options to hide or keep the embed.
1 parent 6f66c29 commit 2215244

File tree

3 files changed

+84
-11
lines changed

3 files changed

+84
-11
lines changed

src/buttons/embeds.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { button } from 'jellycommands';
2+
import { MODERATOR_IDS } from '../config';
3+
import { get_member, has_any_role_or_id } from '../utils/snowflake';
4+
import type advise from '../events/on_message/_advise';
5+
6+
/**
7+
* Handle interaction responses in {@link advise}.
8+
*/
9+
export default button({
10+
id: /^embed_\w+$/,
11+
12+
async run({ interaction }) {
13+
const [, action, author_id] = interaction.customId.split('_') as [
14+
string,
15+
'keep' | 'hide',
16+
string,
17+
];
18+
19+
/**
20+
* Interacting user is either the author of the original message
21+
* or a moderator.
22+
*/
23+
const valid_user = has_any_role_or_id(await get_member(interaction), [
24+
author_id,
25+
...MODERATOR_IDS,
26+
]);
27+
28+
if (!valid_user) {
29+
await interaction.reply({
30+
content:
31+
'Only the original author or a moderator may interact with this post.',
32+
flags: 'Ephemeral',
33+
});
34+
return;
35+
}
36+
37+
if (action === 'hide') {
38+
await interaction.message.suppressEmbeds(true);
39+
}
40+
await interaction.update({ components: [] });
41+
},
42+
});

src/config.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ export const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
1111
// #region people
1212
const ADMIN_ROLES = [
1313
// Moderators role in main server
14-
// '919214972557484073',
14+
'919214972557484073',
1515

1616
// // Maintainers role
1717
// '571775211431526410',
1818

1919
// // Admins role
20-
// '476141440091815947',
20+
'476141440091815947',
21+
];
2122

23+
const PRIVILEGED_ROLES = [
2224
// Threadlords
2325
'949258457352114176',
2426
];
@@ -29,14 +31,16 @@ const TEST_ADMIN_ROLES = ['918888136581476402'];
2931
/**
3032
* List of roles/user IDs allowed to delete tags even if they're not the author.
3133
*/
32-
export const TAG_DEL_PERMITTED_IDS = DEV_MODE ? TEST_ADMIN_ROLES : ADMIN_ROLES;
34+
export const TAG_DEL_PERMITTED_IDS = DEV_MODE
35+
? TEST_ADMIN_ROLES
36+
: PRIVILEGED_ROLES;
3337

3438
/**
3539
* List of roles/user IDs allowed to create tags.
3640
*/
3741
export const TAG_CREATE_PERMITTED_IDS = DEV_MODE
3842
? TEST_ADMIN_ROLES
39-
: ADMIN_ROLES;
43+
: PRIVILEGED_ROLES;
4044

4145
export const BOT_DEVS = [
4246
// cirilla
@@ -50,9 +54,11 @@ export const BOT_DEVS = [
5054
*/
5155
export const THREAD_ADMIN_IDS = [
5256
...BOT_DEVS,
53-
...(DEV_MODE ? TEST_ADMIN_ROLES : ADMIN_ROLES),
57+
...(DEV_MODE ? TEST_ADMIN_ROLES : PRIVILEGED_ROLES),
5458
];
5559

60+
export const MODERATOR_IDS = DEV_MODE ? TEST_ADMIN_ROLES : ADMIN_ROLES;
61+
5662
// #endregion
5763

5864
// #region channels

src/events/on_message/_advise.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { type Message } from 'discord.js';
1+
import {
2+
ActionRowBuilder,
3+
ButtonBuilder,
4+
ButtonStyle,
5+
MessagePayload,
6+
userMention,
7+
type Message,
8+
} from 'discord.js';
29
import urlRegex from 'url-regex';
310

411
export default async function mutate_content(message: Message) {
@@ -19,12 +26,30 @@ export default async function mutate_content(message: Message) {
1926

2027
const link_term = updated_link_list.length === 1 ? 'link' : 'links';
2128

22-
// Reply inline
23-
try {
24-
await message.reply(
25-
`I converted your \`x.com\` ${link_term} to use \`xcancel.com\` so that server members won't require an account to view content and threads:\n${updated_link_list}`,
26-
);
29+
const content = `${userMention(message.author.id)} I converted your \`x.com\` ${link_term} to use \`xcancel.com\` so that server members won't require an account to view content and threads:\n${updated_link_list}`;
30+
31+
const hide_button = new ButtonBuilder()
32+
.setLabel('Hide embed')
33+
.setCustomId(`embed_hide_${message.author.id}`)
34+
.setStyle(ButtonStyle.Secondary);
35+
36+
const keep_button = new ButtonBuilder()
37+
.setLabel('Keep embed')
38+
.setCustomId(`embed_keep_${message.author.id}`)
39+
.setStyle(ButtonStyle.Primary);
2740

41+
const row = new ActionRowBuilder<ButtonBuilder>({
42+
components: [hide_button, keep_button],
43+
});
44+
45+
const payload = new MessagePayload(message.channel, {
46+
content,
47+
components: [row],
48+
});
49+
50+
// Send message with interactions
51+
try {
52+
await message.channel.send(payload);
2853
await message.suppressEmbeds(true);
2954
} catch {
3055
// don't handle failures

0 commit comments

Comments
 (0)