guild adding to db

master
Cristiano Pires 2 years ago
parent 429696d18a
commit 657c52945f

@ -41,7 +41,7 @@ const {Command, } = require('../../lib.js');
sendMessage(true) sendMessage(true)
} }
}) })
}).catch })
async function sendMessage(success,error) async function sendMessage(success,error)
{ {

@ -5,8 +5,8 @@ let guild =
new Schema( new Schema(
{ {
name: {type: String, required: false, max: 100}, name: {type: String, required: false, max: 100},
memberCount: {type:Boolean, required: true}, memberCount: {type:Number, required: false},
gID:{type:String, required:true}, gID:{type:String, required:true, unique:true},
strikes:{type:Boolean}, strikes:{type:Boolean},
music:{type:Boolean} music:{type:Boolean}
} }

@ -8,7 +8,7 @@ const mongoose = require('mongoose');
const port = process.env.PORT || 5000; const port = process.env.PORT || 5000;
const {Client, GatewayIntentBits,Partials, ActivityType} = require('./lib.js'); const {Client, GatewayIntentBits,Partials, ActivityType} = require('./lib.js');
const Spawner = require('child_process'); const Spawner = require('child_process');
const guildsM = require('./models/guilds') const GuildM = require('./models/guilds')
app.use(express.static(path.join(__dirname, "public"))); app.use(express.static(path.join(__dirname, "public")));
app.set("view engine", "ejs"); app.set("view engine", "ejs");
@ -66,13 +66,28 @@ bot.on('ready', () =>
status: 'online', status: 'online',
}); });
console.timeEnd('StartUp'); console.timeEnd('StartUp');
bot.guilds.cache.forEach(guild=>
/* spawining a singe music worker for legendary {
GuildM.find({gID:guild.id})
Spawner.fork('./musicWorker.js',['334456736633323520', 'Legendary']) .then(
(guild_,err)=>
*/ {
if(err || guild_.length==0)
{
var nGuild = GuildM();
nGuild.name = guild.name;
nGuild.memberCount = guild.members.cache.size;
nGuild.gID = guild.id;
nGuild.strike = false;
nGuild.music = false;
nGuild.save(err=>
{
if(err) console.log('Server: Adding non existing guilds to the DB. Error ocurred:', err)
});
}
}
)
})
music(); music();
setInterval(() => setInterval(() =>
{ {
@ -84,12 +99,24 @@ bot.on('ready', () =>
}); });
bot.on('guildCreate', guild=> bot.on('guildCreate', guild=>
{ {
console.log('Added To guild', guild); var guildModel = new GuildM();
var guild = new GuildM() guildModel.name = guild.name;
guildModel.memberCount = guild.members.cache.size;
guildModel.gID = guild.id;
guildModel.strikes = false;
guildModel.music = false;
guildModel.save()
}); });
bot.on('guildDelete', guild => bot.on('guildDelete', guild =>
{ {
GuildM.find({gID:guild.id}).then(guild=>
{
if(guild.length==0) return sendMessage(false, 'ID not on the list.')
GuildM.findOneAndRemove(guild.id, (err)=>
{
if(err) console.log('Server: Deleting guild from DB: There was an error:', err)
})
})
}); });
async function music() async function music()
{ {
@ -141,5 +168,5 @@ function moveAFKs(){
async function serversWithMusicOn() async function serversWithMusicOn()
{ {
return await guildsM.find({music:true}).then(g=>{return g}) return await GuildM.find({music:true}).then(g=>{return g})
} }
Loading…
Cancel
Save