-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
103 lines (79 loc) · 3.56 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Client, EmbedBuilder, GatewayIntentBits, Partials } from "discord.js";
import dotenv from "dotenv";
import fetch from "node-fetch";
dotenv.config({ path: ".env" });
const { DISCORD_TOKEN } = process.env;
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
partials: [Partials.Message, Partials.GuildScheduledEvent, Partials.Channel, Partials.Reaction],
});
client.once("ready", () => { console.log("Bot is ready!"); });
client.on("interactionCreate", async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === "cf") {
await interaction.deferReply();
const contestId = interaction.options.getInteger("contest");
const forumChannelName = interaction.options.getString("forum");
try {
const response = await fetch(`https://codeforces.com/api/contest.standings?contestId=${contestId}&from=1&count=1`);
const data = await response.json();
console.log(data);
if (data.status !== "OK") {
await interaction.editReply("Failed to retrieve contest data.");
return;
}
const { contest, problems } = data.result;
const problemMap = new Map();
problems.forEach((problem) => {
const baseIndex = problem.index.replace(/[0-9]/g, "").trim();
const baseName = problem.name.match(/^[^(]+/g)[0].trim();
const subName = (problem.name.match(/\(([^)]+)\)/) || [])[1] || baseName;
if (!problemMap.has(baseIndex)) {
problemMap.set(baseIndex, { baseName: baseName, subproblems: [] });
}
problemMap.get(baseIndex).subproblems.push({name: subName, index: problem.index });
});
console.log("Problems in the round: ", problemMap);
const forumChannel = interaction.guild.channels.cache.find(
(channel) => channel.name === forumChannelName && channel.type === 15 // Forum channel type
);
if (!forumChannel) {
await interaction.editReply(`Forum channel "${forumChannelName}" not found.`);
return;
}
const todoId = forumChannel.availableTags.find((tag) => tag.name == 'Todo')?.id;
let tags = [];
if (todoId) tags.push(todoId);
let threads = [];
for (const [baseIndex, problem] of problemMap.entries()) {
const infoEmbed = new EmbedBuilder()
.setColor(0x222222)
.setTitle(problem.baseName)
.setFooter({ text: contest.name });
if (problem.subproblems.length > 1) {
for (const subproblem of problem.subproblems) {
infoEmbed.addFields({
name: subproblem.name,
value: `https://codeforces.com/contest/${contest.id}/problem/${subproblem.index}`,
});
}
} else {
infoEmbed.setDescription(`https://codeforces.com/contest/${contest.id}/problem/${problem.subproblems[0].index}`);
}
const thread = await forumChannel.threads.create({
name: `CF ${contest.id}${baseIndex} - ${problem.baseName}`,
message: { content: `Send your code and discuss in the comments! :)`, embeds: [infoEmbed] },
appliedTags: tags,
autoArchiveDuration: 1440,
reason: '',
});
threads.push(`- ${thread}`);
}
await interaction.editReply(`Threads for ${contest.name}:` + "\n" + threads.join("\n"));
} catch (error) {
console.error("Error fetching contest data:", error);
await interaction.editReply("An error occurred while fetching the contest data.");
}
}
});
client.login(DISCORD_TOKEN);