Dernière activité 1 month ago

Révision ef29d7699ff2a8eb07c4c5ab7443c1a85d75f0cd

index.ts Brut
1import { Client, GatewayIntentBits, REST, Routes } from "discord.js";
2import { loadConfig, cfg } from "@systems/config";
3import { loadMessages } from "@systems/messages";
4import { loadEmojis } from "@systems/emojis";
5import { loadCharacters } from "@systems/characters";
6import { loadWRank } from "@systems/wrank";
7import { scheduleSlots } from "@systems/slots";
8import { postPoll, polls, lockPoll, updatePollMessage } from "@systems/poll";
9import { handleInteraction } from "@handlers/interactions";
10import { buildTgCommand } from "@commands/tg";
11import { buildTgConfigCommand } from "@commands/tgConfig";
12import cron from "node-cron"; // npm i node-cron @types/node-cron
13import { TGSlot } from "@src/types";
14
15const TOKEN = process.env.DISCORD_TOKEN!;
16const CLIENT_ID = process.env.CLIENT_ID!;
17const GUILD_ID = process.env.GUILD_ID!;
18
19const client = new Client({
20 intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers],
21});
22
23async function registerCommands(): Promise<void> {
24 const rest = new REST({ version: "10" }).setToken(TOKEN);
25 await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), {
26 body: [buildTgCommand().toJSON(), buildTgConfigCommand().toJSON()],
27 });
28 console.log("Slash commands registered.");
29}
30
31async function onPollOpen(slot: TGSlot): Promise<void> {
32 const channelId = cfg("pollChannelId");
33 const channel = await client.channels.fetch(channelId) as any;
34 if (!channel) return console.error("Poll channel not found.");
35 await postPoll(channel, slot);
36}
37
38async function onPollClose(slot: TGSlot): Promise<void> {
39 const state = polls.get(slot.tgHour);
40 if (!state) return;
41 state.locked = true;
42 const channelId = cfg("pollChannelId");
43 const channel = await client.channels.fetch(channelId) as any;
44 if (!channel) return;
45 const { updatePollMessage } = require("@systems/poll");
46 await updatePollMessage(channel, slot.tgHour);
47 console.log(`[${new Date().toISOString()}] Poll closed for ${slot.tgHour}:00.`);
48}
49
50client.on("interactionCreate", handleInteraction);
51
52client.once("clientReady", async () => {
53 console.log(`Logged in as ${client.user!.tag}`);
54
55 // Load all data
56 loadConfig();
57 loadMessages();
58 loadEmojis();
59 loadCharacters();
60 loadWRank();
61
62 // Warm member cache
63 const guild = await client.guilds.fetch(GUILD_ID);
64 await guild.members.fetch();
65 console.log(`Member cache warmed: ${guild.members.cache.size} members`);
66
67 // Register commands if --register flag passed
68 if (process.argv.includes("--register")) {
69 await registerCommands();
70 }
71
72 // Schedule slots
73 scheduleSlots(client, onPollOpen, onPollClose);
74
75 console.log("Bot ready.");
76});
77
78client.login(TOKEN);
79