From e19ecfd840f965edd8b7f06b90a4587247a8dc03 Mon Sep 17 00:00:00 2001 From: Cristiano Pires Date: Wed, 6 Dec 2023 22:31:15 +0000 Subject: [PATCH] Pushed global functionality into lib. --- lib.js | 51 ++++++++++++++++++++++++++++++++++++++++++---- server.js | 60 +++---------------------------------------------------- 2 files changed, 50 insertions(+), 61 deletions(-) diff --git a/lib.js b/lib.js index 7bded7d..f2a88c1 100644 --- a/lib.js +++ b/lib.js @@ -5,6 +5,8 @@ const roleRulesM = require('./models/autoRoleRule'); const feedM = require('./models/feeds'); const xmlparser = require('xml-js') const GuildM = require('./models/guilds'); +const Spawner = require('child_process'); +const mongoose = require('mongoose'); module.exports.GatewayIntentBits = GatewayIntentBits; module.exports.Partials = Partials; @@ -52,8 +54,6 @@ class command } }module.exports.Command = command; -// const {func}=require('lib.d.ts'); -// func('string'); class _Client extends Client { @@ -62,9 +62,9 @@ class _Client extends Client * @param {ClientOptions} options */ commands = new Map(); - constructor(options) + constructor(options, mongoDBURI) { - super(options) + super(options); let finder = find(path.resolve('commands')); finder.on('file', file=> { @@ -93,6 +93,17 @@ class _Client extends Client { this.enableCommands(); }) + connectToDB(); + function connectToDB() + { + try { + mongoose.connect(mongoDBURI); + } catch (err) { + console.log('Server: Error: There was an Error with the connection to the database, attempting to restart it.', err) + connectToDB(); + } + } + mongoose.Promise = global.Promise; this.setGuilds(); this.RoleSetter(); this.rustCommits = new rustCommits(this); @@ -109,8 +120,10 @@ class _Client extends Client console.log('Client is reconnecting...'); }); this.YTFeed(); + this.music(); setInterval(() => { this.YTFeed() + this.music(); }, 60*60*1000); } setGuilds() @@ -355,6 +368,36 @@ class _Client extends Client return; }); } + async music() + { + const ServersWithMusicOn = await this.serversWithMusicOn(); + const ISGONNABEOFFFORAWHILE = true + if(!ServersWithMusicOn && !ISGONNABEOFFFORAWHILE) return + for(var server of ServersWithMusicOn) + { + if(!Childs[server.name]) + { + console.log('Index: Starting Music Worker:', server.name,`(${server.gID})`) + Childs[server.name] = Spawner.fork('./musicWorker.js',[server.gID, server.name]); + } + else + { //Health check + (function (server) + { + Childs[server.name].on('exit', ()=> + { + console.log('Child DIED, spawning another.') + Childs[server.name] = Spawner.fork('./musicWorker.js',[server.gID, server.name]); + }) + })(server); + } + + } + } + async serversWithMusicOn() + { + return await GuildM.find({music:true}).then(g=>{return g}) + } }module.exports.Client = _Client; const ErrorType = diff --git a/server.js b/server.js index c62b493..ad0c170 100644 --- a/server.js +++ b/server.js @@ -4,11 +4,9 @@ var app = express(); const path = require('path'); const http = require('http'); const {Server} = require('socket.io'); -const mongoose = require('mongoose'); const port = process.env.PORT || 5000; const {Client, GatewayIntentBits,Partials, ActivityType} = require('./lib.js'); -const Spawner = require('child_process'); -const GuildM = require('./models/guilds') + app.use(express.static(path.join(__dirname, "public"))); app.set("view engine", "ejs"); @@ -21,9 +19,6 @@ const mongoDB = process.env.mongoDB; const server = http.createServer(app); const io = new Server(server); -var Childs = []; - - const bot = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, @@ -48,7 +43,7 @@ const bot = new Client({ intents: [ Partials.Reaction, Partials.Message ] -}); +}, mongoDB); var retryCounter = 0; function login() { @@ -72,21 +67,10 @@ function startLimitProtectionTimer() login(); }, 24*60*60*1000); } -function connectToDB() -{ - try { - mongoose.connect(mongoDB); - } catch (error) { - console.log('Server: Error: There was an Error with the connection to the database, attempting to restart it.', err) - connectToDB(); - } -} bot.on('ready', () => { - mongoose.Promise = global.Promise; - connectToDB(); console.log(`--------------------------\n ${bot.user.tag.split('#')[0]} \n Ready \n on `+bot.guilds.cache.size+" guilds \n serving "+bot.users.cache.size+" users \n--------------------------") bot.user.setPresence({ activities: [{ name: `!help`, type: ActivityType.Listening}], @@ -99,45 +83,12 @@ bot.on('ready', () => }); console.timeEnd('StartUp'); - - music(); - setInterval(() => - { - music(); - }, 60000); + setInterval(() => { moveAFKs(); }, 5*60*1000); }); -async function music() -{ - //console.log('Music: Looking for Servers.') - const ServersWithMusicOn = await serversWithMusicOn(bot.guilds.cache); - const ISGONNABEOFFFORAWHILE = true - if(!ServersWithMusicOn && !ISGONNABEOFFFORAWHILE) return - for(var server of ServersWithMusicOn) - { - if(!Childs[server.name]) - { - console.log('Index: Starting Music Worker:', server.name,`(${server.gID})`) - Childs[server.name] = Spawner.fork('./musicWorker.js',[server.gID, server.name]); - } - else - { //Health check - (function (server) - { - Childs[server.name].on('exit', ()=> - { - console.log('Child DIED, spawning another.') - Childs[server.name] = Spawner.fork('./musicWorker.js',[server.gID, server.name]); - }) - })(server); - } - - } -} - function moveAFKs(){ //Gets the members inside the AFK channel let auxmembers = bot.channels.cache.get("335494006890823680").members @@ -159,11 +110,6 @@ function moveAFKs(){ } } -async function serversWithMusicOn() -{ - return await GuildM.find({music:true}).then(g=>{return g}) -} - async function sendNotification(message) { fetch('https://ntfy.wordfights.com/RemChanv2',{