resorted _functions

This commit is contained in:
Emily 2020-04-18 17:52:42 +10:00
parent bb2239bda1
commit 33f76c959a

View file

@ -1,137 +1,11 @@
// Functions in this file are bound to the client object, and available basically anywhere.
const { MessageEmbed } = require('discord.js')
const mongoose = require('mongoose')
const Guild = require('../models/guild')
const User = require('../models/user')
module.exports = client => {
// Permission level function
client.permlevel = (message, settings) => {
let permlvl = 0
const permOrder = client.config.permLevels.slice(0).sort((p, c) => p.level < c.level ? 1 : -1)
while (permOrder.length) {
const currentLevel = permOrder.shift()
if (message.guild && currentLevel.guildOnly) continue
if (currentLevel.check(message, settings)) {
permlvl = currentLevel.level
break
}
}
return permlvl
}
// Get guild settings
client.findOrCreateGuild = async (guild) => {
const data = await Guild.findOne({ guildID: guild.id })
if (data) {
return data
} else {
const merged = Object.assign({ _id: mongoose.Types.ObjectId() }, { guildID: guild.id })
const newGuild = await new Guild(merged)
return newGuild.save()
}
}
// Update guild settings
client.updateGuild = async (guild, settings) => {
let data = await client.findOrCreateGuild(guild)
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 guild settings
client.deleteGuild = async (guild) => {
const data = await client.findOrCreateGuild(guild)
if (data) {
data.deleteOne({ guildID: guild.id })
}
}
// 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.findOrCreateUser(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.findOrCreateUser(user)
if (data) {
data.deleteOne({ userID: user.id })
}
}
// Loads commands
client.loadCommand = (commandName) => {
try {
const props = require(`../commands/${commandName}`)
if (props.init) {
props.init(client)
}
client.commands.set(props.help.name, props)
// So commands can each have their own cooldown time
client.cooldown.set(props.help.name, new Map())
props.conf.aliases.forEach(alias => {
client.aliases.set(alias, props.help.name)
})
return false
} catch (e) {
return `Failed to load ${commandName}: ${e}`
}
}
// Command unloader
client.unloadCommand = async (commandName) => {
let command
if (client.commands.has(commandName)) {
command = client.commands.get(commandName)
} else if (client.aliases.has(commandName)) {
command = client.commands.get(client.aliases.get(commandName))
}
if (!command) return `The command \`${commandName}\` doesn't seem to exist, nor is it an alias. Try again!`
if (command.shutdown) {
await command.shutdown(client)
}
const mod = require.cache[require.resolve(`../commands/${command.help.name}`)]
delete require.cache[require.resolve(`../commands/${command.help.name}.js`)]
for (let i = 0; i < mod.parent.children.length; i++) {
if (mod.parent.children[i] === mod) {
mod.parent.children.splice(i, 1)
break
}
}
return false
}
// Creates an embed for when commands are used incorrectly
client.userError = (msg, cmd, err) => {
const embed = new MessageEmbed()
@ -146,6 +20,15 @@ module.exports = client => {
})
}
// Embed colour function
client.embedColour = function (guild) {
if (!guild || guild.member(client.user).displayHexColor === '#000000') {
return ['#ff9d68', '#ff97cb', '#d789ff', '#74FFFF'].random()
} else {
return guild.member(client.user).displayHexColor
}
}
// Clean up input to remove @everyone, token, etc
client.clean = async (client, text) => {
if (text && text.constructor.name === 'Promise') {
@ -178,15 +61,6 @@ module.exports = client => {
}
}
// Embed colour function
client.embedColour = function (guild) {
if (!guild || guild.member(client.user).displayHexColor === '#000000') {
return ['#ff9d68', '#ff97cb', '#d789ff', '#74FFFF'].random()
} else {
return guild.member(client.user).displayHexColor
}
}
// Capitalises the first letter of every word in a string
// eslint-disable-next-line no-extend-native
Object.defineProperty(String.prototype, 'toProperCase', {
@ -273,6 +147,134 @@ module.exports = client => {
return role
}
// Loads commands
client.loadCommand = (commandName) => {
try {
const props = require(`../commands/${commandName}`)
if (props.init) {
props.init(client)
}
client.commands.set(props.help.name, props)
// So commands can each have their own cooldown time
client.cooldown.set(props.help.name, new Map())
props.conf.aliases.forEach(alias => {
client.aliases.set(alias, props.help.name)
})
return false
} catch (e) {
return `Failed to load ${commandName}: ${e}`
}
}
// Permission level function
client.permlevel = (message, settings) => {
let permlvl = 0
const permOrder = client.config.permLevels.slice(0).sort((p, c) => p.level < c.level ? 1 : -1)
while (permOrder.length) {
const currentLevel = permOrder.shift()
if (message.guild && currentLevel.guildOnly) continue
if (currentLevel.check(message, settings)) {
permlvl = currentLevel.level
break
}
}
return permlvl
}
// Get guild settings
client.findOrCreateGuild = async (guild) => {
const data = await Guild.findOne({ guildID: guild.id })
if (data) {
return data
} else {
const merged = Object.assign({ _id: mongoose.Types.ObjectId() }, { guildID: guild.id })
const newGuild = await new Guild(merged)
return newGuild.save()
}
}
// Update guild settings
client.updateGuild = async (guild, settings) => {
let data = await client.findOrCreateGuild(guild)
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 guild settings
client.deleteGuild = async (guild) => {
const data = await client.findOrCreateGuild(guild)
if (data) {
data.deleteOne({ guildID: guild.id })
}
}
// 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.findOrCreateUser(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.findOrCreateUser(user)
if (data) {
data.deleteOne({ userID: user.id })
}
}
// Command unloader
client.unloadCommand = async (commandName) => {
let command
if (client.commands.has(commandName)) {
command = client.commands.get(commandName)
} else if (client.aliases.has(commandName)) {
command = client.commands.get(client.aliases.get(commandName))
}
if (!command) return `The command \`${commandName}\` doesn't seem to exist, nor is it an alias. Try again!`
if (command.shutdown) {
await command.shutdown(client)
}
const mod = require.cache[require.resolve(`../commands/${command.help.name}`)]
delete require.cache[require.resolve(`../commands/${command.help.name}.js`)]
for (let i = 0; i < mod.parent.children.length; i++) {
if (mod.parent.children[i] === mod) {
mod.parent.children.splice(i, 1)
break
}
}
return false
}
// Both of these functions catch errors and log them (maybe we could use sentry?)
process.on('uncaughtException', (err) => {
const errorMsg = err.stack.replace(new RegExp(`${__dirname}/`, 'g'), './')