new stuff

This commit is contained in:
Emily 2020-08-18 14:58:36 +10:00
commit a5b6425af1
22 changed files with 1375 additions and 0 deletions

94
bot/util/handlers.js Normal file
View file

@ -0,0 +1,94 @@
const fs = require("fs");
class CommandHandler {
constructor (client) {
this.client = client;
}
load (name, category) {
try {
const path = this.client.path + "/commands/" + category + "/" + name + ".js";
const props = new (require(path))(this.client);
this.client.logger.debug(`Loading command ${category}/${name}`);
props.help.name = name;
props.help.category = category;
if (props.init) {
props.init(this.client);
}
this.client.commands.set(props.help.name, props);
props.conf.aliases.forEach(alias => {
this.client.aliases.set(alias, props.help.name);
});
return;
} catch (err) {
return `Failed to load command ${name}: ${err}`;
}
}
unload (name) {
}
loadAll () {
const commandDirectories = fs.readdirSync("./commands/");
this.client.logger.debug(`Found ${commandDirectories.length} command directories.`);
commandDirectories.forEach((dir) => {
const commandFiles = fs.readdirSync("./commands/" + dir + "/");
commandFiles.filter((cmd) => cmd.split(".").pop() === "js").forEach((cmd) => {
cmd = cmd.substring(0, cmd.length - 3);
const resp = this.load(cmd, dir);
if (resp) {
this.client.logger.error(resp);
}
});
});
}
}
class EventHandler {
constructor (client) {
this.client = client;
}
load (name) {
try {
this.client.logger.debug(`Loading event ${name}`);
const path = this.client.path + "/events/" + name + ".js";
const event = new (require(path))(this.client);
this.client.on(name, (...args) => event.run(...args));
delete require.cache[require.resolve(path)];
return;
} catch (err) {
return `Failed to load event ${name}: ${err}`;
}
}
unload (name) {
}
loadAll () {
const eventFiles = fs.readdirSync(this.client.path + "/events");
eventFiles.forEach(file => {
const name = file.split(".")[0];
const resp = this.load(name);
if (resp) {
this.client.logger.error(resp);
}
});
}
}
module.exports = {
CommandHandler: CommandHandler,
EventHandler: EventHandler
};

54
bot/util/logger.js Normal file
View file

@ -0,0 +1,54 @@
const { createLogger, format, transports, addColors } = require("winston");
const { combine, timestamp, printf, colorize } = format;
const fmt = printf(({ level, message, timestamp }) => {
return `${timestamp} - ${level}: ${message}`;
});
const customLevels = {
levels: {
debug: 0,
cmd: 1,
info: 2,
ready: 3,
warn: 4,
error: 5
},
colours: {
debug: "magenta",
cmd: "white",
info: "cyan",
ready: "green",
warn: "yellow",
error: "red"
}
};
const logger = createLogger({
levels: customLevels.levels,
level: "error",
format: combine(
timestamp({
format: "YYYY-MM-DD hh:mm:ss"
}),
fmt
),
transports: [
new transports.Console({
level: "error",
format: combine(
timestamp({
format: "YYYY-MM-DD hh:mm:ss"
}),
colorize(),
fmt
)
})
]
});
addColors(customLevels.colours);
module.exports = logger;

8
bot/util/messageUtil.js Normal file
View file

@ -0,0 +1,8 @@
class MessageUtil {
constructor (client) {
this.client = client;
}
}
module.exports = MessageUtil;

35
bot/util/util.js Normal file
View file

@ -0,0 +1,35 @@
class Util {
constructor (client) {
this.client = client;
}
isDeveloper (userID) {
let isDev = false;
const developers = this.client.config.ownerIDs;
developers.forEach(devID => {
if (devID === userID) {
isDev = true;
}
});
console.log(isDev);
return isDev;
}
async clean (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(this.client.token, "mfa.VkO_2G4Qv3T--NO--lWetW_tjND--TOKEN--QFTm6YGtzq9PH--4U--tG0");
return text;
}
}
module.exports = Util;