Skip to content

Commit

Permalink
Add direct praise quantification support
Browse files Browse the repository at this point in the history
to the Discord bot
  • Loading branch information
kristoferlund committed Dec 14, 2023
1 parent 814650e commit 6adb4ac
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 14 deletions.
80 changes: 69 additions & 11 deletions packages/discord-bot/src/handlers/praise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
GuildMember,
ActionRowBuilder,
ButtonBuilder,
StringSelectMenuBuilder,
StringSelectMenuOptionBuilder,
} from 'discord.js';
import { parseReceivers } from '../utils/parseReceivers';

Expand All @@ -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
Expand All @@ -28,7 +31,7 @@ export const praiseHandler: CommandHandler = async (
host,
responseUrl
) => {
if (!responseUrl) return;
if (!responseUrl || !interaction) return;

const { guild, member } = interaction;

Expand Down Expand Up @@ -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<StringSelectMenuBuilder>().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
Expand Down
5 changes: 4 additions & 1 deletion packages/discord-bot/src/utils/createPraise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ interface PraiseCreateInputDto {
receiverIds: string[];
sourceId: string;
sourceName: string;
score?: number;
}

export const createPraise = async (
interaction: ChatInputCommandInteraction,
giverAccount: UserAccount,
receiverAccounts: UserAccount[],
reason: string,
host: string
host: string,
score?: number
): Promise<Praise[]> => {
const { channel, guild } = interaction;
if (!channel || !guild || channel.type === ChannelType.DM) return [];
Expand All @@ -52,6 +54,7 @@ export const createPraise = async (
sourceName: `DISCORD:${encodeURIComponent(guild.name)}:${encodeURIComponent(
channelName
)}`,
score: score,
};

const response = await apiPost<Praise[], PraiseCreateInputDto>(
Expand Down
6 changes: 4 additions & 2 deletions packages/discord-bot/src/utils/givePraise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const givePraise = async (
receiverOptions: string,
reason: string,
host: string,
responseUrl: string
responseUrl: string,
score?: number
): Promise<void> => {
if (
!parsedReceivers.validReceiverIds ||
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6adb4ac

Please sign in to comment.