-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.mjs
More file actions
197 lines (173 loc) · 6.92 KB
/
Copy pathmain.mjs
File metadata and controls
197 lines (173 loc) · 6.92 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import fs from "fs";
import path from "path";
import express from "express";
import { Client, Collection, GatewayIntentBits, ActivityType, EmbedBuilder } from "discord.js";
import CommandsRegister from "./regist-commands.mjs";
import AtcNotifications from "./models/AtcNotifications.mjs";
import AtcFeeds from "./models/AtcFeeds.mjs";
import fetch from "node-fetch";
import { JSDOM } from "jsdom";
import Sequelize from "sequelize";
if (process.env.RUN_APP === "true") {
const app = express();
app.listen(3000, () => {
console.log("Express server listening on port 3000");
});
app.post("/", (req, res) => {
console.log("Received POST request.");
trigger();
res.send("POST response by glitch");
});
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.DirectMessages,
],
});
client.commands = new Collection();
const categoryFoldersPath = path.join(process.cwd(), "commands");
const commandFolders = fs.readdirSync(categoryFoldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(categoryFoldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter((file) => file.endsWith(".mjs"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
import(filePath).then((module) => {
client.commands.set(module.data.name, module);
});
}
}
const handlers = new Map();
const handlersPath = path.join(process.cwd(), "handlers");
const handlerFiles = fs.readdirSync(handlersPath).filter((file) => file.endsWith(".mjs"));
for (const file of handlerFiles) {
const filePath = path.join(handlersPath, file);
import(filePath).then((module) => {
handlers.set(file.slice(0, -4), module.default);
});
}
client.on("interactionCreate", async (interaction) => {
const handler = handlers.get("interactionCreate");
if (handler && typeof handler === "function") {
await handler(interaction);
} else {
console.warn(`Handler for interactionCreate is not a function`);
}
});
client.on("ready", async () => {
await client.user.setActivity("🥔", { type: ActivityType.Custom, state: "精進中" });
console.log(`${client.user.tag} がログインしました!`);
});
AtcFeeds.sync();
AtcNotifications.sync();
CommandsRegister();
client.login(process.env.TOKEN);
async function trigger() {
const AtcNofications = await AtcNotifications.findAll({
attributes: [[Sequelize.fn("DISTINCT", Sequelize.col("userId")), "userId"]],
});
await Promise.all(
AtcNofications.map(async (n) => {
checkFeed(n.userId);
})
);
}
async function checkFeed(userId) {
const atcFeed = await AtcFeeds.findOne({
where: { userId: userId },
});
let AlgorithmLatestUpdateDate = new Date(atcFeed.AlgorithmLatestUpdateDate),
HueristicLatestUpdateDate = new Date(atcFeed.HueristicLatestUpdateDate);
let AlgorithmLatestRating = Number(atcFeed.AlgorithmLatestRating),
HueristicLatestRating = Number(atcFeed.HueristicLatestRating);
const AlgorithmPreRating = AlgorithmLatestRating,
HueristicPreRating = HueristicLatestRating;
const AlgorithmPreUpdateDate = AlgorithmLatestUpdateDate,
HueristicPreUpdateDate = HueristicLatestUpdateDate;
const url = `https://atcoder.jp/users/${userId}/history`;
const Algorithmhtml = await fetch(url + `?contestType=algo`).then((res) => res.text()),
Hueristichtml = await fetch(url + `?contestType=heuristic`).then((res) => res.text());
if(Algorithmhtml.includes('404 Not Found') || Hueristichtml.includes('404 Not Found')) {
await AtcFeeds.destroy({
where: { userId: userId }
});
await AtcNotifications.destroy({
where: { userId: userId }
});
return;
}
const Algorithmdoc = new JSDOM(Algorithmhtml).window.document,
Hueristicdoc = new JSDOM(Hueristichtml).window.document;
for (let i = 0; i < 2; i++) {
const table = i === 0 ? Algorithmdoc.querySelectorAll("tr") : Hueristicdoc.querySelectorAll("tr");
const lastRow = table[table.length - 1];
if (!lastRow) continue;
const spanElements = lastRow.querySelectorAll("span");
if (!spanElements[2] || spanElements[2].textContent.trim() === "") continue;
const latestRating = Number(spanElements[2].textContent);
const timeElements = lastRow.querySelectorAll("time");
if (!timeElements || timeElements.length === 0) continue;
const latestUpdateDate = new Date(timeElements[0].textContent);
if (i === 0) {
if (latestUpdateDate > AlgorithmLatestUpdateDate) {
AlgorithmLatestUpdateDate = latestUpdateDate;
AlgorithmLatestRating = latestRating;
}
} else {
if (latestUpdateDate > HueristicLatestUpdateDate) {
HueristicLatestUpdateDate = latestUpdateDate;
HueristicLatestRating = latestRating;
}
}
}
const notifications = await AtcNotifications.findAll({
where: { userId: userId },
});
if (AlgorithmLatestUpdateDate > AlgorithmPreUpdateDate) {
const embed = new EmbedBuilder()
.setColor(0x5cb85c)
.setTitle(`AtCoder ユーザー ${userId} のレートが更新されました!`)
.setDescription(
`Algorithm: ${AlgorithmPreRating} -> ${AlgorithmLatestRating}\nhttps://atcoder.jp/users/${userId}`
);
notifications.forEach((n) => {
const channel = client.channels.cache.get(n.textChannelId);
if (channel) {
channel.send({ embeds: [embed] });
} else {
console.warn(`Channel not found for textChannelId: ${n.textChannelId}`);
}
});
}
if (HueristicLatestUpdateDate > HueristicPreUpdateDate) {
const embed = new EmbedBuilder()
.setColor(0x5cb85c)
.setTitle(`AtCoder ユーザー ${userId} のレートが更新されました!`)
.setDescription(
`Hueristic: ${HueristicPreRating} -> ${HueristicLatestRating}\nhttps://atcoder.jp/users/${userId}`
);
notifications.forEach((n) => {
const channel = client.channels.cache.get(n.textChannelId);
if (channel) {
channel.send({ embeds: [embed] });
} else {
console.warn(`Channel not found for textChannelId: ${n.textChannelId}`);
}
});
}
await AtcFeeds.update(
{
AlgorithmLatestUpdateDate: AlgorithmLatestUpdateDate.toISOString(),
AlgorithmLatestRating: AlgorithmLatestRating,
HueristicLatestUpdateDate: HueristicLatestUpdateDate.toISOString(),
HueristicLatestRating: HueristicLatestRating,
},
{ where: { userId: userId } }
);
}
} else {
console.log("アプリケーションは停止中です。RUN_APP を 'true' に設定して起動してください。");
}