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

chore: changes to /announce #1095

Merged
merged 3 commits into from
Jun 26, 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
242 changes: 134 additions & 108 deletions packages/discord-bot/src/handlers/announce.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
StringSelectMenuInteraction,
ActionRowBuilder,
ButtonBuilder,
StringSelectMenuBuilder,
Expand All @@ -16,9 +15,10 @@ import { announcementEmbed } from '../utils/embeds/announcementEmbed';
import { periodSelectMenu } from '../utils/menus/periodSelectMenu';
import { getUserAccount } from '../utils/getUserAccount';

import { apiClient } from '../utils/api';
import { PeriodPaginatedResponseDto } from '../utils/api-schema';
import { apiGet } from '../utils/api';
import { PeriodPaginatedResponseDto, Period } from '../utils/api-schema';
import { renderMessage } from '../utils/renderMessage';
import { logger } from '../utils/logger';

/**
* Executes command /announce
Expand Down Expand Up @@ -48,127 +48,153 @@ export const announcementHandler: CommandHandler = async (
const currentUser = userAccount.user;

if (currentUser.roles.includes('ADMIN')) {
const message = interaction.options.getString('message', true);

const userSelectionMsg = await interaction.editReply({
content: 'Which users do you want to send the message to?',
components: [
new ActionRowBuilder<StringSelectMenuBuilder>().addComponents([
dmTargetMenu,
]),
],
await interaction.editReply({
content:
'You do not have the needed permissions to use this command. If you would like to perform admin actions, you would need to be granted an `ADMIN` role on the Praise Dashboard.',
});
return;
}

const collector = userSelectionMsg.createMessageComponentCollector({
filter: (click) => click.user.id === interaction.user.id,
time: 900000,
});
let selectedUserType: string;
let selectedPeriod: string | undefined;
collector.on('collect', async (click) => {
await click.deferUpdate();
switch (click.customId) {
case 'dm-menu': {
if (!click.isStringSelectMenu()) break;
const menu: StringSelectMenuInteraction = click;
selectedUserType = menu.values[0];
if (
selectedUserType === 'ASSIGNED-QUANTIFIERS' ||
selectedUserType === 'UNFINISHED-QUANTIFIERS'
) {
const openPeriods = await apiClient
.get<PeriodPaginatedResponseDto>('/periods', {
headers: { host },
})
.then((res) =>
res.data.docs.filter((doc) => doc.status === 'QUANTIFY')
)
.catch(() => undefined);

if (!openPeriods || !openPeriods.length) {
await interaction.editReply({
content: 'No periods open for quantification.',
components: [],
});
return;
}
const message = interaction.options.getString('message', true);

const userSelectionMsg = await interaction.editReply({
content: 'Which users do you want to send the message to?',
components: [
new ActionRowBuilder<StringSelectMenuBuilder>().addComponents([
dmTargetMenu,
]),
],
});

const collector = userSelectionMsg.createMessageComponentCollector({
filter: (click) => click.user.id === interaction.user.id,
time: 900000,
});

let selectedUserType: string;
let selectedPeriod: string | undefined;

collector.on('collect', async (click) => {
await click.deferUpdate();
switch (click.customId) {
case 'dm-menu': {
if (!click.isStringSelectMenu()) break;
selectedUserType = click.values[0];
if (
selectedUserType === 'ASSIGNED-QUANTIFIERS' ||
selectedUserType === 'UNFINISHED-QUANTIFIERS' ||
selectedUserType === 'RECEIVERS'
) {
let periods: Period[] = [];

try {
const response = await apiGet<PeriodPaginatedResponseDto>(
'periods',
{
headers: { host: host },
}
);
periods = [...response.data.docs];
} catch (err) {
logger.error(err);
await interaction.editReply(
'No praise periods found. Try again after having created a period and quantified some praise.'
);
return;
}

const openPeriods = periods.filter(
(doc) => doc.status === 'QUANTIFY'
);

if (!openPeriods || !openPeriods.length) {
await interaction.editReply({
content: 'Which period are you referring to?',
components: [
new ActionRowBuilder<StringSelectMenuBuilder>().addComponents([
periodSelectMenu(openPeriods),
]),
],
content: 'No periods open for quantification.',
components: [],
});
break;
return;
}
selectedPeriod = '';
await interaction.editReply({
content: `Preview announcement before continuing:\n---\n${message}\n---`,
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents([
continueButton,
cancelButton,
]),
],
});
break;
}
case 'period-menu': {
if (!click.isStringSelectMenu()) return;
selectedPeriod = click.values[0];

await interaction.editReply({
content: `Preview announcement before continuing:\n---\n${message}\n---`,
content: 'Which period are you referring to?',
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents([
continueButton,
cancelButton,
new ActionRowBuilder<StringSelectMenuBuilder>().addComponents([
periodSelectMenu(openPeriods),
]),
],
});
break;
}
case 'continue': {
await interaction.editReply({
content: 'Sending…',
components: [],
});
await selectTargets(
interaction,
selectedUserType,
selectedPeriod,
announcementEmbed(member.user, guild, message),
host
);
break;
}
case 'cancel': {
await interaction.editReply({
content: 'User cancelled Interaction.',
components: [],
});
return;
}
selectedPeriod = '';
await interaction.editReply({
content: `Preview announcement before continuing:\n---\n${message}\n---`,
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents([
continueButton,
cancelButton,
]),
],
});
break;
}
});
case 'period-menu': {
if (!click.isStringSelectMenu()) return;
selectedPeriod = click.values[0];
await interaction.editReply({
content: `Preview announcement before continuing:\n---\n${message}\n---`,
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents([
continueButton,
cancelButton,
]),
],
});
break;
}
case 'continue': {
await interaction.editReply({
content: 'Sending…',
components: [],
});

// debug log
logger.debug(
`Running /admin announce for users: ${selectedUserType} & period: ${
selectedPeriod || 'undefined'
}`
);

collector.on('end', async (collected) => {
const successfulEndEvents = ['cancel', 'continue'];
const ended = collected.some((clk) =>
successfulEndEvents.includes(clk.customId)
);
if (!ended) {
await interaction.followUp({
content: 'Interaction timed out...',
await selectTargets(
interaction,
selectedUserType,
selectedPeriod,
announcementEmbed(member.user, guild, message),
host
);
break;
}
case 'cancel': {
await interaction.editReply({
content: 'User cancelled Interaction.',
embeds: [],
components: [],
});
return;
}
});
} else {
await interaction.editReply({
content:
'You do not have the needed permissions to use this command. If you would like to perform admin actions, you would need to be granted an `ADMIN` role on the Praise Dashboard.',
});
}
}
});

collector.on('end', async (collected) => {
const successfulEndEvents = ['cancel', 'continue'];
const ended = collected.some((clk) =>
successfulEndEvents.includes(clk.customId)
);
if (!ended) {
await interaction.followUp({
content: 'Interaction timed out...',
embeds: [],
components: [],
});
}
});
};
7 changes: 5 additions & 2 deletions packages/discord-bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ discordClient.on('interactionCreate', async (interaction): Promise<void> => {
discordClient,
interaction.guild.id
);
if (community)
console.log(community);

if (community) {
await command.execute(discordClient, interaction, community.hostname);
else
} else {
await interaction.editReply({
embeds: [communityNotCreatedError(process.env.WEB_URL as string)],
});
}
} catch {
logger.error(
`Interaction /${interaction.commandName} failed for ${
Expand Down
Loading