IT FUCKING WORKS
This commit is contained in:
parent
b1bcb1e24c
commit
661fd36905
52
index.js
52
index.js
|
@ -3,8 +3,6 @@ const fs = require("fs");
|
||||||
const config = require("./config.json");
|
const config = require("./config.json");
|
||||||
const funcs = require("./funcs.js");
|
const funcs = require("./funcs.js");
|
||||||
const wfos = require("./data/wfos.json");
|
const wfos = require("./data/wfos.json");
|
||||||
const blacklist = require("./data/blacklist.json");
|
|
||||||
const events = require("./data/events.json");
|
|
||||||
const outlookURLs = require("./data/outlook.json");
|
const outlookURLs = require("./data/outlook.json");
|
||||||
const sattelites = require("./data/sattelites.json");
|
const sattelites = require("./data/sattelites.json");
|
||||||
const nwrstreams = require("./data/nwrstreams.json")
|
const nwrstreams = require("./data/nwrstreams.json")
|
||||||
|
@ -13,21 +11,22 @@ const { client, xml } = require("@xmpp/client");
|
||||||
const fetch = require("node-fetch");
|
const fetch = require("node-fetch");
|
||||||
const html = require("html-entities")
|
const html = require("html-entities")
|
||||||
const Discord = require("discord.js");
|
const Discord = require("discord.js");
|
||||||
|
const { ClusterClient, getInfo } = require('discord-hybrid-sharding');
|
||||||
const dVC = require("@discordjs/voice");
|
const dVC = require("@discordjs/voice");
|
||||||
const colors = require("colors");
|
const colors = require("colors");
|
||||||
const sqlite3 = require("sqlite3").verbose();
|
const sqlite3 = require("sqlite3").verbose();
|
||||||
const ws = require("ws");
|
|
||||||
// setup ws client to 127.0.0.1:9743
|
|
||||||
const wsClient = new ws("ws://127.0.0.1:9743");
|
|
||||||
// Setup Discord
|
// Setup Discord
|
||||||
const discord = new Discord.Client({
|
const discord = new Discord.Client({
|
||||||
intents: [
|
intents: [
|
||||||
"Guilds",
|
"Guilds",
|
||||||
"GuildVoiceStates",
|
"GuildVoiceStates",
|
||||||
"DirectMessages"
|
"DirectMessages"
|
||||||
]
|
],
|
||||||
|
shards: getInfo().SHARD_LIST, // An array of shards that will get spawned
|
||||||
|
shardCount: getInfo().TOTAL_SHARDS, // Total number of shards
|
||||||
});
|
});
|
||||||
|
|
||||||
|
discord.cluster = new ClusterClient(client); // initialize the Client, so we access the .broadcastEval()
|
||||||
|
|
||||||
// Setup SQlite DB
|
// Setup SQlite DB
|
||||||
const db = new sqlite3.Database("channels.db", (err) => {
|
const db = new sqlite3.Database("channels.db", (err) => {
|
||||||
|
@ -252,9 +251,24 @@ var messages = 0;
|
||||||
|
|
||||||
// START DISCORD
|
// START DISCORD
|
||||||
|
|
||||||
// listen on ws
|
discord.cluster.on('message', async (message) => {
|
||||||
wsClient.on("message", (data) => {
|
if (!message.type) return;
|
||||||
|
switch (message.type) {
|
||||||
|
case "updateRooms":
|
||||||
|
// Update the rooms
|
||||||
|
iem = message.rooms;
|
||||||
|
break;
|
||||||
|
case "sendMsgChannel":
|
||||||
|
console.log(`Trying to send message to ${message.channel}`);
|
||||||
|
channel = discord.channels.cache.get(message.channel);
|
||||||
|
if (!channel) return;
|
||||||
|
channel.send(message.msg).then(msg => {
|
||||||
|
if (msg.channel.type === Discord.ChannelType.GuildAnnouncement) msg.crosspost();
|
||||||
|
});
|
||||||
|
console.log(`Sent message to ${message.channel}`);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
discord.on('ready', async () => {
|
discord.on('ready', async () => {
|
||||||
|
@ -266,14 +280,14 @@ discord.on('ready', async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
//start();
|
// //start();
|
||||||
setTimeout(() => {
|
// setTimeout(() => {
|
||||||
// Wait 10 seconds, if startup is still true, something went wrong
|
// // Wait 10 seconds, if startup is still true, something went wrong
|
||||||
if (startup) {
|
// if (startup) {
|
||||||
console.log(`${colors.red("[ERROR]")} Startup failed, exiting...`);
|
// console.log(`${colors.red("[ERROR]")} Startup failed, exiting...`);
|
||||||
process.exit(1);
|
// process.exit(1);
|
||||||
}
|
// }
|
||||||
}, 10000)
|
// }, 10000)
|
||||||
|
|
||||||
// Check all channels in DB, fetch them, if they dont exist, delete all subscriptions
|
// Check all channels in DB, fetch them, if they dont exist, delete all subscriptions
|
||||||
db.all(`SELECT channelid FROM channels`, (err, rows) => {
|
db.all(`SELECT channelid FROM channels`, (err, rows) => {
|
||||||
|
@ -949,20 +963,20 @@ discord.on("interactionCreate", async (interaction) => {
|
||||||
// })
|
// })
|
||||||
|
|
||||||
process.on("unhandledRejection", (error, promise) => {
|
process.on("unhandledRejection", (error, promise) => {
|
||||||
console.log(`${colors.red("[ERROR]")} Unhandled Rejection @ ${promise}: ${error}`);
|
console.log(`${colors.red("[ERROR]")} Unhandled Rejection @ ${promise}: ${error.stack}`);
|
||||||
// create errors folder if it doesnt exist
|
// create errors folder if it doesnt exist
|
||||||
if (!fs.existsSync("./error")) {
|
if (!fs.existsSync("./error")) {
|
||||||
fs.mkdirSync("./error");
|
fs.mkdirSync("./error");
|
||||||
}
|
}
|
||||||
// write ./error/rejection_timestamp.txt
|
// write ./error/rejection_timestamp.txt
|
||||||
fs.writeFileSync(`./error/rejection_${Date.now()}.txt`, `ERROR:\n${error}\n\nPROMISE:\n${JSON.stringify(promise)}`);
|
fs.writeFileSync(`./error/rejection_${Date.now()}.txt`, `ERROR:\n${error.stack}\n\nPROMISE:\n${JSON.stringify(promise)}`);
|
||||||
// Send discord log
|
// Send discord log
|
||||||
const myGuild = discord.guilds.cache.get(config.discord.mainGuild);
|
const myGuild = discord.guilds.cache.get(config.discord.mainGuild);
|
||||||
const channel = myGuild.channels.cache.get(config.discord.logChannel);
|
const channel = myGuild.channels.cache.get(config.discord.logChannel);
|
||||||
channel.send({
|
channel.send({
|
||||||
embeds: [
|
embeds: [
|
||||||
{
|
{
|
||||||
description: `Unhandled Rejection\n\`\`\`${error}\n${JSON.stringify(promise)}\`\`\``,
|
description: `Unhandled Rejection\n\`\`\`${error.stack}\n${JSON.stringify(promise)}\`\`\``,
|
||||||
color: 0xff0000
|
color: 0xff0000
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
"@xmpp/client": "^0.13.1",
|
"@xmpp/client": "^0.13.1",
|
||||||
"@xmpp/debug": "^0.13.0",
|
"@xmpp/debug": "^0.13.0",
|
||||||
"colors": "^1.4.0",
|
"colors": "^1.4.0",
|
||||||
|
"discord-hybrid-sharding": "^2.2.0",
|
||||||
"discord.js": "14.14.1",
|
"discord.js": "14.14.1",
|
||||||
"geolib": "^3.3.4",
|
"geolib": "^3.3.4",
|
||||||
"html-entities": "^2.5.2",
|
"html-entities": "^2.5.2",
|
||||||
|
|
52
shard.js
52
shard.js
|
@ -7,7 +7,6 @@ const rest = new REST({
|
||||||
version: '10'
|
version: '10'
|
||||||
}).setToken(config.discord.token);
|
}).setToken(config.discord.token);
|
||||||
const sqlite3 = require("sqlite3").verbose();
|
const sqlite3 = require("sqlite3").verbose();
|
||||||
const ws = require("ws");
|
|
||||||
|
|
||||||
// Setup SQlite DB
|
// Setup SQlite DB
|
||||||
const db = new sqlite3.Database("channels.db", (err) => {
|
const db = new sqlite3.Database("channels.db", (err) => {
|
||||||
|
@ -20,8 +19,6 @@ const db = new sqlite3.Database("channels.db", (err) => {
|
||||||
db.run(`CREATE TABLE IF NOT EXISTS userAlerts (userid TEXT, iemchannel TEXT, filter TEXT, filterEvt TEXT, minPriority INT, custommessage TEXT);`);
|
db.run(`CREATE TABLE IF NOT EXISTS userAlerts (userid TEXT, iemchannel TEXT, filter TEXT, filterEvt TEXT, minPriority INT, custommessage TEXT);`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// setup ws server here for the shards to connect to
|
|
||||||
const wss = new ws.Server({ port: 9743 });
|
|
||||||
|
|
||||||
// Requires
|
// Requires
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
@ -380,16 +377,13 @@ xmpp.on("stanza", (stanza) => {
|
||||||
if (!filters.some((filter) => body.toLowerCase().includes(filter)) && !filters.some((filter) => text.toLowerCase().includes(filter))) return;
|
if (!filters.some((filter) => body.toLowerCase().includes(filter)) && !filters.some((filter) => text.toLowerCase().includes(filter))) return;
|
||||||
thisMsg = JSON.parse(JSON.stringify(discordMsg));
|
thisMsg = JSON.parse(JSON.stringify(discordMsg));
|
||||||
thisMsg.content = row.custommessage || null;
|
thisMsg.content = row.custommessage || null;
|
||||||
// send data to all websocket clients
|
// set channelid var in shard
|
||||||
wss.clients.forEach((client) => {
|
channelid = row.channelid;
|
||||||
client.send(JSON.stringify({
|
manager.broadcast({
|
||||||
type: "DISCORD_MESSAGE_CREATE",
|
type: "sendMsgChannel",
|
||||||
data: {
|
channel: channelid,
|
||||||
channelid: row.channelid,
|
msg: thisMsg
|
||||||
message: thisMsg
|
})
|
||||||
}
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -430,12 +424,10 @@ xmpp.on("stanza", (stanza) => {
|
||||||
if (!filters.some((filter) => body.toLowerCase().includes(filter)) && !filters.some((filter) => text.toLowerCase().includes(filter))) return;
|
if (!filters.some((filter) => body.toLowerCase().includes(filter)) && !filters.some((filter) => text.toLowerCase().includes(filter))) return;
|
||||||
thisMsg = JSON.parse(JSON.stringify(discordMsg));
|
thisMsg = JSON.parse(JSON.stringify(discordMsg));
|
||||||
thisMsg.content = row.custommessage || null;
|
thisMsg.content = row.custommessage || null;
|
||||||
manager.broadcast({
|
manager.broadcastEval(client => {
|
||||||
type: "SHARD_DM_MESSAGE_CREATE",
|
client.users.fetch(row.userid.toString())?.then((user) => {
|
||||||
data: {
|
user.send(thisMsg)
|
||||||
userid: row.userid,
|
})
|
||||||
message: thisMsg
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
|
@ -529,13 +521,13 @@ const start = () => {
|
||||||
// END XMPP
|
// END XMPP
|
||||||
|
|
||||||
process.on("unhandledRejection", (error, promise) => {
|
process.on("unhandledRejection", (error, promise) => {
|
||||||
console.log(`${colors.red("[ERROR]")} Unhandled Rejection @ ${promise}: ${error}`);
|
console.log(`${colors.red("[ERROR]")} Unhandled Rejection @ ${JSON.stringify(promise)}: ${error.stack}`);
|
||||||
// create errors folder if it doesnt exist
|
// create errors folder if it doesnt exist
|
||||||
if (!fs.existsSync("./error")) {
|
if (!fs.existsSync("./error")) {
|
||||||
fs.mkdirSync("./error");
|
fs.mkdirSync("./error");
|
||||||
}
|
}
|
||||||
// write ./error/rejection_timestamp.txt
|
// write ./error/rejection_timestamp.txt
|
||||||
fs.writeFileSync(`./error/rejection_${Date.now()}.txt`, `ERROR:\n${error}\n\nPROMISE:\n${JSON.stringify(promise)}`);
|
fs.writeFileSync(`./error/rejection_${Date.now()}.txt`, `ERROR:\n${error.stack}\n\nPROMISE:\n${JSON.stringify(promise)}`);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
});
|
});
|
||||||
|
@ -552,11 +544,23 @@ process.on("uncaughtException", (error) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const { ShardingManager } = require('discord.js');
|
const { ShardingManager } = require('discord.js');
|
||||||
|
const { ClusterManager } = require('discord-hybrid-sharding');
|
||||||
const manager = new ShardingManager('./index.js', { token: config.discord.token });
|
const manager = new ClusterManager(`${__dirname}/index.js`, {
|
||||||
|
totalShards: 'auto', // or numeric shard count
|
||||||
|
/// Check below for more options
|
||||||
|
shardsPerClusters: 2, // 2 shards per process
|
||||||
|
totalClusters: 4,
|
||||||
|
mode: 'process', // you can also choose "worker"
|
||||||
|
token: config.discord.token,
|
||||||
|
});
|
||||||
|
|
||||||
manager.on('shardCreate', shard => console.log(`Launched shard ${shard.id}`));
|
manager.on('shardCreate', shard => console.log(`Launched shard ${shard.id}`));
|
||||||
|
|
||||||
manager.spawn().then(() => {
|
manager.spawn({timeout: -1}).then(() => {
|
||||||
start();
|
start();
|
||||||
|
console.log(`${colors.cyan("[INFO]")} Spawned all shards`);
|
||||||
|
setInterval(() => {
|
||||||
|
manager.broadcast({ type: "heartbeat" });
|
||||||
|
manager.broadcast({ type: "updateRooms", rooms: iem})
|
||||||
|
}, 1000)
|
||||||
});
|
});
|
Loading…
Reference in a new issue