diff --git a/events/guildMemberAdd.js b/events/guildMemberAdd.js index 23c3b20..ce3a89c 100644 --- a/events/guildMemberAdd.js +++ b/events/guildMemberAdd.js @@ -1,8 +1,9 @@ const { EmbedBuilder } = require('discord.js'); +const { GetGuild } = require('../database/guild'); module.exports = async (client, member) => { - let GuildDB = await client.GetGuild(member.guild.id); + let GuildDB = await GetGuild(client, member.guild.id); const channel = client.GetChannel(GuildDB.welcomeChannel); let embed = new EmbedBuilder() diff --git a/events/interactionCreate.js b/events/interactionCreate.js index 2812fca..b5f7f1a 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -1,3 +1,5 @@ +const { GetGuild } = require('../database/guild'); + module.exports = async (client, interaction) => { if (interaction.isCommand()) return; /* @@ -5,7 +7,7 @@ module.exports = async (client, interaction) => { from any command */ - let GuildDB = await client.GetGuild(interaction.guildId); + let GuildDB = await GetGuild(client, interaction.guildId); const interactionName = interaction.customId.split("-")[0]; let interactionHandler = client.interactionHandlers.get(interactionName); diff --git a/index.js b/index.js index 4a702e8..3856eaa 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ const { HandleActivePlayersList } = require('./util/LogsHandler'); // Log all uncaught exceptions before killing process. process.on('uncaughtException', async (error) => { + console.trace(error); let d = new Date(); // Asynchronously write the error message to a log file using Promises await new Promise((resolve, reject) => { diff --git a/src/DayzRBot.js b/src/DayzRBot.js index 753d29d..15c8e8a 100644 --- a/src/DayzRBot.js +++ b/src/DayzRBot.js @@ -56,7 +56,7 @@ class DayzRBot extends Client { this.ws.on("INTERACTION_CREATE", async (interaction) => { const start = new Date().getTime(); if (interaction.type != 3) { - let GuildDB = await this.GetGuild(interaction.guild_id); + let GuildDB = await GetGuild(this, interaction.guild_id); for (const [factionID, data] of Object.entries(GuildDB.factionArmbands)) { const guild = this.guilds.cache.get(GuildDB.serverID); @@ -157,7 +157,11 @@ class DayzRBot extends Client { let logIndex = lines.indexOf(history.lastLog); if (this.playerSessions.size === 0) { - s.map(p => p.connected = false); // assume all players not connected on init only. + let players = await this.dbo.collection('players').find({"connected": true}).toArray(); + players.map(p => p.connected = false); // assume all players not connected on init only. + for (let i = 0; i < players.length; i++) { + UpdatePlayer(this, players[i]) + } } for (let i = logIndex + 1; i < lines.length; i++) { @@ -179,19 +183,31 @@ class DayzRBot extends Client { } // Handle alarm pings + const maxEmbed = 10; for (const [channel_id, data] of Object.entries(this.alarmPingQueue)) { const channel = this.GetChannel(channel_id); if (!channel) continue; for (const [role, embeds] of Object.entries(data)) { - if (role == '-no-role-ping-') channel.send({ embeds: embeds }); - else channel.send({ content: `<@&${role}>`, embeds: embeds }); + if (embeds.length > maxEmbed) { + let embedArrays = []; + while (embeds.length > maxEmbed) + embedArrays.push(embeds.splice(0, maxEmbed)); + + for (let i = 0; i < embedArrays.length; i++) { + if (role == '-no-role-ping-') channel.send({ embeds: embedArrays[i] }); + else channel.send({ content: `<@&${role}>`, embeds: embedArrays[i] }); + } + } else { + if (role == '-no-role-ping-') channel.send({ embeds: embeds }); + else channel.send({ content: `<@&${role}>`, embeds: embeds }); + } } } this.alarmPingQueue = {}; const playerTemplate = /(.*) \| Player \"(.*)\" \(id=(.*) pos=<(.*)>\)/g; - let previouslyConnected = s.filter(p => p.connected); // All players with connection log captured above and no disconnect log + let previouslyConnected = await this.dbo.collection('players').find({"connected": true}).toArray(); // All players with connection log captured above and no disconnect log let lastDetectedTime; for (let i = lines.length - 1; i > 0; i--) { @@ -214,7 +230,7 @@ class DayzRBot extends Client { lastDetectedTime = await this.getDateEST(info.time); let playerStat = await this.dbo.collection("players").findOne({"playerID": info.playerID}); - if (!this.exits(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID, this.config.Nitrado.ServerID); + if (!this.exists(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID, this.config.Nitrado.ServerID); if (!previouslyConnected.includes(playerStat) && this.exists(playerStat.lastDisconnectionDate) && playerStat.lastDisconnectionDate !== null && playerStat.lastDisconnectionDate.getTime() > lastDetectedTime.getTime()) continue; // Skip this player if the lastDisconnectionDate time is later than the player log entry. @@ -262,7 +278,7 @@ class DayzRBot extends Client { const filename = settings.game_specific.log_files.sort((a, b) => a.length - b.length)[0]; const path = `${settings.game_specific.path.slice(0, -1)}${filename.split(settings.game)[1]}`; - let guild = await c.GetGuild(c, c.config.GuildID); + let guild = await GetGuild(c, c.config.GuildID); await DownloadNitradoFile(c, path, './logs/server-logs.ADM').then(async (status) => { if (status == 1) return c.error('...Failed to Download logs...'); @@ -416,8 +432,6 @@ class DayzRBot extends Client { this.guilds.cache.forEach((guild) => RegisterGuildCommands(this, guild.id)); } - async GetGuild(GuildId) { return await GetGuild(this, GuildId) } - build() { this.login(this.config.Token); } diff --git a/util/AlarmsHandler.js b/util/AlarmsHandler.js index 7bf1ea5..c041b68 100644 --- a/util/AlarmsHandler.js +++ b/util/AlarmsHandler.js @@ -2,6 +2,7 @@ const { BanPlayer, UnbanPlayer } = require('./NitradoAPI'); const { EmbedBuilder } = require('discord.js'); const { calculateVector } = require('./vector'); const { destinations } = require('../database/destinations'); +const { GetGuild } = require('../database/guild'); // Private functions (only called locally) @@ -159,7 +160,7 @@ module.exports = { } if (update) { - client.dbo.collection("guilds").updateOne({ "server.serverID": guildId }, {$set: { "server.uavs": uavs }}, (err, res) => { + client.dbo.collection("guilds").updateOne({ "server.serverID": guild.serverID }, {$set: { "server.uavs": uavs }}, (err, res) => { if (err) return client.sendError(client.GetChannel(guild.adminLogsChannel), err); }); } @@ -167,7 +168,7 @@ module.exports = { KillInAlarm: async (client, guildId, data) => { - let guild = await client.GetGuild(guildId); + let guild = await GetGuild(client, guildId); for (let i = 0; i < guild.alarms.length; i++) { let alarm = guild.alarms[i]; diff --git a/util/KillfeedHandler.js b/util/KillfeedHandler.js index bc2bc5d..fb492ad 100644 --- a/util/KillfeedHandler.js +++ b/util/KillfeedHandler.js @@ -47,7 +47,7 @@ module.exports = { let diedTemplate = /(.*) \| Player \"(.*)\" \(DEAD\) \(id=(.*) pos=<(.*)>\) died\. Stats> Water: (.*) Energy: (.*) Bleed sources: (.*)/g; let data = line.includes('>) died.') ? [...line.matchAll(diedTemplate)][0] : [...line.matchAll(killedByZmb)][0]; - if (!data) return stats; + if (!data) return; let info = { time: data[1], @@ -59,7 +59,7 @@ module.exports = { const newDt = await client.getDateEST(info.time); let victimStat = await client.dbo.collection("players").findOne({"playerID": info.playerID}); - if (!client.exits(victimStat)) playerStat = getDefaultPlayer(info.player, info.playerID, client3.config.Nitrado.ServerID); + if (!client.exists(victimStat)) victimStat = getDefaultPlayer(info.player, info.playerID, client.config.Nitrado.ServerID); victimStat.lastDeathDate = newDt; @@ -143,7 +143,7 @@ module.exports = { if (killedBy == Templates.LandMine || killedBy == Templates.Explosion || killedBy == Templates.Vehicle) { let victimStat = await client.dbo.collection("players").findOne({"playerID": info.victimID}); - if (!client.exits(victimStat)) victimStat = getDefaultPlayer(info.victim, info.victimID, client.config.Nitrado.ServerID); + if (!client.exists(victimStat)) victimStat = getDefaultPlayer(info.victim, info.victimID, client.config.Nitrado.ServerID); victimStat.lastDeathDate = newDt; const cod = killedBy == Templates.LandMine ? `Land Mine Trap` : @@ -158,15 +158,15 @@ module.exports = { if (client.exists(channel)) await channel.send({ embeds: [killEvent] }); return await UpdatePlayer(client, victimStat); } - killerStat - KillInAlarm(client, guildId, info); // check if kill happened in a no kill zone + + KillInAlarm(client, guild.serverID, info); // check if kill happened in a no kill zone if (!client.exists(info.victim) || !client.exists(info.victimID) || !client.exists(info.killer) || !client.exists(info.killerID)) return stats; let victimStat = await client.dbo.collection("players").findOne({"playerID": info.victimID}); let killerStat = await client.dbo.collection("players").findOne({"playerID": info.killerID}); - if (!client.exits(victimStat)) victimStat = getDefaultPlayer(info.victim, info.victimID, client.config.Nitrado.ServerID); - if (!client.exits(killerStat)) killerStat = getDefaultPlayer(info.killer, info.killerID, client.config.Nitrado.ServerID); + if (!client.exists(victimStat)) victimStat = getDefaultPlayer(info.victim, info.victimID, client.config.Nitrado.ServerID); + if (!client.exists(killerStat)) killerStat = getDefaultPlayer(info.killer, info.killerID, client.config.Nitrado.ServerID); killerStat.kills++; killerStat.killStreak++; @@ -191,21 +191,21 @@ module.exports = { let banking = await client.dbo.collection("users").findOne({"user.userID": killerStat.discordID}).then(banking => banking); if (!banking) { - banking = await createUser(interaction.member.user.id, guildId, guild.startingBalance, client) + banking = await createUser(interaction.member.user.id, guild.serverID, guild.startingBalance, client) if (!client.exists(banking)) return client.sendInternalError(interaction, err); } banking = banking.user; - if (!client.exists(banking.guilds[guildId])) { - const success = addUser(banking.guilds, guildId, interaction.member.user.id, client, guild.startingBalance); + if (!client.exists(banking.guilds[ guild.serverID])) { + const success = addUser(banking.guilds, guild.serverID, interaction.member.user.id, client, guild.startingBalance); if (!success) return client.sendInternalError(interaction, 'Failed to add bank'); } - const newBalance = banking.guilds[guildId].balance + totalBounty; + const newBalance = banking.guilds[ guild.serverID].balance + totalBounty; await client.dbo.collection("users").updateOne({ "user.userID": killerStat.discordID }, { $set: { - [`user.guilds.${guildId}.balance`]: newBalance, + [`user.guilds.${ guild.serverID}.balance`]: newBalance, } }, (err, res) => { if (err) return client.sendError(client.GetChannel(guild.killfeedChannel), `Killfeed Error: Updating killer bank balance\n${err}`); diff --git a/util/LogsHandler.js b/util/LogsHandler.js index ac2acb6..ac5c7c9 100644 --- a/util/LogsHandler.js +++ b/util/LogsHandler.js @@ -30,7 +30,7 @@ module.exports = { if (!client.exists(info.player) || !client.exists(info.playerID)) return; let playerStat = await client.dbo.collection("players").findOne({"playerID": info.playerID}); - if (!client.exits(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID, client.config.Nitrado.ServerID); + if (!client.exists(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID, client.config.Nitrado.ServerID); const newDt = await client.getDateEST(info.time); playerStat.lastConnectionDate = newDt; @@ -74,7 +74,7 @@ module.exports = { if (!client.exists(info.player) || !client.exists(info.playerID)) return; let playerStat = await client.dbo.collection("players").findOne({"playerID": info.playerID}); - if (!client.exits(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID, client.config.Nitrado.ServerID); + if (!client.exists(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID, client.config.Nitrado.ServerID); const newDt = await client.getDateEST(info.time); const unixTime = Math.round(newDt.getTime() / 1000); // Seconds @@ -124,7 +124,7 @@ module.exports = { if (!client.exists(info.player) || !client.exists(info.playerID)) return; let playerStat = await client.dbo.collection("players").findOne({"playerID": info.playerID}); - if (!client.exits(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID, client.config.Nitrado.ServerID); + if (!client.exists(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID, client.config.Nitrado.ServerID); if (!client.exists(playerStat.lastConnectionDate)) playerStat.lastConnectionDate = await client.getDateEST(info.time); playerStat.lastPos = playerStat.pos; @@ -161,7 +161,7 @@ module.exports = { if (!client.exists(info.player) || !client.exists(info.playerID)) return; let playerStat = await client.dbo.collection("players").findOne({"playerID": info.playerID}); - if (!client.exits(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID, client.config.Nitrado.ServerID); + if (!client.exists(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID, client.config.Nitrado.ServerID); playerStat.lastDamageDate = await client.getDateEST(info.time); playerStat.lastHitBy = info.attacker; @@ -203,7 +203,7 @@ module.exports = { if (!client.exists(guild.activePlayersChannel)) return; const channel = client.GetChannel(guild.activePlayersChannel); - let activePlayers = client.dbo.collection("players").find({"connected": true}) + let activePlayers = await client.dbo.collection("players").find({"connected": true}).toArray(); let des = ``; for (let i = 0; i < activePlayers.length; i++) {