diff --git a/packages/discord-bot/src/utils/dmTargets.ts b/packages/discord-bot/src/utils/dmTargets.ts index fb7ef5250..aedfbd678 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,13 +11,14 @@ 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}`) - .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 = []; @@ -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',