From 43c23263f0c8d4858d0511889ed4ca3e0e0ef99c Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Sun, 6 Jun 2021 02:41:06 +0200 Subject: [PATCH] Properly order reaction roles --- backend/src/data/GuildReactionRoles.ts | 13 ++++++++++++- backend/src/data/entities/ReactionRole.ts | 2 ++ backend/src/index.ts | 2 +- .../1622939525343-OrderReactionRoles.ts | 18 ++++++++++++++++++ .../commands/InitReactionRolesCmd.ts | 3 +++ .../ReactionRoles/events/AddReactionRoleEvt.ts | 2 +- .../applyReactionRoleReactionsToMessage.ts | 10 ++++------ 7 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 backend/src/migrations/1622939525343-OrderReactionRoles.ts diff --git a/backend/src/data/GuildReactionRoles.ts b/backend/src/data/GuildReactionRoles.ts index 9b309f03..d724493f 100644 --- a/backend/src/data/GuildReactionRoles.ts +++ b/backend/src/data/GuildReactionRoles.ts @@ -24,6 +24,9 @@ export class GuildReactionRoles extends BaseGuildRepository { guild_id: this.guildId, message_id: messageId, }, + order: { + order: "ASC", + }, }); } @@ -50,7 +53,14 @@ export class GuildReactionRoles extends BaseGuildRepository { await this.reactionRoles.delete(criteria); } - async add(channelId: string, messageId: string, emoji: string, roleId: string, exclusive?: boolean) { + async add( + channelId: string, + messageId: string, + emoji: string, + roleId: string, + exclusive?: boolean, + position?: number, + ) { await this.reactionRoles.insert({ guild_id: this.guildId, channel_id: channelId, @@ -58,6 +68,7 @@ export class GuildReactionRoles extends BaseGuildRepository { emoji, role_id: roleId, is_exclusive: Boolean(exclusive), + order: position, }); } } diff --git a/backend/src/data/entities/ReactionRole.ts b/backend/src/data/entities/ReactionRole.ts index 532d3895..198fe0f5 100644 --- a/backend/src/data/entities/ReactionRole.ts +++ b/backend/src/data/entities/ReactionRole.ts @@ -21,4 +21,6 @@ export class ReactionRole { @Column() role_id: string; @Column() is_exclusive: boolean; + + @Column() order: number; } diff --git a/backend/src/index.ts b/backend/src/index.ts index fe3a07da..6cb6fbea 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -164,7 +164,7 @@ connect().then(async () => { intents: [ // Privileged Intents.FLAGS.GUILD_MEMBERS, - // "guildPresences", + // Intents.FLAGS.GUILD_PRESENCES, Intents.FLAGS.GUILD_MESSAGE_TYPING, // Regular diff --git a/backend/src/migrations/1622939525343-OrderReactionRoles.ts b/backend/src/migrations/1622939525343-OrderReactionRoles.ts new file mode 100644 index 00000000..2c94b118 --- /dev/null +++ b/backend/src/migrations/1622939525343-OrderReactionRoles.ts @@ -0,0 +1,18 @@ +import { MigrationInterface, QueryRunner, TableColumn } from "typeorm"; + +export class OrderReactionRoles1622939525343 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.addColumn( + "reaction_roles", + new TableColumn({ + name: "order", + type: "int", + isNullable: true, + }), + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.dropColumn("reaction_roles", "order"); + } +} diff --git a/backend/src/plugins/ReactionRoles/commands/InitReactionRolesCmd.ts b/backend/src/plugins/ReactionRoles/commands/InitReactionRolesCmd.ts index 2434dde5..44ede36e 100644 --- a/backend/src/plugins/ReactionRoles/commands/InitReactionRolesCmd.ts +++ b/backend/src/plugins/ReactionRoles/commands/InitReactionRolesCmd.ts @@ -103,6 +103,7 @@ export const InitReactionRolesCmd = reactionRolesCmd({ const progressMessage = msg.channel.send("Adding reaction roles..."); // Save the new reaction roles to the database + let pos = 0; for (const pair of emojiRolePairs) { await pluginData.state.reactionRoles.add( args.message.channel.id, @@ -110,7 +111,9 @@ export const InitReactionRolesCmd = reactionRolesCmd({ pair[0], pair[1], args.exclusive, + pos, ); + pos++; } // Apply the reactions themselves diff --git a/backend/src/plugins/ReactionRoles/events/AddReactionRoleEvt.ts b/backend/src/plugins/ReactionRoles/events/AddReactionRoleEvt.ts index b8331cf5..b996f68d 100644 --- a/backend/src/plugins/ReactionRoles/events/AddReactionRoleEvt.ts +++ b/backend/src/plugins/ReactionRoles/events/AddReactionRoleEvt.ts @@ -61,7 +61,7 @@ export const AddReactionRoleEvt = reactionRolesEvt({ setTimeout(() => { pluginData.state.reactionRemoveQueue.add(async () => { const wait = sleep(1500); - await meta.args.reaction.remove().catch(noop); + await meta.args.reaction.users.remove(userId).catch(noop); await wait; }); }, 1500); diff --git a/backend/src/plugins/ReactionRoles/util/applyReactionRoleReactionsToMessage.ts b/backend/src/plugins/ReactionRoles/util/applyReactionRoleReactionsToMessage.ts index e8280a8f..b2d39707 100644 --- a/backend/src/plugins/ReactionRoles/util/applyReactionRoleReactionsToMessage.ts +++ b/backend/src/plugins/ReactionRoles/util/applyReactionRoleReactionsToMessage.ts @@ -71,18 +71,16 @@ export async function applyReactionRoleReactionsToMessage( emojisToAdd.push(CLEAR_ROLES_EMOJI); for (const rawEmoji of emojisToAdd) { - const emoji = isSnowflake(rawEmoji) ? `foo:${rawEmoji}` : rawEmoji; - try { - await targetMessage.reactions.add(emoji); - await sleep(1250); // Make sure we don't hit rate limits + await targetMessage.react(rawEmoji); + await sleep(750); // Make sure we don't hit rate limits } catch (e) { if (isDiscordRESTError(e)) { if (e.code === 10014) { pluginData.state.reactionRoles.removeFromMessage(messageId, rawEmoji); - errors.push(`Unknown emoji: ${emoji}`); + errors.push(`Unknown emoji: ${rawEmoji}`); logs.log(LogType.BOT_ALERT, { - body: `Could not add unknown reaction role emoji ${emoji} to message ${channelId}/${messageId}`, + body: `Could not add unknown reaction role emoji ${rawEmoji} to message ${channelId}/${messageId}`, }); continue; } else if (e.code === 50013) {