2020-01-25 10:02:43 +00:00
|
|
|
module.exports = client => {
|
|
|
|
// Permission level function
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Guild settings function
|
|
|
|
client.getSettings = guild => {
|
|
|
|
const defaults = client.config.defaultSettings || {};
|
|
|
|
if (!guild) return defaults;
|
|
|
|
const guildData = client.settings.get(guild) || {};
|
|
|
|
const returnObject = {};
|
|
|
|
Object.keys(defaults).forEach(key => {
|
|
|
|
returnObject[key] = guildData[key] ? guildData[key] : defaults[key];
|
|
|
|
});
|
|
|
|
return returnObject;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Single line await messages
|
|
|
|
client.awaitReply = async (msg, question, limit = 60000) => {
|
|
|
|
const filter = m => m.author.id === msg.author.id;
|
|
|
|
await msg.channel.send(question);
|
|
|
|
try {
|
|
|
|
const collected = await msg.channel.awaitMessages(filter, {
|
|
|
|
max: 1,
|
|
|
|
time: limit,
|
|
|
|
errors: ["time"]
|
|
|
|
});
|
|
|
|
return collected.first().content;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Message clean function
|
|
|
|
client.clean = async (client, text) => {
|
|
|
|
if (text && text.constructor.name == "Promise") text = await text;
|
|
|
|
if (typeof evaled !== "string")
|
|
|
|
text = require("util").inspect(text, { depth: 1 });
|
|
|
|
|
|
|
|
text = text
|
|
|
|
.replace(/`/g, "`" + String.fromCharCode(8203))
|
|
|
|
.replace(/@/g, "@" + String.fromCharCode(8203))
|
|
|
|
.replace(
|
|
|
|
client.token,
|
|
|
|
"NaKzDzgwNDef1Nitl3YmDAy.tHEvdg.r34L.whl7sTok3N.18n4Ryj094p"
|
|
|
|
);
|
|
|
|
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
client.loadCommand = commandName => {
|
|
|
|
try {
|
|
|
|
const props = require(`../commands/${commandName}`);
|
|
|
|
if (props.init) {
|
|
|
|
props.init(client);
|
|
|
|
}
|
|
|
|
client.commands.set(props.help.name, props);
|
|
|
|
props.conf.aliases.forEach(alias => {
|
|
|
|
client.aliases.set(alias, props.help.name);
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
} catch (e) {
|
|
|
|
return `Failed to load command ${commandName}: ${e}`;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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 `<:error:466995152976871434> The command \`${commandName}\` could not be found.`;
|
|
|
|
|
|
|
|
if (command.shutdown) {
|
|
|
|
await command.shutdown(client);
|
|
|
|
};
|
|
|
|
const mod = require.cache[require.resolve(`../commands/${commandName}`)];
|
|
|
|
delete require.cache[require.resolve(`../commands/${commandName}.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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// MEMBER SEARCH
|
|
|
|
client.searchForMembers = function(guild, query) {
|
|
|
|
if (!query) return;
|
|
|
|
query = query.toLowerCase();
|
|
|
|
|
|
|
|
var a = [];
|
|
|
|
var b;
|
|
|
|
|
|
|
|
try {
|
2020-03-09 01:11:33 +00:00
|
|
|
b = guild.members.cache.find(x => x.displayName.toLowerCase() == query);
|
|
|
|
if (!b) guild.members.cache.find(x => x.user.username.toLowerCase() == query);
|
2020-01-25 10:02:43 +00:00
|
|
|
} catch (err) {};
|
|
|
|
if (b) a.push(b);
|
2020-03-09 01:11:33 +00:00
|
|
|
guild.members.cache.forEach(member => {
|
2020-01-25 10:02:43 +00:00
|
|
|
if (
|
|
|
|
(member.displayName.toLowerCase().startsWith(query) ||
|
2020-03-09 01:11:33 +00:00
|
|
|
member.user.tag.toLowerCase().startsWith(query)) &&
|
2020-01-25 10:02:43 +00:00
|
|
|
member.id != (b && b.id)
|
|
|
|
) {
|
|
|
|
a.push(member);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return a;
|
|
|
|
};
|
|
|
|
|
2020-03-11 10:46:13 +00:00
|
|
|
// USER OBJECT FROM MENTION
|
|
|
|
client.getUserFromMention = mention => {
|
|
|
|
if (!mention) return;
|
|
|
|
|
|
|
|
if (mention.startsWith('<@') && mention.endsWith('>')) {
|
|
|
|
mention = mention.slice(2, -1);
|
|
|
|
|
|
|
|
if (mention.startsWith('!')) {
|
|
|
|
mention = mention.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.users.cache.get(mention);
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 10:02:43 +00:00
|
|
|
|
2020-03-09 01:11:33 +00:00
|
|
|
//FIND ROLE
|
|
|
|
client.findRole = function(input, message) {
|
|
|
|
var role;
|
|
|
|
role = message.guild.roles.cache.find(r => r.name.toLowerCase() === input.toLowerCase());
|
|
|
|
if(!role) {
|
|
|
|
role = message.guild.roles.cache.get(input.toLowerCase());
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!role) {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
return role;
|
|
|
|
};
|
|
|
|
|
|
|
|
// EMBED COLOUR
|
|
|
|
client.embedColour = function(msg) {
|
|
|
|
if(!msg.guild) {
|
|
|
|
return ["#ff9d68", "#ff97cb", "#d789ff", "#74FFFF"].random();
|
|
|
|
} else {
|
|
|
|
return msg.guild.member(client.user).displayHexColor;
|
|
|
|
};
|
|
|
|
};
|
2020-01-25 10:02:43 +00:00
|
|
|
|
2020-03-22 13:01:54 +00:00
|
|
|
// FIND RANDOM INT BETWEEN TWO INTEGERS
|
|
|
|
client.intBetween = function(min, max){
|
|
|
|
return Math.round((Math.random() * (max - min))+min);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-01-25 10:02:43 +00:00
|
|
|
// <String>.toPropercase() returns a proper-cased string
|
|
|
|
Object.defineProperty(String.prototype, "toProperCase", {
|
|
|
|
value: function() {
|
|
|
|
return this.replace(
|
|
|
|
/([^\W_]+[^\s-]*) */g,
|
|
|
|
txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// <Array>.random() returns a single random element from an array
|
|
|
|
Object.defineProperty(Array.prototype, "random", {
|
|
|
|
value: function() {
|
|
|
|
return this[Math.floor(Math.random() * this.length)];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// `await client.wait(1000);` to "pause" for 1 second.
|
|
|
|
client.wait = require("util").promisify(setTimeout);
|
|
|
|
|
|
|
|
// These 2 process methods will catch exceptions and give *more details* about the error and stack trace.
|
|
|
|
process.on("uncaughtException", err => {
|
|
|
|
const errorMsg = err.stack.replace(new RegExp(`${__dirname}/`, "g"), "./");
|
|
|
|
client.logger.error(`Uncaught Exception: ${errorMsg}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on("unhandledRejection", err => {
|
2020-04-18 04:11:13 +00:00
|
|
|
client.logger.error(`Unhandled rejection: ${err}`);
|
2020-01-25 10:02:43 +00:00
|
|
|
});
|
|
|
|
};
|