From 9ec1a66f7c5e4a35565bde2bc45f8530d9804f54 Mon Sep 17 00:00:00 2001 From: Vy Date: Thu, 6 Oct 2022 02:11:40 +0530 Subject: [PATCH] chore: refactor & clean-up --- packages/discord-bot/src/handlers/activate.ts | 2 +- packages/discord-bot/src/handlers/announce.ts | 2 +- packages/discord-bot/src/handlers/forward.ts | 30 ++++++-------- packages/discord-bot/src/handlers/praise.ts | 39 +++++++++---------- packages/discord-bot/src/handlers/whoami.ts | 4 +- .../src/utils/assertPraiseAllowedInChannel.ts | 2 +- .../src/utils/assertPraiseGiver.ts | 2 +- .../discord-bot/src/utils/createPraise.ts | 39 +++++++++++++++++++ 8 files changed, 76 insertions(+), 44 deletions(-) create mode 100644 packages/discord-bot/src/utils/createPraise.ts diff --git a/packages/discord-bot/src/handlers/activate.ts b/packages/discord-bot/src/handlers/activate.ts index e37ed8a4f..032dd6659 100644 --- a/packages/discord-bot/src/handlers/activate.ts +++ b/packages/discord-bot/src/handlers/activate.ts @@ -4,7 +4,7 @@ import { EventLogTypeKey } from 'api/src/eventlog/types'; import { logEvent } from 'api/src/eventlog/utils'; import randomstring from 'randomstring'; import { CommandHandler } from 'src/interfaces/CommandHandler'; -import { alreadyActivatedError } from '../utils/praiseEmbeds'; +import { alreadyActivatedError } from '../utils/embeds/praiseEmbeds'; /** * Executes command /activate diff --git a/packages/discord-bot/src/handlers/announce.ts b/packages/discord-bot/src/handlers/announce.ts index 7db3a4c11..ee516619f 100644 --- a/packages/discord-bot/src/handlers/announce.ts +++ b/packages/discord-bot/src/handlers/announce.ts @@ -17,7 +17,7 @@ import { import { dmTargetMenu } from '../utils/menus/dmTargetmenu'; import { selectTargets } from '../utils/dmTargets'; import { periodSelectMenu } from '../utils/menus/periodSelectMenu'; -import { notActivatedError } from '../utils/praiseEmbeds'; +import { notActivatedError } from '../utils/embeds/praiseEmbeds'; /** * Executes command /announce diff --git a/packages/discord-bot/src/handlers/forward.ts b/packages/discord-bot/src/handlers/forward.ts index 359c66a8f..eda8a8bee 100644 --- a/packages/discord-bot/src/handlers/forward.ts +++ b/packages/discord-bot/src/handlers/forward.ts @@ -1,5 +1,4 @@ -import { PraiseModel } from 'api/dist/praise/entities'; -import { GuildMember, cleanContent } from 'discord.js'; +import { GuildMember } from 'discord.js'; import { UserModel } from 'api/dist/user/entities'; import { EventLogTypeKey } from 'api/src/eventlog/types'; import { logEvent } from 'api/src/eventlog/utils'; @@ -19,10 +18,11 @@ import { forwardSuccess, giverNotActivatedError, selfPraiseWarning, -} from '../utils/praiseEmbeds'; +} from '../utils/embeds/praiseEmbeds'; import { assertPraiseGiver } from '../utils/assertPraiseGiver'; import { assertPraiseAllowedInChannel } from '../utils/assertPraiseAllowedInChannel'; import { CommandHandler } from '../interfaces/CommandHandler'; +import { createPraise } from '../utils/createPraise'; /** * Execute command /firward @@ -70,8 +70,8 @@ export const forwardHandler: CommandHandler = async ( const receivers = interaction.options.getString('receivers'); const receiverData = { - validReceiverIds: receivers?.match(/<@!([0-9]+)>/g), - undefinedReceivers: receivers?.match(/@([a-z0-9]+)/gi), + validReceiverIds: receivers?.match(/<@!?([0-9]+)>/g), + undefinedReceivers: receivers?.match(/[^<]@([a-z0-9]+)/gi), roleMentions: receivers?.match(/<@&([0-9]+)>/g), }; @@ -117,8 +117,6 @@ export const forwardHandler: CommandHandler = async ( (u) => u ); - const guildChannel = await guild.channels.fetch(channel?.id || ''); - for (const receiver of Receivers) { const receiverAccount = await getUserAccount(receiver); @@ -131,17 +129,13 @@ export const forwardHandler: CommandHandler = async ( ); } } - const praiseObj = await PraiseModel.create({ - reason: reason, - reasonRealized: cleanContent(reason, channel), - giver: giverAccount._id, - forwarder: forwarderAccount._id, - sourceId: `DISCORD:${guild.id}:${interaction.channelId}`, - sourceName: `DISCORD:${encodeURIComponent( - guild.name - )}:${encodeURIComponent(guildChannel?.name || '')}`, - receiver: receiverAccount._id, - }); + const praiseObj = await createPraise( + interaction, + giverAccount, + receiverAccount, + reason, + forwarderAccount + ); await logEvent( EventLogTypeKey.PRAISE, diff --git a/packages/discord-bot/src/handlers/praise.ts b/packages/discord-bot/src/handlers/praise.ts index 3a2bf9b10..10cfa26d3 100644 --- a/packages/discord-bot/src/handlers/praise.ts +++ b/packages/discord-bot/src/handlers/praise.ts @@ -2,7 +2,7 @@ import { PraiseModel } from 'api/dist/praise/entities'; import { EventLogTypeKey } from 'api/src/eventlog/types'; import { logEvent } from 'api/src/eventlog/utils'; import logger from 'jet-logger'; -import { GuildMember, User, cleanContent } from 'discord.js'; +import { GuildMember, User } from 'discord.js'; import { settingValue } from 'api/dist/shared/settings'; import { dmError, @@ -16,11 +16,12 @@ import { undefinedReceiverWarning, selfPraiseWarning, firstTimePraiserInfo, -} from '../utils/praiseEmbeds'; +} from '../utils/embeds/praiseEmbeds'; import { assertPraiseGiver } from '../utils/assertPraiseGiver'; import { assertPraiseAllowedInChannel } from '../utils/assertPraiseAllowedInChannel'; import { CommandHandler } from '../interfaces/CommandHandler'; import { getUserAccount } from '../utils/getUserAccount'; +import { createPraise } from '../utils/createPraise'; /** * Execute command /praise @@ -53,6 +54,7 @@ export const praiseHandler: CommandHandler = async ( undefinedReceivers: receivers?.match(/[^<]@([a-z0-9]+)/gi), roleMentions: receivers?.match(/<@&([0-9]+)>/g), }; + if ( !receivers || receivers.length === 0 || @@ -69,12 +71,12 @@ export const praiseHandler: CommandHandler = async ( return; } - const userAccount = await getUserAccount(member as GuildMember); + const giverAccount = await getUserAccount(member as GuildMember); const praiseItemsCount = await PraiseModel.countDocuments({ - giver: userAccount._id, + giver: giverAccount._id, }); - if (!userAccount.user) { + if (!giverAccount.user) { await interaction.editReply(await notActivatedError()); return; } @@ -91,15 +93,14 @@ export const praiseHandler: CommandHandler = async ( )) as boolean; let warnSelfPraise = false; - if (!selfPraiseAllowed && receiverIds.includes(userAccount.accountId)) { + if (!selfPraiseAllowed && receiverIds.includes(giverAccount.accountId)) { warnSelfPraise = true; - receiverIds.splice(receiverIds.indexOf(userAccount.accountId), 1); + receiverIds.splice(receiverIds.indexOf(giverAccount.accountId), 1); } const Receivers = (await guild.members.fetch({ user: receiverIds })).map( (u) => u ); - const guildChannel = await guild.channels.fetch(channel?.id || ''); for (const receiver of Receivers) { const receiverAccount = await getUserAccount(receiver); @@ -112,22 +113,20 @@ export const praiseHandler: CommandHandler = async ( ); } } - const praiseObj = await PraiseModel.create({ - reason: reason, - reasonRealized: cleanContent(reason, channel), - giver: userAccount._id, - sourceId: `DISCORD:${guild.id}:${interaction.channelId}`, - sourceName: `DISCORD:${encodeURIComponent( - guild.name - )}:${encodeURIComponent(guildChannel?.name || '')}`, - receiver: receiverAccount._id, - }); + + const praiseObj = await createPraise( + interaction, + giverAccount, + receiverAccount, + reason + ); + if (praiseObj) { await logEvent( EventLogTypeKey.PRAISE, 'Created a new praise from discord', { - userAccountId: userAccount._id, + userAccountId: giverAccount._id, } ); @@ -141,7 +140,7 @@ export const praiseHandler: CommandHandler = async ( praised.push(receiverAccount.accountId); } else { logger.err( - `Praise not registered for [${userAccount.accountId}] -> [${receiverAccount.accountId}] for [${reason}]` + `Praise not registered for [${giverAccount.accountId}] -> [${receiverAccount.accountId}] for [${reason}]` ); } } diff --git a/packages/discord-bot/src/handlers/whoami.ts b/packages/discord-bot/src/handlers/whoami.ts index aace69d06..658b91ebe 100644 --- a/packages/discord-bot/src/handlers/whoami.ts +++ b/packages/discord-bot/src/handlers/whoami.ts @@ -3,9 +3,9 @@ import { UserAccountModel } from 'api/dist/useraccount/entities'; import { CommandInteraction, GuildMember } from 'discord.js'; import { UserState } from '../interfaces/UserState'; import { getUserAccount } from '../utils/getUserAccount'; -import { getStateEmbed } from '../utils/stateEmbed'; +import { getStateEmbed } from '../utils/embeds/stateEmbed'; import { assertPraiseGiver } from '../utils/assertPraiseGiver'; -import { dmError } from '../utils/praiseEmbeds'; +import { dmError } from '../utils/embeds/praiseEmbeds'; /** * Execute command /whoami diff --git a/packages/discord-bot/src/utils/assertPraiseAllowedInChannel.ts b/packages/discord-bot/src/utils/assertPraiseAllowedInChannel.ts index 996e0c644..4c0779447 100644 --- a/packages/discord-bot/src/utils/assertPraiseAllowedInChannel.ts +++ b/packages/discord-bot/src/utils/assertPraiseAllowedInChannel.ts @@ -4,7 +4,7 @@ import { settingValue } from 'api/dist/shared/settings'; const getChannelId = (channel: TextBasedChannel): string => { return channel.type === ChannelType.PublicThread || channel.type === ChannelType.PrivateThread || - channel.type === ChannelType.GuildAnnouncement + channel.type === ChannelType.AnnouncementThread ? channel?.parent?.id || channel.id : channel.id; }; diff --git a/packages/discord-bot/src/utils/assertPraiseGiver.ts b/packages/discord-bot/src/utils/assertPraiseGiver.ts index 86089ae1a..0007370dc 100644 --- a/packages/discord-bot/src/utils/assertPraiseGiver.ts +++ b/packages/discord-bot/src/utils/assertPraiseGiver.ts @@ -1,6 +1,6 @@ import { CacheType, CommandInteraction, GuildMember } from 'discord.js'; import { settingValue } from 'api/dist/shared/settings'; -import { dmError, praiseRoleError } from '../utils/praiseEmbeds'; +import { dmError, praiseRoleError } from './embeds/praiseEmbeds'; /** * Check if user has discord role PRAISE_GIVER_ROLE_ID if required, diff --git a/packages/discord-bot/src/utils/createPraise.ts b/packages/discord-bot/src/utils/createPraise.ts new file mode 100644 index 000000000..96f4e2f5b --- /dev/null +++ b/packages/discord-bot/src/utils/createPraise.ts @@ -0,0 +1,39 @@ +import { PraiseModel } from 'api/dist/praise/entities'; +import { PraiseDocument } from 'api/dist/praise/types'; +import { UserAccountDocument } from 'api/dist/useraccount/types'; +import { + ChannelType, + ChatInputCommandInteraction, + cleanContent, +} from 'discord.js'; + +export const createPraise = async ( + interaction: ChatInputCommandInteraction, + giverAccount: UserAccountDocument, + receiverAccount: UserAccountDocument, + reason: string, + forwarderAccount?: UserAccountDocument +): Promise => { + const { channel, guild } = interaction; + if (!channel || !guild || channel.type === ChannelType.DM) return; + + const channelName = + (channel.type === ChannelType.PublicThread || + channel.type === ChannelType.AnnouncementThread || + channel.type === ChannelType.PrivateThread) && + channel.parent + ? `${channel.parent.name}/${channel.name}` + : channel.name; + const praiseData = { + reason: reason, + reasonRealized: cleanContent(reason, channel), + giver: giverAccount._id, + forwarder: forwarderAccount?._id || undefined, + sourceId: `DISCORD:${guild.id}:${interaction.channelId}`, + sourceName: `DISCORD:${encodeURIComponent(guild.name)}:${encodeURIComponent( + channelName + )}`, + receiver: receiverAccount._id, + }; + return await PraiseModel.create(praiseData); +};