From 6adb4ac708a22fe1e68222d0b6f30764e3d3f2ad Mon Sep 17 00:00:00 2001 From: Kristofer Date: Thu, 14 Dec 2023 16:04:12 +0100 Subject: [PATCH] Add direct praise quantification support to the Discord bot --- packages/discord-bot/src/handlers/praise.ts | 80 ++++++++++++++++--- .../discord-bot/src/utils/createPraise.ts | 5 +- packages/discord-bot/src/utils/givePraise.ts | 6 +- 3 files changed, 77 insertions(+), 14 deletions(-) diff --git a/packages/discord-bot/src/handlers/praise.ts b/packages/discord-bot/src/handlers/praise.ts index 11c99ca99..bc6e66529 100644 --- a/packages/discord-bot/src/handlers/praise.ts +++ b/packages/discord-bot/src/handlers/praise.ts @@ -3,6 +3,8 @@ import { GuildMember, ActionRowBuilder, ButtonBuilder, + StringSelectMenuBuilder, + StringSelectMenuOptionBuilder, } from 'discord.js'; import { parseReceivers } from '../utils/parseReceivers'; @@ -15,6 +17,7 @@ import { getUserAccount } from '../utils/getUserAccount'; import { logger } from '../utils/logger'; import { sendActivationMessage } from '../utils/sendActivationMessage'; import { givePraise } from '../utils/givePraise'; +import { getSetting } from '../utils/settingsUtil'; /** * Execute command /praise @@ -28,7 +31,7 @@ export const praiseHandler: CommandHandler = async ( host, responseUrl ) => { - if (!responseUrl) return; + if (!responseUrl || !interaction) return; const { guild, member } = interaction; @@ -160,17 +163,72 @@ export const praiseHandler: CommandHandler = async ( ); } } else { - await givePraise( - interaction, - guild, - member as GuildMember, - giverAccount, - parsedReceivers, - receiverOptions, - reason, - host, - responseUrl + const directQuantificationEnanbled = (await getSetting( + 'DISCORD_BOT_DIRECT_PRAISE_QUANTIFICATION_ENABLED' + )) as boolean; + + // If direct quantification is disabled, give praise directly + // This is the default behavior + if (!directQuantificationEnanbled) { + await givePraise( + interaction, + guild, + member as GuildMember, + giverAccount, + parsedReceivers, + receiverOptions, + reason, + host, + responseUrl + ); + return; + } + + // If direct quantification is enabled, allow user to select a score from a dropdown + const allowedScores = (await getSetting( + 'PRAISE_QUANTIFY_ALLOWED_VALUES' + )) as number[]; + + const select = new StringSelectMenuBuilder() + .setCustomId('score') + .setPlaceholder('Select an impact score!') + .addOptions( + allowedScores.map((score) => + new StringSelectMenuOptionBuilder() + .setLabel(score.toString()) + .setValue(score.toString()) + ) + ); + + const row = new ActionRowBuilder().addComponents( + select ); + + await interaction.followUp({ + content: 'Select an impact score!', + components: [row], + }); + + const collector = interaction.channel?.createMessageComponentCollector({ + componentType: ComponentType.StringSelect, + time: 15000, + }); + + collector?.on('collect', async (menuInteraction) => { + const score = Number(menuInteraction.values[0]); + await givePraise( + interaction, + guild, + member as GuildMember, + giverAccount, + parsedReceivers, + receiverOptions, + reason, + host, + responseUrl, + score + ); + }); } } catch (err) { // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/packages/discord-bot/src/utils/createPraise.ts b/packages/discord-bot/src/utils/createPraise.ts index b5ff9985d..a7db83e17 100644 --- a/packages/discord-bot/src/utils/createPraise.ts +++ b/packages/discord-bot/src/utils/createPraise.ts @@ -18,6 +18,7 @@ interface PraiseCreateInputDto { receiverIds: string[]; sourceId: string; sourceName: string; + score?: number; } export const createPraise = async ( @@ -25,7 +26,8 @@ export const createPraise = async ( giverAccount: UserAccount, receiverAccounts: UserAccount[], reason: string, - host: string + host: string, + score?: number ): Promise => { const { channel, guild } = interaction; if (!channel || !guild || channel.type === ChannelType.DM) return []; @@ -52,6 +54,7 @@ export const createPraise = async ( sourceName: `DISCORD:${encodeURIComponent(guild.name)}:${encodeURIComponent( channelName )}`, + score: score, }; const response = await apiPost( diff --git a/packages/discord-bot/src/utils/givePraise.ts b/packages/discord-bot/src/utils/givePraise.ts index 7a9b913f2..98a951715 100644 --- a/packages/discord-bot/src/utils/givePraise.ts +++ b/packages/discord-bot/src/utils/givePraise.ts @@ -22,7 +22,8 @@ export const givePraise = async ( receiverOptions: string, reason: string, host: string, - responseUrl: string + responseUrl: string, + score?: number ): Promise => { if ( !parsedReceivers.validReceiverIds || @@ -88,7 +89,8 @@ export const givePraise = async ( giverAccount, receivers.map((receiver) => receiver.userAccount), reason, - host + host, + score ); } else if (warnSelfPraise) { await ephemeralWarning(interaction, 'SELF_PRAISE_WARNING', host);