refactor(discord-bot): move differents modes in separate files

This commit is contained in:
Pihkaal
2025-12-04 18:43:53 +01:00
parent 2ad0fcc623
commit 464f367c80
4 changed files with 145 additions and 94 deletions

View File

@@ -1,29 +1,12 @@
import { env } from "~/env"; import { env } from "~/env";
import { listTrackedPlayers, trackWovPlayer } from "~/services/tracking"; import { Client, GatewayIntentBits, Partials } from "discord.js";
import { checkForNewQuest } from "~/services/wov"; import { setupBotMode } from "~/modes/bot";
import { createInfoEmbed } from "~/utils/discord"; import { setupUserMode } from "~/modes/user";
import { askForGrinders } from "~/utils/quest"; import { parseArgs } from "~/utils/cli";
import { commands } from "~/commands";
import { ChannelType, Client, GatewayIntentBits, Partials } from "discord.js"; const mode = parseArgs(process.argv.slice(2));
import * as readline from "node:readline";
// user mode = write in console, send in channel console.log(`Mode: ${mode.type}`);
const flagIndex = process.argv.indexOf("--user");
let userMode: { enabled: true; channelId: string } | { enabled: false } = {
enabled: false,
};
if (flagIndex !== -1) {
const channelId = process.argv[flagIndex + 1];
if (channelId === undefined) {
console.error("ERROR: --user expects channelId as a paramater");
process.exit(1);
}
userMode = { enabled: true, channelId };
}
console.log(`User mode: ${userMode.enabled ? "enabled" : "disabled"}`);
const client = new Client({ const client = new Client({
intents: [ intents: [
@@ -35,81 +18,22 @@ const client = new Client({
partials: [Partials.Message, Partials.Channel], partials: [Partials.Message, Partials.Channel],
}); });
const questCheckCron = async () => { switch (mode.type) {
const quest = await checkForNewQuest(); case "user": {
if (quest) { setupUserMode(client, mode.channelId);
await askForGrinders(quest, client); break;
} }
};
const trackingCron = async () => { case "bot": {
const trackedPlayers = await listTrackedPlayers(); setupBotMode(client);
for (const playerId of trackedPlayers) { break;
const res = await trackWovPlayer(playerId);
if (res.event !== "changed") return;
const chan = client.channels.cache.get(env.DISCORD_TRACKING_CHANNEL);
if (!chan?.isSendable()) throw "Invalid tracking channel";
const lastUsername = res.oldUsernames[res.oldUsernames.length - 1];
await chan.send(
createInfoEmbed(
`### [UPDATE] \`${lastUsername}\` -> \`${res.newUsername}\` [\`${playerId}\`]\n\n**Nouveau pseudo:** \`${res.newUsername}\`\n**Anciens pseudos:**\n${res.oldUsernames.map((x) => `- \`${x}\``).join("\n")}`,
),
);
} }
};
client.on("ready", async (client) => { default: {
console.log(`Logged in as ${client.user.username}`); // @ts-ignore
console.error(`ERROR: Not implemented: '${mode.type}'`);
if (userMode.enabled) {
const chan = client.channels.cache.get(userMode.channelId);
if (chan?.type !== ChannelType.GuildText) {
console.error("ERROR: invalid channel");
process.exit(1); process.exit(1);
} }
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: `${chan.name} ~ `,
});
rl.prompt();
rl.on("line", async (line) => {
await chan.send(line);
rl.prompt();
});
rl.on("close", () => {
process.exit(0);
});
} else {
await questCheckCron();
setInterval(questCheckCron, env.WOV_FETCH_INTERVAL);
await trackingCron();
setInterval(trackingCron, env.WOV_TRACKING_INTERVAL);
}
});
client.on("messageCreate", async (message) => {
if (message.author.bot || userMode.enabled) return;
if (message.content.startsWith(`<@${client.user!.id}>`)) {
const [command, ...args] = message.content
.replace(`<@${client.user!.id}>`, "")
.trim()
.split(" ");
const commandHandler = commands[command];
if (commandHandler) {
await commandHandler(message, args);
}
}
});
await client.login(env.DISCORD_BOT_TOKEN); await client.login(env.DISCORD_BOT_TOKEN);

View File

@@ -0,0 +1,61 @@
import type { Client } from "discord.js";
import { env } from "~/env";
import { listTrackedPlayers, trackWovPlayer } from "~/services/tracking";
import { checkForNewQuest } from "~/services/wov";
import { createInfoEmbed } from "~/utils/discord";
import { askForGrinders } from "~/utils/quest";
import { commands } from "~/commands";
const questCheckCron = async (client: Client) => {
const quest = await checkForNewQuest();
if (quest) {
await askForGrinders(quest, client);
}
};
const trackingCron = async (client: Client) => {
const trackedPlayers = await listTrackedPlayers();
for (const playerId of trackedPlayers) {
const res = await trackWovPlayer(playerId);
if (res.event !== "changed") return;
const chan = client.channels.cache.get(env.DISCORD_TRACKING_CHANNEL);
if (!chan?.isSendable()) throw "Invalid tracking channel";
const lastUsername = res.oldUsernames[res.oldUsernames.length - 1];
await chan.send(
createInfoEmbed(
`### [UPDATE] \`${lastUsername}\` -> \`${res.newUsername}\` [\`${playerId}\`]\n\n**Nouveau pseudo:** \`${res.newUsername}\`\n**Anciens pseudos:**\n${res.oldUsernames.map((x) => `- \`${x}\``).join("\n")}`,
),
);
}
};
export const setupBotMode = (client: Client) => {
client.on("clientReady", async (client) => {
console.log(`Logged in as ${client.user.username}`);
await questCheckCron(client);
setInterval(() => questCheckCron(client), env.WOV_FETCH_INTERVAL);
await trackingCron(client);
setInterval(() => trackingCron(client), env.WOV_TRACKING_INTERVAL);
});
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (message.content.startsWith(`<@${client.user!.id}>`)) {
const [command, ...args] = message.content
.replace(`<@${client.user!.id}>`, "")
.trim()
.split(" ");
const commandHandler = commands[command];
if (commandHandler) {
await commandHandler(message, args);
}
}
});
};

View File

@@ -0,0 +1,34 @@
import type { Client, TextChannel } from "discord.js";
import { ChannelType } from "discord.js";
import * as readline from "node:readline";
export const setupUserMode = (client: Client, channelId: string) => {
client.on("clientReady", (client) => {
console.log(`Logged in as ${client.user.username}`);
const chan = client.channels.cache.get(channelId);
if (chan?.type !== ChannelType.GuildText) {
console.error("ERROR: invalid channel");
process.exit(1);
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: `${chan.name} ~ `,
});
rl.prompt();
rl.on("line", async (line) => {
if (line.trim().length > 0) {
await (chan as TextChannel).send(line);
}
rl.prompt();
});
rl.on("close", () => {
process.exit(0);
});
});
};

View File

@@ -0,0 +1,32 @@
type Mode = { type: "bot" } | { type: "user"; channelId: string };
export const parseArgs = (args: string[]): Mode => {
let mode: Mode = { type: "bot" };
for (let i = 0; i < args.length; i++) {
const arg = args[i];
switch (arg) {
case "-u":
case "--as-user": {
const channelId = args[(i += 1)];
if (!channelId) {
console.error(`ERROR: ${arg} requires a channel ID`);
process.exit(1);
}
mode = { type: "user", channelId };
break;
}
default: {
console.error(`ERROR: Unrecognized argument: '${arg}'`);
process.exit(1);
}
}
}
return mode;
};