-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
42 lines (35 loc) · 1.12 KB
/
worker.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
const { isMainThread, parentPort, workerData } = require("worker_threads");
options = workerData;
// Create discord.js client
const Discord = require("discord.js");
const client = new Discord.Client();
let ready = false;
client.on("ready", () => {
ready = true;
console.log(`Music client ready`);
});
client.on("error", (error) => {
console.log(error);
});
client.login(options.TOKEN); // discord.js connect to discord bot
const musicCmdPath = "main.js";
let { cmdFunc } = require("./" + musicCmdPath); // Gets function of music commands
// On worker message (music command passthrough)
parentPort.on("message", async (data) => {
if (!isMainThread && ready) {
let { msgId, channelId, args, cmd } = data;
try {
let channel = await client.channels.fetch(channelId); // Get channel from ID
let msg = await channel.messages.fetch(msgId); // Get message from ID
setImmediate(async () => {
cmdFunc(msg, args, cmd.action); // Runs command function
});
} catch (e) {
console.log(e);
}
}
});
// Catch process errors
process.on("uncaughtException", function (err) {
console.log(err);
});