refactor/remove mongoose + quality of life
This commit is contained in:
25 files changed
+195
-289
No files matched your search
+5
-18
@@ -2,8 +2,8 @@ const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, StringSelect
|
||||
const CommandOptions = require('../util/CommandOptionTypes').CommandOptionTypes;
|
||||
const bitfieldCalculator = require('discord-bitfield-calculator');
|
||||
const { BanPlayer, UnbanPlayer, RestartServer, CheckServerStatus, DisableBaseDamage } = require('../util/NitradoAPI');
|
||||
const { Armbands } = require('../config/armbandsdb.js');
|
||||
const { User, addUser } = require('../structures/user');
|
||||
const { Armbands } = require('../database/armbands.js');
|
||||
const { createUser, addUser } = require('../database/user')
|
||||
|
||||
module.exports = {
|
||||
name: "admin",
|
||||
@@ -404,23 +404,10 @@ module.exports = {
|
||||
let banking = await client.dbo.collection("users").findOne({"user.userID": targetUserID}).then(banking => banking);
|
||||
|
||||
if (!banking) {
|
||||
banking = {
|
||||
userID: targetUserID,
|
||||
guilds: {
|
||||
[GuildDB.serverID]: {
|
||||
balance: GuildDB.startingBalance,
|
||||
banking = await createUser(targetUserID, GuildDB.serverID, GuildDB.startingBalance, client)
|
||||
if (!client.exists(banking)) return client.sendInternalError(interaction, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register bank for user
|
||||
let newBank = new User();
|
||||
newBank.createUser(targetUserID, GuildDB.serverID, GuildDB.startingBalance);
|
||||
newBank.save().catch(err => {
|
||||
if (err) return client.sendInternalError(interaction, err);
|
||||
});
|
||||
|
||||
} else banking = banking.user;
|
||||
banking = banking.user;
|
||||
|
||||
if (!client.exists(banking.guilds[GuildDB.serverID])) {
|
||||
const success = addUser(banking.guilds, GuildDB.serverID, targetUserID, client, GuildDB.startingBalance);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const { StringSelectMenuBuilder, EmbedBuilder, ActionRowBuilder } = require('discord.js');
|
||||
const { Armbands } = require('../config/armbandsdb.js');
|
||||
const { Armbands } = require('../database/armbands.js');
|
||||
|
||||
module.exports = {
|
||||
name: "armbands",
|
||||
|
||||
+17
-50
@@ -1,6 +1,6 @@
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const CommandOptions = require('../util/CommandOptionTypes').CommandOptionTypes;
|
||||
const { User, addUser } = require('../structures/user');
|
||||
const { createUser, addUser } = require('../database/user');
|
||||
|
||||
module.exports = {
|
||||
name: "bank",
|
||||
@@ -66,23 +66,10 @@ module.exports = {
|
||||
let banking = await client.dbo.collection("users").findOne({"user.userID": interaction.member.user.id}).then(banking => banking);
|
||||
|
||||
if (!banking) {
|
||||
banking = {
|
||||
userID: interaction.member.user.id,
|
||||
guilds: {
|
||||
[GuildDB.serverID]: {
|
||||
balance: GuildDB.startingBalance,
|
||||
banking = await createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance, client)
|
||||
if (!client.exists(banking)) return client.sendInternalError(interaction, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register bank for user
|
||||
let newBank = new User();
|
||||
newBank.createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance);
|
||||
newBank.save().catch(err => {
|
||||
if (err) return client.sendInternalError(interaction, err);
|
||||
});
|
||||
|
||||
} else banking = banking.user;
|
||||
banking = banking.user;
|
||||
|
||||
if (!client.exists(banking.guilds[GuildDB.serverID])) {
|
||||
const success = addUser(banking.guilds, GuildDB.serverID, interaction.member.user.id, client, GuildDB.startingBalance);
|
||||
@@ -97,29 +84,16 @@ module.exports = {
|
||||
// Show target users balance
|
||||
|
||||
let targetUserID = args[0].options[0].value.replace('<@!', '').replace('>', '');
|
||||
let targetUserBanking = await client.dbo.collection("users").findOne({"user.userID": targetUserID}).then(banking => banking);
|
||||
let targetUserBanking = await client.dbo.collection("users").findOne({"user.userID": targetUserID}).then(targetUserBanking => targetUserBanking);
|
||||
|
||||
if (!targetUserBanking) {
|
||||
targetUserBanking = {
|
||||
userID: targetUserID,
|
||||
guilds: {
|
||||
[GuildDB.serverID]: {
|
||||
balance: GuildDB.startingBalance,
|
||||
targetUserBanking = await createUser(targetUserID, GuildDB.serverID, GuildDB.startingBalance, client)
|
||||
if (!client.exists(banking)) return client.sendInternalError(interaction, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register bank for user
|
||||
let newBank = new User();
|
||||
newBank.createUser(targetUserID, GuildDB.serverID, GuildDB.startingBalance, 0);
|
||||
newBank.save().catch(err => {
|
||||
if (err) return client.sendInternalError(interaction, err);
|
||||
});
|
||||
|
||||
} else targetUserBanking = targetUserBanking.user;
|
||||
targetUserBanking = targetUserBanking.user;
|
||||
|
||||
if (!client.exists(targetUserBanking.guilds[GuildDB.serverID])) {
|
||||
const success = addUser(targetUserBanking.guilds, GuildDB.serverID, targetUserID, client, GuildDB.startingBalance);
|
||||
const success = addUser(banking.guilds, GuildDB.serverID, targetUserID, client, GuildDB.startingBalance);
|
||||
if (!success) return client.sendInternalError(interaction, 'Failed to add bank');
|
||||
}
|
||||
|
||||
@@ -160,25 +134,18 @@ module.exports = {
|
||||
if (err) return client.sendInternalError(interaction, err);
|
||||
});
|
||||
|
||||
let targetUserBanking = await client.dbo.collection("users").findOne({"user.userID": targetUserID}).then(banking => banking);
|
||||
let targetUserBanking = await client.dbo.collection("users").findOne({"user.userID": targetUserID}).then(targetUserBanking => targetUserBanking);
|
||||
|
||||
if (!targetUserBanking) {
|
||||
targetUserBanking = {
|
||||
userID: targetUserID,
|
||||
guilds: {
|
||||
[GuildDB.serverID]: {
|
||||
balance: (GuildDB.startingBalance + args[0].options[1].value),
|
||||
}
|
||||
}
|
||||
targetUserBanking = await createUser(targetUserID, GuildDB.serverID, GuildDB.startingBalance, client)
|
||||
if (!client.exists(banking)) return client.sendInternalError(interaction, err);
|
||||
}
|
||||
targetUserBanking = targetUserBanking.user;
|
||||
|
||||
// Register bank for user
|
||||
let newBank = new User();
|
||||
newBank.createUser(targetUserID, GuildDB.serverID, GuildDB.startingBalance, 0);
|
||||
newBank.save().catch(err => {
|
||||
if (err) return client.sendInternalError(interaction, err);
|
||||
});
|
||||
} else targetUserBanking = targetUserBanking.user;
|
||||
if (!client.exists(targetUserBanking.guilds[GuildDB.serverID])) {
|
||||
const success = addUser(banking.guilds, GuildDB.serverID, targetUserID, client, GuildDB.startingBalance);
|
||||
if (!success) return client.sendInternalError(interaction, 'Failed to add bank');
|
||||
}
|
||||
|
||||
const newTargetBalance = targetUserBanking.guilds[GuildDB.serverID].balance + args[0].options[1].value;
|
||||
|
||||
|
||||
+4
-16
@@ -1,5 +1,6 @@
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const CommandOptions = require('../util/CommandOptionTypes').CommandOptionTypes;
|
||||
const { createUser, addUser } = require('../database/user');
|
||||
|
||||
module.exports = {
|
||||
name: "bounty",
|
||||
@@ -62,23 +63,10 @@ module.exports = {
|
||||
banking = await client.dbo.collection("users").findOne({"user.userID": interaction.member.user.id}).then(banking => banking);
|
||||
|
||||
if (!banking) {
|
||||
banking = {
|
||||
userID: interaction.member.user.id,
|
||||
guilds: {
|
||||
[GuildDB.serverID]: {
|
||||
balance: GuildDB.startingBalance,
|
||||
banking = await createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance, client)
|
||||
if (!client.exists(banking)) return client.sendInternalError(interaction, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register bank for user
|
||||
let newBank = new User();
|
||||
newBank.createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance);
|
||||
newBank.save().catch(err => {
|
||||
if (err) return client.sendInternalError(interaction, err);
|
||||
});
|
||||
|
||||
} else banking = banking.user;
|
||||
banking = banking.user;
|
||||
|
||||
if (!client.exists(banking.guilds[GuildDB.serverID])) {
|
||||
const success = addUser(banking.guilds, GuildDB.serverID, interaction.member.user.id, client, GuildDB.startingBalance);
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
const { ActionRowBuilder, EmbedBuilder, ButtonBuilder, ButtonStyle, StringSelectMenuBuilder } = require('discord.js');
|
||||
const CommandOptions = require('../util/CommandOptionTypes').CommandOptionTypes;
|
||||
const { Armbands } = require('../config/armbandsdb.js');
|
||||
const { Armbands } = require('../database/armbands.js');
|
||||
|
||||
module.exports = {
|
||||
name: "claim",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const { EmbedBuilder, } = require('discord.js');
|
||||
const { User, addUser } = require('../structures/user');
|
||||
const { createUser, addUser } = require('../database/user');
|
||||
|
||||
module.exports = {
|
||||
name: "collect-income",
|
||||
@@ -41,25 +41,12 @@ module.exports = {
|
||||
|
||||
let banking = await client.dbo.collection("users").findOne({"user.userID": interaction.member.user.id}).then(banking => banking);
|
||||
|
||||
|
||||
if (!banking) {
|
||||
banking = {
|
||||
userID: interaction.member.user.id,
|
||||
guilds: {
|
||||
[GuildDB.serverID]: {
|
||||
balance: GuildDB.startingBalance,
|
||||
lastIncome: new Date('2000-01-01T00:00:00'),
|
||||
banking = await createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance, client)
|
||||
if (!client.exists(banking)) return client.sendInternalError(interaction, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register bank for user
|
||||
let newBank = new User();
|
||||
newBank.createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance);
|
||||
newBank.save().catch(err => {
|
||||
if (err) return client.sendInternalError(interaction, err);
|
||||
});
|
||||
|
||||
} else banking = banking.user;
|
||||
banking = banking.user;
|
||||
|
||||
if (!client.exists(banking.guilds[GuildDB.serverID])) {
|
||||
const success = addUser(banking.guilds, GuildDB.serverID, interaction.member.user.id, client, GuildDB.startingBalance);
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
|
||||
const CommandOptions = require('../util/CommandOptionTypes').CommandOptionTypes;
|
||||
const bitfieldCalculator = require('discord-bitfield-calculator');
|
||||
const { getDefaultSettings } = require('../database/guildSettings');
|
||||
|
||||
module.exports = {
|
||||
name: "config",
|
||||
@@ -832,7 +833,7 @@ module.exports = {
|
||||
let action = '';
|
||||
if (interaction.customId.split('-')[1]=='yes') {
|
||||
action = 'reset';
|
||||
const defaultGuildConfig = client.getDefaultSettings(GuildDB.serverID);
|
||||
const defaultGuildConfig = getDefaultSettings(GuildDB.serverID);
|
||||
client.dbo.collection("guilds").updateOne({"server.serverID":GuildDB.serverID}, {$set: {"server": defaultGuildConfig}}, (err, res) => {
|
||||
if (err) return client.sendInternalError(interaction, err);
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const CommandOptions = require('../util/CommandOptionTypes').CommandOptionTypes;
|
||||
const { Armbands } = require('../config/armbandsdb.js');
|
||||
const { Armbands } = require('../database/armbands.js');
|
||||
|
||||
module.exports = {
|
||||
name: "factions",
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const bitfieldCalculator = require('discord-bitfield-calculator');
|
||||
|
||||
module.exports = {
|
||||
name: "location",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const { EmbedBuilder, ActionRowBuilder, StringSelectMenuBuilder } = require('discord.js');
|
||||
const { User, addUser } = require('../structures/user');
|
||||
const { createUser, addUser } = require('../database/user');
|
||||
|
||||
module.exports = {
|
||||
name: "purchase-emp",
|
||||
@@ -24,24 +24,12 @@ module.exports = {
|
||||
|
||||
let banking = await client.dbo.collection("users").findOne({"user.userID": interaction.member.user.id}).then(banking => banking);
|
||||
|
||||
|
||||
if (!banking) {
|
||||
banking = {
|
||||
userID: interaction.member.user.id,
|
||||
guilds: {
|
||||
[GuildDB.serverID]: {
|
||||
balance: GuildDB.startingBalance,
|
||||
banking = await createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance, client)
|
||||
if (!client.exists(banking)) return client.sendInternalError(interaction, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register bank for user
|
||||
let newBank = new User();
|
||||
newBank.createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance);
|
||||
newBank.save().catch(err => {
|
||||
if (err) return client.sendInternalError(interaction, err);
|
||||
});
|
||||
|
||||
} else banking = banking.user;
|
||||
banking = banking.user;
|
||||
|
||||
if (!client.exists(banking.guilds[GuildDB.serverID])) {
|
||||
const success = addUser(banking.guilds, GuildDB.serverID, interaction.member.user.id, client, GuildDB.startingBalance);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const CommandOptions = require('../util/CommandOptionTypes').CommandOptionTypes;
|
||||
const { User, addUser } = require('../structures/user')
|
||||
const { createUser, addUser } = require('../database/user')
|
||||
|
||||
module.exports = {
|
||||
name: "purchase-uav",
|
||||
@@ -42,24 +42,12 @@ module.exports = {
|
||||
|
||||
let banking = await client.dbo.collection("users").findOne({"user.userID": interaction.member.user.id}).then(banking => banking);
|
||||
|
||||
|
||||
if (!banking) {
|
||||
banking = {
|
||||
userID: interaction.member.user.id,
|
||||
guilds: {
|
||||
[GuildDB.serverID]: {
|
||||
balance: GuildDB.startingBalance,
|
||||
banking = await createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance, client)
|
||||
if (!client.exists(banking)) return client.sendInternalError(interaction, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register bank for user
|
||||
let newBank = new User();
|
||||
newBank.createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance, 0);
|
||||
newBank.save().catch(err => {
|
||||
if (err) return client.sendInternalError(interaction, err);
|
||||
});
|
||||
|
||||
} else banking = banking.user;
|
||||
banking = banking.user;
|
||||
|
||||
if (!client.exists(banking.guilds[GuildDB.serverID])) {
|
||||
const success = addUser(banking.guilds, GuildDB.serverID, interaction.member.user.id, client, GuildDB.startingBalance);
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
|
||||
const CommandOptions = require('../util/CommandOptionTypes').CommandOptionTypes;
|
||||
const { addUser } = require('../structures/user');
|
||||
const { addUser } = require('../database/user');
|
||||
const bitfieldCalculator = require('discord-bitfield-calculator');
|
||||
|
||||
module.exports = {
|
||||
|
||||
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,39 @@
|
||||
module.exports = {
|
||||
getDefaultSettings(GuildId) {
|
||||
return {
|
||||
serverID: GuildId,
|
||||
autoRestart: 0,
|
||||
showKillfeedCoords: 0,
|
||||
allowedChannels: [],
|
||||
|
||||
killfeedChannel: "",
|
||||
connectionLogsChannel: "",
|
||||
activePlayersChannel: "",
|
||||
welcomeChannel: "",
|
||||
|
||||
factionArmbands: {},
|
||||
usedArmbands: [],
|
||||
excludedRoles: [],
|
||||
botAdminRoles: [],
|
||||
|
||||
playerstats: [], // to be removed
|
||||
|
||||
alarms: [],
|
||||
events: [],
|
||||
uavs: [],
|
||||
|
||||
incomeRoles: [],
|
||||
incomeLimiter: 168, // # of hours in 7 days
|
||||
|
||||
startingBalance: 500,
|
||||
uavPrice: 50000,
|
||||
empPrice: 500000,
|
||||
|
||||
linkedGamertagRole: "",
|
||||
memberRole: "",
|
||||
adminRole: "",
|
||||
|
||||
combatLogTimer: 5, // minutes
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
module.exports = {
|
||||
getDefaultPlayerStats(gt, pID) {
|
||||
return {
|
||||
gamertag: gt,
|
||||
playerID: pID,
|
||||
discordID: "",
|
||||
|
||||
KDR: 0.00,
|
||||
kills: 0,
|
||||
deaths: 0,
|
||||
killStreak: 0,
|
||||
bestKillStreak: 0,
|
||||
longestKill: 0,
|
||||
deathStreak: 0,
|
||||
worstDeathStreak: 0,
|
||||
|
||||
pos: [],
|
||||
lastPos: [],
|
||||
time: null,
|
||||
lastTime: null,
|
||||
|
||||
lastConnectionDate: null,
|
||||
lastDisconnectionDate: null,
|
||||
lastDamageDate: null,
|
||||
lastDeathDate: null,
|
||||
lastHitBy: null,
|
||||
connected: false,
|
||||
|
||||
totalSessionTime: 0,
|
||||
lastSessionTime: 0,
|
||||
longestSessionTime: 0,
|
||||
|
||||
bounties: [],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
module.exports = {
|
||||
createUser: async (userID, initialGuildID, startingBalance, client) => {
|
||||
let User = {
|
||||
user: {
|
||||
userID: userID,
|
||||
guilds: {}
|
||||
}
|
||||
};
|
||||
|
||||
User.user.guilds[initialGuildID] = {
|
||||
balance: startingBalance,
|
||||
lastIncome: new Date('2000-01-01T00:00:00'),
|
||||
};
|
||||
|
||||
await client.dbo.collection("users").insertOne(User, (err, res) => {
|
||||
if (err) {
|
||||
client.error(`Failed to create user - ${err}`);
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
|
||||
return User;
|
||||
},
|
||||
|
||||
/*
|
||||
This function is to add a new guild specific user to an already existing
|
||||
user document
|
||||
or
|
||||
can be used to reset a data back to default
|
||||
*/
|
||||
addUser: async (guilds, newGuildID, userID, client, startingBalance) => {
|
||||
let updatedGuilds = guilds;
|
||||
updatedGuilds[newGuildID] = {
|
||||
balance: startingBalance,
|
||||
lastIncome: new Date('2000-01-01T00:00:00')
|
||||
}
|
||||
|
||||
await client.dbo.collection("users").updateOne({"user.userID":userID}, {$set: {"user.guilds": updatedGuilds}}, (err, res) => {
|
||||
if (err) return false
|
||||
})
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
const DayzR = require('./structures/DayzRBot');
|
||||
const DayzR = require('./src/DayzRBot');
|
||||
const config = require('./config/config');
|
||||
const { GatewayIntentBits } = require('discord.js');
|
||||
|
||||
|
||||
+1
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "dayzr-bot",
|
||||
"version": "10.4.4",
|
||||
"version": "11.0.0",
|
||||
"description": "A General Purpose Discord Bot for DayZ Nitrado Servers.",
|
||||
"main": "index.js",
|
||||
"nodemonConfig": {
|
||||
@@ -25,7 +25,6 @@
|
||||
"dotenv": "^16.0.3",
|
||||
"form-data": "^4.0.0",
|
||||
"mongodb": "^4.12.1",
|
||||
"mongoose": "^7.0.1",
|
||||
"winston": "^3.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -3,7 +3,6 @@ const { Collection, Client, EmbedBuilder, Routes } = require('discord.js');
|
||||
const MongoClient = require('mongodb').MongoClient;
|
||||
const { REST } = require('@discordjs/rest');
|
||||
const Logger = require("../util/Logger");
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
// custom util imports
|
||||
const { DownloadNitradoFile, CheckServerStatus } = require('../util/NitradoAPI');
|
||||
@@ -11,6 +10,10 @@ const { HandlePlayerLogs, HandleActivePlayersList } = require('../util/LogsHandl
|
||||
const { HandleKillfeed, UpdateLastDeathDate } = require('../util/KillfeedHandler');
|
||||
const { HandleExpiredUAVs, HandleEvents } = require('../util/AlarmsHandler');
|
||||
|
||||
// Data structures imports
|
||||
const { getDefaultPlayerStats } = require('../database/playerStatistics');
|
||||
const { getDefaultSettings } = require('../database/guildSettings');
|
||||
|
||||
const path = require("path");
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
@@ -193,7 +196,7 @@ class DayzRBot extends Client {
|
||||
|
||||
lastDetectedTime = await this.getDateEST(info.time);
|
||||
let playerStat = s.find(stat => stat.playerID == info.playerID);
|
||||
if (playerStat == undefined) playerStat = this.getDefaultPlayerStats(info.player, info.playerID);
|
||||
if (playerStat == undefined) playerStat = getDefaultPlayerStats(info.player, info.playerID);
|
||||
|
||||
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.
|
||||
|
||||
@@ -283,19 +286,13 @@ class DayzRBot extends Client {
|
||||
// Connect to Mongo database.
|
||||
this.db = await MongoClient.connect(mongoURI, { connectTimeoutMS: 1000 });
|
||||
this.dbo = this.db.db(dbo);
|
||||
mongoose.connect(`mongodb://${mongoURI.split('@')[1]}/${dbo}`, {
|
||||
authSource: "admin",
|
||||
user: mongoURI.split('//')[1].split(':')[0],
|
||||
pass: mongoURI.split('//')[1].split(':')[1].split('@')[0],
|
||||
useNewUrlParser: true,
|
||||
}).catch(e => this.error(e));
|
||||
this.log('Successfully connected to mongoDB');
|
||||
databaselogs.connected = true;
|
||||
databaselogs.attempts = 0; // reset attempts
|
||||
this.databaseConnected = true;
|
||||
} catch (err) {
|
||||
databaselogs.attempts++;
|
||||
this.error(`Failed to connect to mongodb (mongodb://${mongoURI.split('@')[1]}/${dbo}): attempt ${databaselogs.attempts} - Error: ${err}`);
|
||||
this.error(`Failed to connect to mongodb (mongodb://${mongoURI.split('@')[1]}/${dbo}): attempt ${databaselogs.attempts} - ${err}`);
|
||||
failed = true;
|
||||
}
|
||||
|
||||
@@ -404,66 +401,6 @@ class DayzRBot extends Client {
|
||||
this.guilds.cache.forEach((guild) => RegisterGuildCommands(this, guild.id));
|
||||
}
|
||||
|
||||
getDefaultSettings(GuildId) {
|
||||
return {
|
||||
serverID: GuildId,
|
||||
autoRestart: 0,
|
||||
allowedChannels: [],
|
||||
killfeedChannel: "",
|
||||
showKillfeedCoords: 0,
|
||||
connectionLogsChannel: "",
|
||||
activePlayersChannel: "",
|
||||
welcomeChannel: "",
|
||||
factionArmbands: {},
|
||||
usedArmbands: [],
|
||||
excludedRoles: [],
|
||||
botAdminRoles: [],
|
||||
playerstats: [],
|
||||
alarms: [],
|
||||
events: [],
|
||||
uavs: [],
|
||||
incomeRoles: [],
|
||||
incomeLimiter: 168, // # of hours in 7 days
|
||||
linkedGamertagRole: "",
|
||||
startingBalance: 500,
|
||||
uavPrice: 50000,
|
||||
empPrice: 500000,
|
||||
memberRole: "",
|
||||
adminRole: "",
|
||||
combatLogTimer: 5, // minutes
|
||||
}
|
||||
}
|
||||
|
||||
getDefaultPlayerStats(gt, pID) {
|
||||
return {
|
||||
gamertag: gt,
|
||||
playerID: pID,
|
||||
discordID: "",
|
||||
KDR: 0.00,
|
||||
kills: 0,
|
||||
deaths: 0,
|
||||
killStreak: 0,
|
||||
bestKillStreak: 0,
|
||||
longestKill: 0,
|
||||
deathStreak: 0,
|
||||
worstDeathStreak: 0,
|
||||
pos: [],
|
||||
lastPos: [],
|
||||
time: null,
|
||||
lastTime: null,
|
||||
lastConnectionDate: null,
|
||||
lastDisconnectionDate: null,
|
||||
lastDamageDate: null,
|
||||
lastDeathDate: null,
|
||||
lastHitBy: null,
|
||||
connected: false,
|
||||
totalSessionTime: 0,
|
||||
lastSessionTime: 0,
|
||||
longestSessionTime: 0,
|
||||
bounties: [],
|
||||
}
|
||||
}
|
||||
|
||||
async GetGuild(GuildId) {
|
||||
let guild = undefined;
|
||||
if (this.databaseConnected) guild = await this.dbo.collection("guilds").findOne({"server.serverID":GuildId}).then(guild => guild);
|
||||
@@ -471,7 +408,7 @@ class DayzRBot extends Client {
|
||||
// If guild not found, generate guild default
|
||||
if (!guild) {
|
||||
guild = {}
|
||||
guild.server = this.getDefaultSettings(GuildId);
|
||||
guild.server = getDefaultSettings(GuildId);
|
||||
if (this.databaseConnected) {
|
||||
this.dbo.collection("guilds").insertOne(guild, (err, res) => {
|
||||
if (err) throw err;
|
||||
@@ -1,41 +0,0 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
let userSchema = mongoose.Schema({
|
||||
user: {
|
||||
userID: String,
|
||||
guilds: {}
|
||||
},
|
||||
});
|
||||
|
||||
userSchema.methods.createUser = function (userID, guildID, startingBalance) {
|
||||
this.user.userID = userID;
|
||||
this.user.guilds = {};
|
||||
this.user.guilds[guildID] = {
|
||||
balance: startingBalance,
|
||||
lastIncome: new Date('2000-01-01T00:00:00'),
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
This function is to add a new guild specific user to an already existing
|
||||
user document
|
||||
or
|
||||
can be used to reset a data back to default
|
||||
*/
|
||||
function addUser(guilds, guildID, userID, client, startingBalance) {
|
||||
let updatedGuilds = guilds;
|
||||
updatedGuilds[guildID] = {
|
||||
balance: startingBalance,
|
||||
lastIncome: new Date('2000-01-01T00:00:00')
|
||||
}
|
||||
|
||||
client.dbo.collection("users").updateOne({"user.userID":userID}, {$set: {"user.guilds": updatedGuilds}}, (err, res) => {
|
||||
if (err) return false
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
User: mongoose.model('User', userSchema),
|
||||
addUser: addUser,
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const { destinations } = require('../config/destinations');
|
||||
const { destinations } = require('../database/destinations');
|
||||
const { calculateVector } = require('./vector');
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const { BanPlayer, UnbanPlayer } = require('./NitradoAPI');
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const { calculateVector } = require('./vector');
|
||||
const { destinations } = require('../config/destinations');
|
||||
const { destinations } = require('../database/destinations');
|
||||
|
||||
// Private functions (only called locally)
|
||||
|
||||
|
||||
+14
-25
@@ -1,8 +1,9 @@
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const { User, addUser } = require('../structures/user');
|
||||
const { createUser, addUser } = require('../database/user');
|
||||
const { KillInAlarm } = require('./AlarmsHandler');
|
||||
const { destinations } = require('../config/destinations');
|
||||
const { destinations } = require('../database/destinations');
|
||||
const { calculateVector } = require('./vector');
|
||||
const { getDefaultPlayerStats } = require('../database/playerStatistics');
|
||||
|
||||
const Templates = {
|
||||
Killed: 1,
|
||||
@@ -59,7 +60,7 @@ module.exports = {
|
||||
|
||||
let victimStat = stats.find(stat => stat.playerID == info.victimID);
|
||||
let victimStatIndex = stats.indexOf(victimStat);
|
||||
if (victimStat == undefined) victimStat = client.getDefaultPlayerStats(info.victim, info.victimID);
|
||||
if (victimStat == undefined) victimStat = getDefaultPlayerStats(info.victim, info.victimID);
|
||||
victimStat.lastDeathDate = newDt;
|
||||
if (victimStatIndex == -1) stats.push(victimStat);
|
||||
else stats[victimStatIndex] = victimStat;
|
||||
@@ -146,7 +147,7 @@ module.exports = {
|
||||
if (killedBy == Templates.LandMine || killedBy == Templates.Explosion || killedBy == Templates.Vehicle) {
|
||||
let victimStat = stats.find(stat => stat.playerID == info.victimID)
|
||||
let victimStatIndex = stats.indexOf(victimStat);
|
||||
if (victimStat == undefined) victimStat = client.getDefaultPlayerStats(info.victim, info.victimID);
|
||||
if (victimStat == undefined) victimStat = getDefaultPlayerStats(info.victim, info.victimID);
|
||||
victimStat.lastDeathDate = newDt;
|
||||
if (victimStatIndex == -1) stats.push(victimStat);
|
||||
else stats[victimStatIndex] = victimStat;
|
||||
@@ -172,8 +173,8 @@ module.exports = {
|
||||
let victimStat = stats.find(stat => stat.playerID == info.victimID)
|
||||
let killerStatIndex = stats.indexOf(killerStat);
|
||||
let victimStatIndex = stats.indexOf(victimStat);
|
||||
if (killerStat == undefined) killerStat = client.getDefaultPlayerStats(info.killer, info.killerID);
|
||||
if (victimStat == undefined) victimStat = client.getDefaultPlayerStats(info.victim, info.victimID);
|
||||
if (killerStat == undefined) killerStat = getDefaultPlayerStats(info.killer, info.killerID);
|
||||
if (victimStat == undefined) victimStat = getDefaultPlayerStats(info.victim, info.victimID);
|
||||
|
||||
killerStat.kills++;
|
||||
killerStat.killStreak++;
|
||||
@@ -197,28 +198,16 @@ module.exports = {
|
||||
|
||||
let banking = await client.dbo.collection("users").findOne({"user.userID": killerStat.discordID}).then(banking => banking);
|
||||
|
||||
|
||||
if (!banking) {
|
||||
banking = {
|
||||
userID: killerStat.discordID,
|
||||
guilds: {
|
||||
[guildId]: {
|
||||
balance: guild.startingBalance,
|
||||
}
|
||||
}
|
||||
banking = await createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance, client)
|
||||
if (!client.exists(banking)) return client.sendInternalError(interaction, err);
|
||||
}
|
||||
banking = banking.user;
|
||||
|
||||
// Register bank for user
|
||||
let newBank = new User();
|
||||
newBank.createUser(killerStat.discordID, guildId, guild.startingBalance);
|
||||
newBank.save().catch(err => {
|
||||
if (err) return client.sendError(client.GetChannel(guild.killfeedChannel), err);
|
||||
});
|
||||
|
||||
} else banking = banking.user;
|
||||
|
||||
if (!client.exists(banking.guilds[guildId])) {
|
||||
const success = addUser(banking.guilds, guildId, killer.discordID, this, guild.startingBalance);
|
||||
if (!success) return client.sendError(client.GetChannel(guild.killfeedChannel), 'Automatic Bounty Payout: Failed to add bank to database.');
|
||||
if (!client.exists(banking.guilds[GuildDB.serverID])) {
|
||||
const success = addUser(banking.guilds, GuildDB.serverID, interaction.member.user.id, client, GuildDB.startingBalance);
|
||||
if (!success) return client.sendInternalError(interaction, 'Failed to add bank');
|
||||
}
|
||||
|
||||
const newBalance = banking.guilds[guildId].balance + totalBounty;
|
||||
|
||||
+5
-6
@@ -1,8 +1,7 @@
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const { HandleAlarmsAndUAVs } = require('./AlarmsHandler');
|
||||
const { SendConnectionLogs, DetectCombatLog } = require('./AdminLogsHandler');
|
||||
|
||||
// custom util imports
|
||||
const { getDefaultPlayerStats } = require('../database/playerStatistics');
|
||||
const { FetchServerSettings } = require('../util/NitradoAPI');
|
||||
|
||||
let lastSendMessage;
|
||||
@@ -31,7 +30,7 @@ module.exports = {
|
||||
|
||||
let playerStat = stats.find(stat => stat.playerID == info.playerID);
|
||||
let playerStatIndex = stats.indexOf(playerStat);
|
||||
if (playerStat === undefined) playerStat = client.getDefaultPlayerStats(info.player, info.playerID);
|
||||
if (playerStat === undefined) playerStat = getDefaultPlayerStats(info.player, info.playerID);
|
||||
|
||||
const newDt = await client.getDateEST(info.time);
|
||||
|
||||
@@ -77,7 +76,7 @@ module.exports = {
|
||||
|
||||
let playerStat = stats.find(stat => stat.playerID == info.playerID);
|
||||
let playerStatIndex = stats.indexOf(playerStat);
|
||||
if (playerStat === undefined) playerStat = client.getDefaultPlayerStats(info.player, info.playerID);
|
||||
if (playerStat === undefined) playerStat = getDefaultPlayerStats(info.player, info.playerID);
|
||||
|
||||
const newDt = await client.getDateEST(info.time);
|
||||
const unixTime = Math.round(newDt.getTime() / 1000); // Seconds
|
||||
@@ -129,7 +128,7 @@ module.exports = {
|
||||
|
||||
let playerStat = stats.find(stat => stat.playerID == info.playerID);
|
||||
let playerStatIndex = stats.indexOf(playerStat);
|
||||
if (playerStat === undefined) playerStat = client.getDefaultPlayerStats(info.player, info.playerID);
|
||||
if (playerStat === undefined) playerStat = getDefaultPlayerStats(info.player, info.playerID);
|
||||
if (!client.exists(playerStat.lastConnectionDate)) playerStat.lastConnectionDate = await client.getDateEST(info.time);
|
||||
|
||||
playerStat.lastPos = playerStat.pos;
|
||||
@@ -168,7 +167,7 @@ module.exports = {
|
||||
|
||||
let playerStat = stats.find(stat => stat.playerID == info.playerID);
|
||||
let playerStatIndex = stats.indexOf(playerStat);
|
||||
if (playerStat === undefined) playerStat = client.getDefaultPlayerStats(info.player, info.playerID);
|
||||
if (playerStat === undefined) playerStat = getDefaultPlayerStats(info.player, info.playerID);
|
||||
|
||||
playerStat.lastDamageDate = await client.getDateEST(info.time);
|
||||
playerStat.lastHitBy = info.attacker;
|
||||
|
||||
Reference in new issue
Block a user