summaryrefslogtreecommitdiff
path: root/packages/gateway/src/commands/characterClaimParser.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gateway/src/commands/characterClaimParser.ts')
-rw-r--r--packages/gateway/src/commands/characterClaimParser.ts117
1 files changed, 117 insertions, 0 deletions
diff --git a/packages/gateway/src/commands/characterClaimParser.ts b/packages/gateway/src/commands/characterClaimParser.ts
new file mode 100644
index 0000000..84fab04
--- /dev/null
+++ b/packages/gateway/src/commands/characterClaimParser.ts
@@ -0,0 +1,117 @@
+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<CharacterListData | null> => {
+ 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<string | null> => {
+ 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;
+};