Skip to content

Commit 1a22b3e

Browse files
authored
Merge pull request #127 from tauri-apps/feat/disable-role-reaction
feat: disabled role reaction
2 parents c996695 + 0f2692b commit 1a22b3e

File tree

4 files changed

+26
-94
lines changed

4 files changed

+26
-94
lines changed

src/events/messageCreate.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
MessageOptions,
32
ActionRowBuilder,
43
ButtonBuilder,
54
ThreadChannel,
@@ -15,7 +14,7 @@ import { add_thread_prefix } from '../utils/threads';
1514

1615
export default event({
1716
name: 'messageCreate',
18-
run: async ({}, message) => {
17+
run: async ({ }, message) => {
1918
// Rules for whether or not the message should be dealt with by the bot
2019
const should_ignore =
2120
message.author.bot ||
@@ -62,7 +61,7 @@ export default event({
6261
`> Reply by <@${message.author.id}>:\n` +
6362
message.content,
6463
files: message.attachments.map((x) => x),
65-
} as MessageOptions;
64+
};
6665
// Send the message to the thread
6766
await thread.send(forwardMessage);
6867
// Create response message to the user with a link to the thread
@@ -106,7 +105,7 @@ async function send_instruction_message(thread: ThreadChannel) {
106105
: base_description;
107106

108107
// Add the solve button to the message
109-
const msg = wrap_in_embed(description) as MessageOptions;
108+
const msg = wrap_in_embed(description) as any;
110109
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
111110
new ButtonBuilder()
112111
.setCustomId('solve')

src/events/messageReactionAdd.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import { ThreadChannel } from 'discord.js';
22
import { event } from 'jellycommands';
3-
import { MESSAGE_READ, REACTION_ROLE_CHANNEL, SOLVABLE_FORUMS } from '../config'
3+
import {
4+
MESSAGE_READ,
5+
REACTION_ROLE_CHANNEL,
6+
SOLVABLE_FORUMS,
7+
} from '../config';
48
import { hasPermission } from '../utils/reactionHandler';
59

610
export default event({
711
name: 'messageReactionAdd',
812
run: async (_, reaction, user) => {
913
try {
1014
// The bot shouldn't react to its own reactions
11-
if (user.bot || SOLVABLE_FORUMS.includes(reaction.message.channelId))
12-
return
13-
// If this is a reaction in the role reaction channel
14-
if (reaction.message.channelId === REACTION_ROLE_CHANNEL) {
15-
const result = await hasPermission(reaction, user);
16-
result.member.roles.add(result.roleId);
17-
} else {
18-
// Otherwise we make a more general guess that if the original message was from the bot
19-
// and it's a MESSAGE_READ emoji reaction, we assume the message should be deleted
20-
if (reaction.message.author.bot && reaction.emoji.name === MESSAGE_READ) {
21-
reaction.message.delete()
22-
}
15+
if (
16+
user.bot ||
17+
SOLVABLE_FORUMS.includes(reaction.message.channelId)
18+
)
19+
return;
20+
// Make a general guess that if the original message was from the bot
21+
// and it's a MESSAGE_READ emoji reaction, we assume the message should be deleted
22+
if (
23+
reaction.message.author.bot &&
24+
reaction.emoji.name === MESSAGE_READ
25+
) {
26+
reaction.message.delete();
2327
}
2428
} catch (error) {
2529
// Couldn't get it to raise the unwanted error in the test server, so instead of

src/events/messageReactionRemove.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/utils/reactionHandler.ts

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
User,
99
} from 'discord.js';
1010
import { REACTION_ROLE, REACTION_ROLE_CHANNEL } from '../config';
11-
import { wrap_in_embed } from './embed_helpers';
1211

1312
export async function hasPermission(
1413
reaction: MessageReaction | PartialMessageReaction,
@@ -76,14 +75,12 @@ export async function sendReactionRoleMessage(client: Client) {
7675
);
7776
messageArray.push('\n**<#879007560429088800>**');
7877
messageArray.push(
79-
"If you'd like to get involved with Tauri development you can react to the Tauri Contributor role below to be able to chat in the various channels.",
78+
"Get involved with Tauri development and browse the different projects.",
8079
);
8180
messageArray.push('\n**<#683637724116418561>**');
8281
messageArray.push(
8382
"See what the community is working on outside of Tauri. Reach out if you have a passion project you'd like to talk about.",
8483
);
85-
messageArray.push('\n**Server Roles**');
86-
messageArray.push('React below to receive the relative role');
8784

8885
let messageBody = messageArray.join('\n');
8986

@@ -93,71 +90,24 @@ export async function sendReactionRoleMessage(client: Client) {
9390

9491
console.debug('Got messages');
9592

93+
// Get an existing message with identical contents
9694
let message = messages
9795
.filter((item) => item.content === messageBody)
9896
.last();
9997

10098
if (message && message.author.id == message.client.user.id) {
10199
console.debug('Attempting to edit message...');
100+
// Edit the message
102101
await message.edit(messageBody);
103102
console.debug('Message edited');
104103
} else {
104+
// Delete old messages from the bot
105+
messages.filter((item) => item.author.id == item.client.user.id).forEach((item) => item.delete())
105106
console.debug('Attempting to send message...');
107+
// Send the message
106108
message = await channel.send(messageBody);
107109
console.debug('Message sent');
108110
}
109-
110-
// Loop all available reaction roles
111-
REACTION_ROLE.forEach(async (role) => {
112-
// Get the reaction
113-
console.debug('Attempting to get reactions...');
114-
const reaction = await message.reactions.resolve(role.emojiId);
115-
console.debug('Got reactions');
116-
117-
// No reactions yet
118-
if (!reaction) {
119-
return;
120-
}
121-
122-
// Get all users that reacted minus the bot
123-
console.debug('Getting users who reacted...');
124-
const reactedUsers = (await reaction.users.fetch()).filter(
125-
(user) => user.id !== message.author.id,
126-
);
127-
console.debug('Finished fetching users');
128-
129-
// Loop all users and add the role
130-
let counter = 0;
131-
reactedUsers.forEach(async (user) => {
132-
try {
133-
const result = await hasPermission(reaction, user);
134-
counter += 1;
135-
const localCounter = counter;
136-
console.debug(
137-
`(${localCounter} / ${reactedUsers.size}) Attempting to add role...`,
138-
);
139-
if (result) {
140-
await result.member.roles.add(result.roleId);
141-
console.debug(
142-
`(${localCounter} / ${reactedUsers.size}) Role added`,
143-
);
144-
}
145-
} catch (error) {
146-
console.error(`Issue adding role: ${error}`);
147-
}
148-
});
149-
});
150-
151-
var roleDescription: string[] = [];
152-
153-
REACTION_ROLE.forEach(async (reaction) => {
154-
message.react(reaction.emojiId);
155-
roleDescription.push(
156-
`<:${reaction.emojiName}:${reaction.emojiId}> ${reaction.description}`,
157-
);
158-
});
159-
160-
await message.edit(wrap_in_embed(roleDescription.join('\n')));
161111
} catch (error) {
162112
console.error(`Issue starting up reaction: ${error}`);
163113
}

0 commit comments

Comments
 (0)