From a4a834ad27af82fd5c729a31d56b5d62c580c5d4 Mon Sep 17 00:00:00 2001 From: kenobi Date: Mon, 26 Jun 2023 23:48:52 +0200 Subject: [PATCH] refactor reaction handling - rename - externalise handling of none_of_that to vote controller - base for extensions for more reaction handling --- server/events/handleMessageReactionAdd.ts | 31 ++++++++++++++++ server/events/handleReactionAdd.ts | 45 ----------------------- 2 files changed, 31 insertions(+), 45 deletions(-) create mode 100644 server/events/handleMessageReactionAdd.ts delete mode 100644 server/events/handleReactionAdd.ts diff --git a/server/events/handleMessageReactionAdd.ts b/server/events/handleMessageReactionAdd.ts new file mode 100644 index 0000000..e137c17 --- /dev/null +++ b/server/events/handleMessageReactionAdd.ts @@ -0,0 +1,31 @@ + +import { Message, MessageReaction, User } from "discord.js"; +import { logger, newRequestId, noGuildId } from "../logger"; +import { NONE_OF_THAT } from "../constants"; +import { client } from "../.."; + + +export const name = 'messageReactionAdd' + +export async function execute(messageReaction: MessageReaction, user: User) { + if (user.id == client.user?.id) + logger.info('Skipping bot reaction') + const requestId = newRequestId + const guildId = messageReaction.message.inGuild() ? messageReaction.message.guildId : noGuildId + const reactedUponMessage: Message = messageReaction.message.partial ? await messageReaction.message.fetch() : messageReaction.message + if (!messageReaction.message.guild) { + logger.warn(`Received messageReactionAdd on non-guild message.`, { requestId }) + return + } + + logger.info(`Got reaction on message`, { requestId, guildId }) + //logger.debug(`reactedUponMessage payload: ${JSON.stringify(reactedUponMessage)}`) + + logger.info(`emoji: ${messageReaction.emoji.toString()}`) + if (messageReaction.emoji.toString() === NONE_OF_THAT) { + logger.info(`Reaction is NONE_OF_THAT. Handling`, { requestId, guildId }) + return client.VoteController.handleNoneOfThatVote(messageReaction, user, reactedUponMessage, requestId, guildId) + } + + return +} diff --git a/server/events/handleReactionAdd.ts b/server/events/handleReactionAdd.ts deleted file mode 100644 index 6ad27e6..0000000 --- a/server/events/handleReactionAdd.ts +++ /dev/null @@ -1,45 +0,0 @@ - -import { Message, MessageReaction, User } from "discord.js"; -import { messageIsVoteMessage } from "../helper/messageIdentifiers"; -import { logger, newRequestId, noGuildId } from "../logger"; -import { NONE_OF_THAT } from "../constants"; -import { client } from "../.."; -import { getMembersWithRoleFromGuild } from "../helper/roleFilter"; -import { config } from "../configuration"; - - -export const name = 'messageReactionAdd' - -export async function execute(messageReaction: MessageReaction, user: User) { - if (user.id == client.user?.id) - logger.info('Skipping bot reaction') - - const requestId = newRequestId - const guildId = messageReaction.message.inGuild() ? messageReaction.message.guildId : noGuildId - const reactedUponMessage: Message = messageReaction.message.partial ? await messageReaction.message.fetch() : messageReaction.message - if (!messageReaction.message.guild) return 'No guild' - - logger.info(`Got reaction on message`, { requestId, guildId }) - logger.debug(`reactedUponMessage payload: ${JSON.stringify(reactedUponMessage)}`) - - if (messageIsVoteMessage(reactedUponMessage)) { - logger.debug(`${reactedUponMessage.id} is vote message`, { requestId, guildId }) - if (messageReaction.message.reactions.cache.find(reaction => reaction.emoji.toString() == NONE_OF_THAT)) { - const watcherRoleMember = await getMembersWithRoleFromGuild(config.bot.announcement_role, messageReaction.message.guild) - logger.info("ROLE MEMBERS " + JSON.stringify(watcherRoleMember), { requestId, guildId }) - const watcherRoleMemberCount = watcherRoleMember.size - logger.info(`MEMBER COUNT: ${watcherRoleMemberCount}`, { requestId, guildId }) - let noneOfThatReactions = messageReaction.message.reactions.cache.get(NONE_OF_THAT)?.users.cache.filter(x => x.id !== client.user?.id).size ?? 0 - - const memberThreshold = (watcherRoleMemberCount / 2) - logger.info(`Reroll ${noneOfThatReactions} > ${memberThreshold} ?`, { requestId, guildId }) - if (noneOfThatReactions > memberThreshold) { - logger.info('Starting poll reroll', { requestId, guildId }) - messageReaction.message.edit((messageReaction.message.content ?? "").concat('\nDiese Abstimmung muss wiederholt werden.')) - } - logger.info(`No reroll`, { requestId, guildId }) - } - } - - return -}