mirror of
https://github.com/zkldi/Tachi.git
synced 2026-09-22 23:18:05 +03:00
feat: announce target achievements
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<QuestDocument>(
|
||||
`/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,
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
@@ -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<integer> {
|
||||
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;
|
||||
}
|
||||
@@ -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. | -->
|
||||
| `goal-achieved/v1` | Fires whenever a user has achieved a goal. |
|
||||
| `quest-achieved/v1` | Fires whenever a user has achieved a quest. |
|
||||
|
||||
Reference in New Issue
Block a user