From a4e9aecd6b25c4342902c9e3013f603b44f5211a Mon Sep 17 00:00:00 2001 From: Vy Date: Wed, 2 Aug 2023 12:47:32 +0530 Subject: [PATCH 1/2] feat: add unactivated users option --- packages/discord-bot/src/utils/dmTargets.ts | 57 ++++++++++++------- .../src/utils/menus/dmTargetmenu.ts | 9 ++- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/packages/discord-bot/src/utils/dmTargets.ts b/packages/discord-bot/src/utils/dmTargets.ts index fb7ef5250..700584d25 100644 --- a/packages/discord-bot/src/utils/dmTargets.ts +++ b/packages/discord-bot/src/utils/dmTargets.ts @@ -1,4 +1,4 @@ -import { PeriodDetailsDto, User } from '../utils/api-schema'; +import { PeriodDetailsDto, User, UserAccount } from '../utils/api-schema'; import { CommandInteraction, DiscordAPIError, EmbedBuilder } from 'discord.js'; import { Buffer } from 'node:buffer'; import { FailedToDmUsersList } from '../interfaces/FailedToDmUsersList'; @@ -11,12 +11,13 @@ import { logger } from './logger'; */ const sendDMs = async ( interaction: CommandInteraction, - users: User[], - message: EmbedBuilder + message: EmbedBuilder, + users: User[] | undefined, + host?: string ): Promise => { logger.debug( `Sending DMs to users: ${users - .map((u) => `${u._id} - ${u.username}`) + ?.map((u) => `${u._id} - ${u.username}`) .join(', ')} with message: ${JSON.stringify(message.toJSON())}` ); @@ -27,18 +28,30 @@ const sendDMs = async ( closedDmUsers: [], unknownErrorUsers: [], }; - if (!users || users.length === 0) { + + let userTargets: { accountId: string; name: string }[] | undefined; + if (!users) { + userTargets = await apiGet('/useraccounts', { + headers: { host }, + }) + .then((res) => res.data.filter((u) => !u.user)) + .catch(() => undefined); + } else { + userTargets = users.map( + (u) => u.accounts.filter((acc) => acc.platform === 'DISCORD')[0] + ); + } + + if (!userTargets || userTargets.length === 0) { await interaction.editReply( 'Message not sent. No recipients matched filter.' ); + return; } - for (const user of users) { - const userAccount = user.accounts.filter( - (acc) => acc.platform === 'DISCORD' - )[0]; - const userId: string = userAccount?.accountId || 'Unknown user'; - const userName: string = userAccount?.name || userId; + for (const userAccount of userTargets) { + const userId: string = userAccount.accountId || 'Unknown user'; + const userName: string = userAccount.name || userId; try { const discordUser = await interaction.guild?.members.fetch(userId); if (!discordUser) { @@ -165,14 +178,18 @@ export const selectTargets = async ( if (!users) return; switch (type) { - case 'USERS': + case 'UNACTIVATED-USERS': { + await sendDMs(interaction, message, undefined, host); + break; + } + case 'ACTIVATED-USERS': case 'QUANTIFIERS': { await sendDMs( interaction, + message, type === 'QUANTIFIERS' ? users.filter((user) => user.roles.includes('QUANTIFIER')) - : users, - message + : users ); return; } @@ -195,8 +212,8 @@ export const selectTargets = async ( const receivers = selectedPeriod.receivers?.map((r) => r._id); await sendDMs( interaction, - users.filter((user) => receivers?.includes(user._id)), - message + message, + users.filter((user) => receivers?.includes(user._id)) ); return; } @@ -218,16 +235,16 @@ export const selectTargets = async ( .map((q) => q._id); await sendDMs( interaction, - users.filter((user) => q.includes(user._id)), - message + message, + users.filter((user) => q.includes(user._id)) ); return; } const q = quantifiers.map((q) => q._id); await sendDMs( interaction, - users.filter((user) => q.includes(user._id)), - message + message, + users.filter((user) => q.includes(user._id)) ); return; } catch (err) { diff --git a/packages/discord-bot/src/utils/menus/dmTargetmenu.ts b/packages/discord-bot/src/utils/menus/dmTargetmenu.ts index 158113d34..5773c57d4 100644 --- a/packages/discord-bot/src/utils/menus/dmTargetmenu.ts +++ b/packages/discord-bot/src/utils/menus/dmTargetmenu.ts @@ -8,9 +8,14 @@ export const dmTargetMenu = new StringSelectMenuBuilder() .setPlaceholder('Select user group') .addOptions([ { - label: 'All users', + label: 'All activated users', description: 'Send to all activated Praise users', - value: 'USERS', + value: 'ACTIVATED-USERS', + }, + { + label: 'All unactivated users', + description: 'Send to all unactivated Praise users', + value: 'UNACTIVATED-USERS', }, { label: 'All quantifiers', From 512a27138cc4b021b0f64e49958153e8b064e0f4 Mon Sep 17 00:00:00 2001 From: Vy Date: Wed, 2 Aug 2023 15:05:04 +0530 Subject: [PATCH 2/2] chore: lint' --- packages/discord-bot/src/utils/dmTargets.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/discord-bot/src/utils/dmTargets.ts b/packages/discord-bot/src/utils/dmTargets.ts index 700584d25..aedfbd678 100644 --- a/packages/discord-bot/src/utils/dmTargets.ts +++ b/packages/discord-bot/src/utils/dmTargets.ts @@ -16,9 +16,9 @@ const sendDMs = async ( host?: string ): Promise => { logger.debug( - `Sending DMs to users: ${users - ?.map((u) => `${u._id} - ${u.username}`) - .join(', ')} with message: ${JSON.stringify(message.toJSON())}` + `Sending DMs to users: ${ + users?.map((u) => `${u._id} - ${u.username}`).join(', ') || 'undefined' + } with message: ${JSON.stringify(message.toJSON())}` ); const successful = [];