Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unactivated users option #1127

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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