From ed7e49e770af93ec9c92ef9743fb720e7bdd5f69 Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Sat, 26 Nov 2022 16:04:43 +0000 Subject: [PATCH] feat: announce target achievements --- bot/src/server/server.ts | 9 ++++- bot/src/utils/apiRequests.ts | 14 +++++++ bot/src/utils/embeds.ts | 11 +++++- .../{goalAchieved.ts => goalsAchieved.ts} | 2 +- bot/src/webhookHandlers/questAchieved.ts | 39 +++++++++++++++++++ docs/docs/api/webhooks/main.md | 4 +- 6 files changed, 72 insertions(+), 7 deletions(-) rename bot/src/webhookHandlers/{goalAchieved.ts => goalsAchieved.ts} (97%) create mode 100644 bot/src/webhookHandlers/questAchieved.ts diff --git a/bot/src/server/server.ts b/bot/src/server/server.ts index e824f6bdd..3d417fded 100644 --- a/bot/src/server/server.ts +++ b/bot/src/server/server.ts @@ -6,8 +6,9 @@ import { TachiServerV1Request, RequestTypes, TachiServerV1Get } from "../utils/f import { CreateLayeredLogger } from "../utils/logger"; import { VERSION_PRETTY } from "../version"; import { HandleClassUpdateV1 } from "../webhookHandlers/classUpdate"; -import { HandleGoalAchievedV1 } from "../webhookHandlers/goalAchieved"; +import { HandleGoalAchievedV1 } from "../webhookHandlers/goalsAchieved"; import express from "express"; +import { HandleQuestAchievedV1 } from "webhookHandlers/questAchieved"; import path from "path"; import type { Express } from "express"; import type { APITokenDocument, UserDocument, WebhookEvents } from "tachi-common"; @@ -150,7 +151,11 @@ app.post("/webhook", ValidateWebhookRequest, async (req, res) => { break; } - case "quest-achieved/v1": + case "quest-achieved/v1": { + statusCode = await HandleQuestAchievedV1(webhookEvent.content); + break; + } + default: { // get around to updating in time. // to define new webhooks, and the bot might not diff --git a/bot/src/utils/apiRequests.ts b/bot/src/utils/apiRequests.ts index 4f4f58949..1c202b608 100644 --- a/bot/src/utils/apiRequests.ts +++ b/bot/src/utils/apiRequests.ts @@ -12,6 +12,7 @@ import type { integer, PBScoreDocument, Playtype, + QuestDocument, SongDocument, UserDocument, } from "tachi-common"; @@ -54,6 +55,19 @@ export async function GetGoalWithID(goalID: string, game: Game, playtype: Playty return res.body; } +export async function GetQuestWithID(questID: string, game: Game, playtype: Playtype) { + const res = await TachiServerV1Get( + `/games/${game}/${playtype}/targets/quests/${questID}`, + null + ); + + if (!res.success) { + throw new Error(`Failed to fetch goal with ID ${questID}. '${res.description}'.`); + } + + return res.body; +} + export async function GetChartInfoForUser( userID: integer | string, chartID: string, diff --git a/bot/src/utils/embeds.ts b/bot/src/utils/embeds.ts index cca0f46a8..bbfbfe170 100644 --- a/bot/src/utils/embeds.ts +++ b/bot/src/utils/embeds.ts @@ -22,12 +22,19 @@ import type { Playtype, ScoreDocument, UserDocument, + integer, } from "tachi-common"; -export function CreateEmbed() { - return new MessageEmbed() +export function CreateEmbed(userID?: integer) { + const embed = new MessageEmbed() .setColor(ServerConfig.type === "ktchi" ? "#e61c6e" : "#527acc") .setTimestamp(); + + if (userID !== undefined) { + embed.setThumbnail(PrependTachiUrl(`/users/${userID}/pfp`)); + } + + return embed; } export function CreateImportEmbed(importDoc: ImportDocument) { diff --git a/bot/src/webhookHandlers/goalAchieved.ts b/bot/src/webhookHandlers/goalsAchieved.ts similarity index 97% rename from bot/src/webhookHandlers/goalAchieved.ts rename to bot/src/webhookHandlers/goalsAchieved.ts index e3a05cb80..20bc370cb 100644 --- a/bot/src/webhookHandlers/goalAchieved.ts +++ b/bot/src/webhookHandlers/goalsAchieved.ts @@ -38,7 +38,7 @@ export async function HandleGoalAchievedV1( const shouldShowPlaytype = gameConfig.validPlaytypes.length !== 1; - const embed = CreateEmbed() + const embed = CreateEmbed(userDoc.id) .setTitle( `${userDoc.username} just achieved ${event.goals.length} ${Pluralise( event.goals.length, diff --git a/bot/src/webhookHandlers/questAchieved.ts b/bot/src/webhookHandlers/questAchieved.ts new file mode 100644 index 000000000..42d16e5ce --- /dev/null +++ b/bot/src/webhookHandlers/questAchieved.ts @@ -0,0 +1,39 @@ +import { client } from "../main"; +import { GetQuestWithID, GetUserInfo } from "../utils/apiRequests"; +import { CreateEmbed } from "../utils/embeds"; +import logger from "../utils/logger"; +import { GetGameChannel } from "../utils/misc"; +import { GetGameConfig } from "tachi-common"; +import type { integer, WebhookEventQuestAchievedV1 } from "tachi-common"; + +export async function HandleQuestAchievedV1( + event: WebhookEventQuestAchievedV1["content"] +): Promise { + const { game, playtype } = event; + + let channel; + + try { + channel = GetGameChannel(client, game); + } catch (e) { + const err = e as Error; + + logger.error(`ClassUpdate handler failed: ${err.message}`); + return 500; + } + + const userDoc = await GetUserInfo(event.userID); + + const quest = await GetQuestWithID(event.questID, game, playtype); + + const gameConfig = GetGameConfig(game); + const shouldShowPlaytype = gameConfig.validPlaytypes.length > 1 ? ` (${playtype})` : ""; + + const embed = CreateEmbed(userDoc.id).setTitle( + `${userDoc.username} just completed ${quest.name}${shouldShowPlaytype}!` + ); + + await channel.send({ embeds: [embed] }); + + return 200; +} diff --git a/docs/docs/api/webhooks/main.md b/docs/docs/api/webhooks/main.md index 48dbd6750..6e55c9aad 100644 --- a/docs/docs/api/webhooks/main.md +++ b/docs/docs/api/webhooks/main.md @@ -29,5 +29,5 @@ The current Events are: | Type | Description | | :: | :: | | `class-update/v1` | Fires whenever a user has had a class update positively, such as going from 9th Dan to 10th Dan. | - +| `goal-achieved/v1` | Fires whenever a user has achieved a goal. | +| `quest-achieved/v1` | Fires whenever a user has achieved a quest. |