combined getGuild & createGuild, added user funcs
This commit is contained in:
parent
da505f8554
commit
cdddb4d11d
6 changed files with 43 additions and 17 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
|
@ -1 +0,0 @@
|
|||
// will fix invalid db entries (invalid ids and stuff)
|
Loading…
Reference in a new issue