-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cjs
138 lines (127 loc) · 3.58 KB
/
index.cjs
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
const cron = require("node-cron");
const {
Client,
GatewayIntentBits,
Collection,
Partials,
EmbedBuilder,
} = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
],
partials: [Partials.Channel],
ws: {
properties: {
$os: "Uni OS",
$browser: "Uni Browser",
$device: "K8s on Proxmox VE 6.4 in Sibainu",
},
},
});
const fs = require("fs");
const config = require("./config.js");
const functions = {
timeUtils: require("./lib/timeUtils.js"),
logUtils: require("./lib/logUtils.js"),
};
client.conf = config;
client.func = functions;
client.fs = fs;
const cmdH = require(`./lib/commandUtils.js`);
client.commands = new Collection();
cmdH.handling(client, fs, Collection, config);
const eventFiles = fs
.readdirSync("./events")
.filter((file) => file.endsWith(".js"));
for (const file of eventFiles) {
const event = require(`./events/${file}`);
if (event.once) {
try {
client.once(event.name, (...args) => event.execute(...args, client));
} catch (error) {
console.error(
`\u001b[31m[${functions.timeUtils.timeToJST(
Date.now(),
true
)}]\u001b[0m\n`,
error
);
}
} else {
try {
client.on(event.name, (...args) => event.execute(...args, client));
} catch (error) {
console.error(
`\u001b[31m[${functions.timeUtils.timeToJST(
Date.now(),
true
)}]\u001b[0m\n`,
error
);
}
}
}
const { rssGet } = require("./lib/rss.cjs");
cron.schedule("*/1-59 * * * *", async () => {
console.log("Cron job start");
const files = fs.readdirSync("./log/v1/feed");
for (const file of files) {
const datas = await JSON.parse(
fs.readFileSync(`./log/v1/feed/${file}.log`)
).data;
datas.forEach(async (data, index) => {
console.log(data.url);
const items = await rssGet(data.url);
const channel = client.channels.cache.get(file.replace(".log", ""));
for (const item of items) {
if (item.pubDate <= data.lastDate) continue;
console.log("send");
const embed = new EmbedBuilder()
.setTitle(item.title)
.setURL(item.link)
.setDescription(item.content)
.setColor(config.color.s)
.setTimestamp();
channel.send({ embeds: [embed] });
datas[index].lastDate = items[0].pubDate;
functions.logUtils.loging(datas, `v1/feed/${file}`);
}
});
}
});
client.login(config.token);
// エラー処理 (これ入れないとエラーで落ちる。本当は良くないかもしれない)
process.on("uncaughtException", (error) => {
console.error(
`[${functions.timeUtils.timeToJST(Date.now(), true)}] ${error.stack}`
);
const embed = new EmbedBuilder()
.setTitle("ERROR - uncaughtException")
.setDescription("```\n" + error.stack + "\n```")
.setColor(config.color.e)
.setTimestamp();
client.channels
.fetch(config.logch.error)
.then((c) => c.send({ embeds: [embed] }));
});
process.on("unhandledRejection", (reason, promise) => {
console.error(
`\u001b[31m[${functions.timeUtils.timeToJST(
Date.now(),
true
)}] ${reason}\u001b[0m\n`,
promise
);
const embed = new EmbedBuilder()
.setTitle("ERROR - unhandledRejection")
.setDescription("```\n" + reason + "\n```")
.setColor(config.color.e)
.setTimestamp();
client.channels
.fetch(config.logch.error)
.then((c) => c.send({ embeds: [embed] }));
});