-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeworker.js
51 lines (42 loc) · 1.33 KB
/
typeworker.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
require("dotenv").config();
const procenv = process.env,
{ parentPort } = require("worker_threads"),
Discord = require("discord.js"),
client = new Discord.Client({
intents: ["Guilds", "GuildMessages", "MessageContent"],
}),
logger = (m) => console.log(`[${new Date()}] ${m}`);
/** @type {Discord.Channel[] | []} */
var typingChannels = [];
client.once("ready", () => {
parentPort.on("message", async (message) => {
if (!message[0]) return;
/** @type {Discord.Channel | null} */
var targetChannel,
targetId = message[0];
try {
targetChannel = await client.channels.fetch(targetId);
if (!targetChannel) return;
} catch (e) {
logger(`[WARN] Failed to fetch for typer to ${targetId}, ${e}`);
return;
}
if (!typingChannels.includes(targetChannel))
typingChannels.push(targetChannel);
else
typingChannels = typingChannels.filter((c) => c.id != targetChannel.id);
});
setInterval(() => {
typingChannels.forEach(async (channel) => {
if (
channel.type != Discord.ChannelType.GuildText &&
channel.type != Discord.ChannelType.PublicThread &&
channel.type != Discord.ChannelType.PrivateThread
)
return;
await channel.sendTyping();
});
}, 3000);
logger(`Typer worker ready.`);
});
client.login(procenv.TOKEN);