refactor(discord-bot): move types to their services

This commit is contained in:
Pihkaal
2025-12-04 19:08:27 +01:00
parent 464f367c80
commit 1b53f82e8a
4 changed files with 32 additions and 38 deletions

View File

@@ -1,6 +1,20 @@
import { env } from "~/env"; import { env } from "~/env";
import { redis } from "@lbf-bot/database"; import { redis } from "@lbf-bot/database";
import type { QuestResult } from "~/types";
export type QuestResult = {
quest: {
id: string;
promoImageUrl: string;
promoImagePrimaryColor: string;
};
participants: Array<QuestParticipant>;
};
export type QuestParticipant = {
playerId: string;
username: string;
xp: number;
};
export const getLatestQuest = async (): Promise<QuestResult> => { export const getLatestQuest = async (): Promise<QuestResult> => {
const response = await fetch( const response = await fetch(

View File

@@ -1,30 +0,0 @@
export type QuestResult = {
quest: {
id: string;
promoImageUrl: string;
promoImagePrimaryColor: string;
};
participants: Array<QuestParticipant>;
};
export type QuestParticipant = {
playerId: string;
username: string;
xp: number;
};
export type DiscordMessage = {
content: string;
embeds: Array<DiscordEmbed>;
};
export type DiscordEmbed = {
title?: string;
description: string;
image?: {
url: string;
};
color: number;
};
export type TrackedPlayers = Record<string, string[]>;

View File

@@ -1,16 +1,17 @@
import { getAccountBalance, setAccountBalance } from "~/services/account"; import { getAccountBalance, setAccountBalance } from "~/services/account";
import { env } from "~/env"; import { env } from "~/env";
import type { QuestResult, DiscordMessage, DiscordEmbed } from "~/types"; import type { QuestResult } from "~/services/wov";
import type { MessageCreateOptions, APIEmbed } from "discord.js";
export const makeResultEmbed = async ( export const makeResultEmbed = async (
result: QuestResult, result: QuestResult,
exclude: Array<string>, exclude: Array<string>,
): Promise<DiscordMessage> => { ): Promise<MessageCreateOptions> => {
const imageUrl = result.quest.promoImageUrl; const imageUrl = result.quest.promoImageUrl;
const color = parseInt(result.quest.promoImagePrimaryColor.substring(1), 16); const color = parseInt(result.quest.promoImagePrimaryColor.substring(1), 16);
const participants = result.participants.toSorted((a, b) => b.xp - a.xp); const participants = result.participants.toSorted((a, b) => b.xp - a.xp);
let rewardsEmbed: DiscordEmbed | undefined; let rewardsEmbed: APIEmbed | undefined;
if (env.QUEST_REWARDS) { if (env.QUEST_REWARDS) {
const rewardedParticipants = participants const rewardedParticipants = participants
.map((x) => ({ id: x.playerId, username: x.username })) .map((x) => ({ id: x.playerId, username: x.username }))
@@ -69,7 +70,10 @@ export const makeResultEmbed = async (
}; };
}; };
export const createErrorEmbed = (message: string, color = 15335424) => ({ export const createErrorEmbed = (
message: string,
color = 15335424,
): MessageCreateOptions => ({
embeds: [ embeds: [
{ {
description: `### ❌ Erreur\n\n\n${message}`, description: `### ❌ Erreur\n\n\n${message}`,
@@ -78,7 +82,10 @@ export const createErrorEmbed = (message: string, color = 15335424) => ({
], ],
}); });
export const createSuccessEmbed = (message: string, color = 65280) => ({ export const createSuccessEmbed = (
message: string,
color = 65280,
): MessageCreateOptions => ({
embeds: [ embeds: [
{ {
description: `### ✅ ${message}`, description: `### ✅ ${message}`,
@@ -87,7 +94,10 @@ export const createSuccessEmbed = (message: string, color = 65280) => ({
], ],
}); });
export const createInfoEmbed = (message: string, color = 0x89cff0) => ({ export const createInfoEmbed = (
message: string,
color = 0x89cff0,
): MessageCreateOptions => ({
embeds: [ embeds: [
{ {
description: message, description: message,

View File

@@ -1,7 +1,7 @@
import { ChannelType, type Client, type Message } from "discord.js"; import { ChannelType, type Client, type Message } from "discord.js";
import { env } from "~/env"; import { env } from "~/env";
import { makeResultEmbed } from "~/utils/discord"; import { makeResultEmbed } from "~/utils/discord";
import type { QuestResult } from "~/types"; import type { QuestResult } from "~/services/wov";
export const askForGrinders = async (quest: QuestResult, client: Client) => { export const askForGrinders = async (quest: QuestResult, client: Client) => {
const adminChannel = await client.channels.fetch(env.DISCORD_ADMIN_CHANNEL); const adminChannel = await client.channels.fetch(env.DISCORD_ADMIN_CHANNEL);