Skip to content

Commit

Permalink
Merge pull request #1127 from givepraise/feat/announce-unactivated-users
Browse files Browse the repository at this point in the history
feat: add unactivated users option
  • Loading branch information
kristoferlund authored Aug 4, 2023
2 parents cf2e2d1 + 512a271 commit df0f9cc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
61 changes: 39 additions & 22 deletions packages/discord-bot/src/utils/dmTargets.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<void> => {
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 = [];
Expand All @@ -27,18 +28,30 @@ const sendDMs = async (
closedDmUsers: <string[]>[],
unknownErrorUsers: <string[]>[],
};
if (!users || users.length === 0) {

let userTargets: { accountId: string; name: string }[] | undefined;
if (!users) {
userTargets = await apiGet<UserAccount[]>('/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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions packages/discord-bot/src/utils/menus/dmTargetmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit df0f9cc

Please sign in to comment.