From c2c452a9654689788fd6fcaf6eb73b9168fd8362 Mon Sep 17 00:00:00 2001 From: Nebs Date: Mon, 18 Jul 2022 10:07:06 +0200 Subject: [PATCH 1/5] Added bot message for the first time praisers --- .../17_settings_first_time_praiser.ts | 35 +++++++++++++++++++ packages/discord-bot/src/handlers/praise.ts | 8 +++++ .../discord-bot/src/utils/praiseEmbeds.ts | 13 +++++++ 3 files changed, 56 insertions(+) create mode 100644 packages/api/src/database/migrations/17_settings_first_time_praiser.ts diff --git a/packages/api/src/database/migrations/17_settings_first_time_praiser.ts b/packages/api/src/database/migrations/17_settings_first_time_praiser.ts new file mode 100644 index 000000000..a1ba99248 --- /dev/null +++ b/packages/api/src/database/migrations/17_settings_first_time_praiser.ts @@ -0,0 +1,35 @@ +import { SettingGroup } from '@/settings/types'; +import { SettingsModel } from '../../settings/entities'; + +const settings = [ + { + key: 'FIRST_TIME_PRAISER', + value: + '**⚠️ We noticed this is the first time you praise**\n Here is some good info about what makes good Praise.', + type: 'Textarea', + label: 'First Time Praiser Message', + description: 'Make a great new praise!', + group: SettingGroup.DISCORD, + }, +]; + +const up = async (): Promise => { + const settingUpdates = settings.map((s) => ({ + updateOne: { + filter: { key: s.key }, + + // Insert setting if not found, otherwise continue + update: { $setOnInsert: { ...s } }, + upsert: true, + }, + })); + + await SettingsModel.bulkWrite(settingUpdates); +}; + +const down = async (): Promise => { + const allKeys = settings.map((s) => s.key); + await SettingsModel.deleteMany({ key: { $in: allKeys } }); +}; + +export { up, down }; diff --git a/packages/discord-bot/src/handlers/praise.ts b/packages/discord-bot/src/handlers/praise.ts index 0d9939640..ab82a08c0 100644 --- a/packages/discord-bot/src/handlers/praise.ts +++ b/packages/discord-bot/src/handlers/praise.ts @@ -15,6 +15,7 @@ import { roleMentionWarning, undefinedReceiverWarning, selfPraiseWarning, + firstTimePraiserInfo, } from '../utils/praiseEmbeds'; import { assertPraiseGiver } from '../utils/assertPraiseGiver'; import { CommandHandler } from '../interfaces/CommandHandler'; @@ -67,6 +68,9 @@ export const praiseHandler: CommandHandler = async ( } const userAccount = await getUserAccount(member as GuildMember); + const praiseItems = await PraiseModel.find({ + giver: userAccount._id, + }); if (!userAccount.user) { await interaction.editReply(await notActivatedError()); @@ -180,5 +184,9 @@ export const praiseHandler: CommandHandler = async ( await msg.reply(await selfPraiseWarning()); } + if (praiseItems.length > 0) { + await msg.reply(await firstTimePraiserInfo()); + } + return; }; diff --git a/packages/discord-bot/src/utils/praiseEmbeds.ts b/packages/discord-bot/src/utils/praiseEmbeds.ts index 52405b1a0..97b3ebffe 100644 --- a/packages/discord-bot/src/utils/praiseEmbeds.ts +++ b/packages/discord-bot/src/utils/praiseEmbeds.ts @@ -262,3 +262,16 @@ export const selfPraiseWarning = async (): Promise => { } return 'SELF-PRAISE NOT ALLOWED, PRAISE GIVERS UNABLE TO PRAISE THEMSELVES (message not set)'; }; + +/** + * Generate response info message FIRST_TIME_PRAISER + * + * @returns {Promise} + */ +export const firstTimePraiserInfo = async (): Promise => { + const msg = (await settingValue('FIRST_TIME_PRAISER')) as string; + if (msg) { + return msg; + } + return 'FIRST TIME PRAISER MESSAGE'; +}; From 07463a8398499ef48c5fd15e6ebe32819b3d566c Mon Sep 17 00:00:00 2001 From: kristoferlund Date: Tue, 19 Jul 2022 11:24:24 +0200 Subject: [PATCH 2/5] Updated message --- .../src/database/migrations/17_settings_first_time_praiser.ts | 2 +- packages/discord-bot/src/handlers/praise.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api/src/database/migrations/17_settings_first_time_praiser.ts b/packages/api/src/database/migrations/17_settings_first_time_praiser.ts index a1ba99248..f32887883 100644 --- a/packages/api/src/database/migrations/17_settings_first_time_praiser.ts +++ b/packages/api/src/database/migrations/17_settings_first_time_praiser.ts @@ -5,7 +5,7 @@ const settings = [ { key: 'FIRST_TIME_PRAISER', value: - '**⚠️ We noticed this is the first time you praise**\n Here is some good info about what makes good Praise.', + '**😃 We noticed this is the first time you praise!**\n Find here some info about what makes good Praise: https://givepraise.xyz', type: 'Textarea', label: 'First Time Praiser Message', description: 'Make a great new praise!', diff --git a/packages/discord-bot/src/handlers/praise.ts b/packages/discord-bot/src/handlers/praise.ts index ab82a08c0..abf73a19e 100644 --- a/packages/discord-bot/src/handlers/praise.ts +++ b/packages/discord-bot/src/handlers/praise.ts @@ -184,7 +184,7 @@ export const praiseHandler: CommandHandler = async ( await msg.reply(await selfPraiseWarning()); } - if (praiseItems.length > 0) { + if (praiseItems.length === 0) { await msg.reply(await firstTimePraiserInfo()); } From 5a4992e8712b65767e0fa76d22c8da75f2993219 Mon Sep 17 00:00:00 2001 From: kristoferlund Date: Mon, 25 Jul 2022 16:10:17 +0200 Subject: [PATCH 3/5] Added to default firstTimePraiserInfo msg --- packages/discord-bot/src/utils/praiseEmbeds.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord-bot/src/utils/praiseEmbeds.ts b/packages/discord-bot/src/utils/praiseEmbeds.ts index 97b3ebffe..594b04275 100644 --- a/packages/discord-bot/src/utils/praiseEmbeds.ts +++ b/packages/discord-bot/src/utils/praiseEmbeds.ts @@ -273,5 +273,5 @@ export const firstTimePraiserInfo = async (): Promise => { if (msg) { return msg; } - return 'FIRST TIME PRAISER MESSAGE'; + return 'FIRST TIME PRAISER MESSAGE (message not set)'; }; From cf0901707c1ba4e3c9855bd66aeb7d95ef7ef894 Mon Sep 17 00:00:00 2001 From: kristoferlund Date: Mon, 25 Jul 2022 16:19:02 +0200 Subject: [PATCH 4/5] Changing firstTimePraiserInfo message --- packages/discord-bot/src/utils/praiseEmbeds.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord-bot/src/utils/praiseEmbeds.ts b/packages/discord-bot/src/utils/praiseEmbeds.ts index 594b04275..2fc14cd32 100644 --- a/packages/discord-bot/src/utils/praiseEmbeds.ts +++ b/packages/discord-bot/src/utils/praiseEmbeds.ts @@ -273,5 +273,5 @@ export const firstTimePraiserInfo = async (): Promise => { if (msg) { return msg; } - return 'FIRST TIME PRAISER MESSAGE (message not set)'; + return 'YOU ARE PRAISING FOR THE FIRST TIME. WELCOME TO PRAISE! (message not set)'; }; From c52fc5ce0d4f873fdd5e859e0f719b3967f2dec4 Mon Sep 17 00:00:00 2001 From: Nebs Date: Tue, 26 Jul 2022 10:29:18 +0200 Subject: [PATCH 5/5] use less expensive call to count praise items for the praise giver --- packages/discord-bot/src/handlers/praise.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/discord-bot/src/handlers/praise.ts b/packages/discord-bot/src/handlers/praise.ts index abf73a19e..374adc63d 100644 --- a/packages/discord-bot/src/handlers/praise.ts +++ b/packages/discord-bot/src/handlers/praise.ts @@ -68,7 +68,7 @@ export const praiseHandler: CommandHandler = async ( } const userAccount = await getUserAccount(member as GuildMember); - const praiseItems = await PraiseModel.find({ + const praiseItemsCount = await PraiseModel.countDocuments({ giver: userAccount._id, }); @@ -184,7 +184,7 @@ export const praiseHandler: CommandHandler = async ( await msg.reply(await selfPraiseWarning()); } - if (praiseItems.length === 0) { + if (praiseItemsCount === 0) { await msg.reply(await firstTimePraiserInfo()); }