import { Client, Message } from "discord.js"; import { CHARACTER_LIST_MESSAGE_ID, CHARACTER_LIST_CHANNEL_ID, } from "../../../shared"; import { log, LogLevel } from "../../../shared/log"; interface ClaimedCharacter { name: string; mention: string; } interface UnclaimedCharacter { name: string; } interface CharacterListData { claimed: ClaimedCharacter[]; unclaimed: UnclaimedCharacter[]; unregistered: string[]; } export const fetchCharacterList = async ( client: Client, ): Promise => { try { const channel = await client.channels.fetch(CHARACTER_LIST_CHANNEL_ID); if (!channel || !channel.isTextBased() || channel.isDMBased()) { log( `Failed to fetch character list channel: ${CHARACTER_LIST_CHANNEL_ID}`, LogLevel.Error, ); return null; } const message = await channel.messages.fetch(CHARACTER_LIST_MESSAGE_ID); if (!message) { log( `Failed to fetch character list message: ${CHARACTER_LIST_MESSAGE_ID}`, LogLevel.Error, ); return null; } const claimed: ClaimedCharacter[] = []; const unclaimed: UnclaimedCharacter[] = []; const unregistered: string[] = []; for (const embed of message.embeds) { const description = embed.description || embed.fields.map((f) => f.value).join("\n"); if (!description) continue; const lines = description.split("\n"); for (const line of lines) { const trimmed = line.trim(); if ( !trimmed || trimmed.startsWith(".") || trimmed.startsWith("☆") || !trimmed.includes("►") ) continue; const match = trimmed.match(/^(.+?)\s*[►➤]\s*(?:<@(\d+)>)?$/); if (match) { const [, characterName] = match; const mention = match[2]; if (mention) { claimed.push({ name: characterName.trim(), mention: mention.trim(), }); } else { unclaimed.push({ name: characterName.trim() }); } } } } return { claimed, unclaimed, unregistered }; } catch (error) { log(`Failed to fetch character list: ${error}`, LogLevel.Error); return null; } }; export const getCharacterNameFromMessage = async ( message: Message, ): Promise => { if (message.webhookId) return message.author.username; if (message.author.bot) return message.author.username; let member = message.member; if (!member && message.guild) try { member = await message.guild.members.fetch(message.author.id); } catch { return message.author.username; } if (member?.displayName) return member.displayName; return message.author.username; };