woomy/modules/functions.js

137 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-03-29 07:45:09 +00:00
module.exports = client => {
2020-03-31 08:24:51 +00:00
client.permlevel = message => {
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)) {
permlvl = currentLevel.level
break
}
}
return permlvl
}
2020-03-31 07:59:09 +00:00
client.loadCommand = (commandName) => {
try {
const props = require(`../commands/${commandName}`)
if (props.init) {
props.init(client)
}
client.commands.set(props.help.name, props)
2020-03-31 08:24:51 +00:00
// So commands can each have their own cooldown time
client.cooldown.set(props.help.name, new Map())
2020-03-31 07:59:09 +00:00
props.conf.aliases.forEach(alias => {
client.aliases.set(alias, props.help.name)
})
return false
} catch (e) {
return `Failed to load ${commandName}: ${e}`
}
}
2020-03-29 07:45:09 +00:00
2020-04-01 08:33:02 +00:00
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))
}
2020-04-03 06:26:16 +00:00
if (!command) return `The command \`${commandName}\` doesn't seem to exist, nor is it an alias. Try again!`
2020-04-01 08:33:02 +00:00
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
}
2020-04-03 06:26:16 +00:00
client.clean = async (client, text) => {
if (text && text.constructor.name === 'Promise') {
text = await text
}
if (typeof text !== 'string') {
text = require('util').inspect(text, { depth: 1 })
}
text = text
.replace(/`/g, '`' + String.fromCharCode(8203))
.replace(/@/g, '@' + String.fromCharCode(8203))
.replace(client.token, 'mfa.VkO_2G4Qv3T--NO--lWetW_tjND--TOKEN--QFTm6YGtzq9PH--4U--tG0')
return text
}
2020-04-01 08:33:02 +00:00
client.getMembers = function (guild, query) {
if (!query) return
query = query.toLowerCase()
var a = []
var b
// MAKE IT SO IT CAN TAKE AN ID
try {
b = guild.members.cache.find(x => x.displayName.toLowerCase() === query)
if (!b) guild.members.cache.find(x => x.user.username.toLowerCase() === query)
} catch (err) {}
if (b) a.push(b)
guild.members.cache.forEach(member => {
if (
(member.displayName.toLowerCase().startsWith(query) ||
member.user.tag.toLowerCase().startsWith(query)) &&
member.id !== (b && b.id)
) {
a.push(member)
}
})
return a
}
2020-04-03 06:26:16 +00:00
client.getMembers = function (guild, query) {
if (!query || typeof query !== 'string') return
2020-04-02 08:24:36 +00:00
2020-04-03 06:26:16 +00:00
const members = []
2020-04-02 08:24:36 +00:00
2020-04-03 06:26:16 +00:00
// Try ID search
if (!isNaN(query) === true) {
members.push(guild.members.cache.get(query))
if (members[0]) {
return members[0]
}
2020-04-02 08:24:36 +00:00
}
2020-04-01 08:33:02 +00:00
2020-04-03 06:26:16 +00:00
// Try username search
2020-04-01 08:33:02 +00:00
try {
2020-04-03 06:26:16 +00:00
guild.members.cache.forEach(m => {
if (m.displayName.toLowerCase().startsWith(query) || m.user.tag.toLowerCase.startsWith(query)) {
members.push(m)
console.log(m)
}
})
return members
2020-04-01 08:33:02 +00:00
} catch (err) {}
}
// Both of these functions catch errors and log them
2020-03-31 07:59:09 +00:00
process.on('uncaughtException', (err) => {
const errorMsg = err.stack.replace(new RegExp(`${__dirname}/`, 'g'), './')
client.logger.fatal(`Uncaught Exception: ${errorMsg}`)
process.exit(1)
})
process.on('unhandledRejection', err => {
client.logger.error(`Unhandled rejection: ${err}`)
})
2020-03-29 07:45:09 +00:00
}