From cdddb4d11de36e455395e5910e6008b46acba8f6 Mon Sep 17 00:00:00 2001 From: mudkipscience Date: Mon, 6 Apr 2020 15:53:41 +1000 Subject: [PATCH] combined getGuild & createGuild, added user funcs --- events/guildCreate.js | 5 +- util/functions.js => helpers/_functions.js | 54 +++++++++++++++++----- util/webhooks.js => helpers/botlists.js | 0 {util => helpers}/mongoose.js | 0 {util => helpers}/music.js | 0 util/dbfix.js | 1 - 6 files changed, 43 insertions(+), 17 deletions(-) rename util/functions.js => helpers/_functions.js (83%) rename util/webhooks.js => helpers/botlists.js (100%) rename {util => helpers}/mongoose.js (100%) rename {util => helpers}/music.js (100%) delete mode 100644 util/dbfix.js diff --git a/events/guildCreate.js b/events/guildCreate.js index 6903c0f..3eb2e46 100644 --- a/events/guildCreate.js +++ b/events/guildCreate.js @@ -4,10 +4,7 @@ module.exports = async (client, guild) => { // Create DB entry for newly joined guild try { - const newGuild = { - guildID: guild.id - } - await client.createGuild(newGuild) + await client.findOrCreateGuild(guild) } catch (err) { client.logger.error('Failed to create DB entry for newly joined guild: ' + err) } diff --git a/util/functions.js b/helpers/_functions.js similarity index 83% rename from util/functions.js rename to helpers/_functions.js index af64cd5..59a4ec0 100644 --- a/util/functions.js +++ b/helpers/_functions.js @@ -1,5 +1,6 @@ const mongoose = require('mongoose') const Guild = require('../models/guild') +const User = require('../models/user') module.exports = client => { // Permission level function @@ -19,17 +20,19 @@ module.exports = client => { return permlvl } - // Get settings - client.getGuild = async (guild) => { + // Get guild settings + client.findOrCreateGuild = async (guild) => { const data = await Guild.findOne({ guildID: guild.id }) if (data) { return data } else { - throw new Error('No entry for this guild was found in the database!') + const merged = Object.assign({ _id: mongoose.Types.ObjectId() }, { guildID: guild.id }) + const newGuild = await new Guild(merged) + return newGuild.save() } } - // Update settings + // Update guild settings client.updateGuild = async (guild, settings) => { let data = await client.getGuild(guild) @@ -43,14 +46,7 @@ module.exports = client => { return data.updateOne(settings) } - // Create new entry for new guild - client.createGuild = async (settings) => { - const merged = Object.assign({ _id: mongoose.Types.ObjectId() }, settings) - const newGuild = await new Guild(merged) - return newGuild.save() - } - - // Delete guild data + // Delete guild settings client.deleteGuild = async (guild) => { const data = await client.getGuild(guild) if (data) { @@ -58,6 +54,40 @@ module.exports = client => { } } + // Get user settings + client.findOrCreateUser = async (user) => { + const data = await User.findOne({ userID: user.id }) + if (data) { + return data + } else { + const merged = Object.assign({ _id: mongoose.Types.ObjectId() }, { userID: user.id }) + const newUser = await new User(merged) + return newUser.save() + } + } + + // Update user settings + client.updateUser = async (user, settings) => { + let data = await client.getUser(user) + + if (typeof data !== 'object') data = {} + for (const key in settings) { + if (data[key] !== settings[key]) { + data[key] = settings[key] + } else return + } + + return data.updateOne(settings) + } + + // Delete user settings + client.deleteUser = async (user) => { + const data = await client.getUser(user) + if (data) { + data.deleteOne({ userID: user.id }) + } + } + // Loads commands client.loadCommand = (commandName) => { try { diff --git a/util/webhooks.js b/helpers/botlists.js similarity index 100% rename from util/webhooks.js rename to helpers/botlists.js diff --git a/util/mongoose.js b/helpers/mongoose.js similarity index 100% rename from util/mongoose.js rename to helpers/mongoose.js diff --git a/util/music.js b/helpers/music.js similarity index 100% rename from util/music.js rename to helpers/music.js diff --git a/util/dbfix.js b/util/dbfix.js deleted file mode 100644 index b03aec6..0000000 --- a/util/dbfix.js +++ /dev/null @@ -1 +0,0 @@ -// will fix invalid db entries (invalid ids and stuff)