logger stuff
This commit is contained in:
Eartharoid 2019-05-05 14:31:21 +01:00
parent 524ebaa767
commit 537f59d496
9 changed files with 83 additions and 49 deletions

View File

@ -1,6 +1,6 @@
const Discord = require('discord.js'); const Discord = require('discord.js');
const config = require('../config.json'); const config = require('../config.json');
const leeks = require('leeks.js'); const log = require(`../handlers/logger.js`);
module.exports = { module.exports = {
name: 'add', name: 'add',
description: 'Add a member to a ticket channel', description: 'Add a member to a ticket channel',

View File

@ -1,6 +1,6 @@
const Discord = require('discord.js'); const Discord = require('discord.js');
const config = require('../config.json'); const config = require('../config.json');
const leeks = require('leeks.js'); const log = require(`../handlers/logger.js`);
module.exports = { module.exports = {
name: 'close', name: 'close',
description: 'Close a ticket', description: 'Close a ticket',

View File

@ -1,6 +1,6 @@
const Discord = require('discord.js'); const Discord = require('discord.js');
const config = require('../config.json'); const config = require('../config.json');
const leeks = require('leeks.js'); const log = require(`../handlers/logger.js`);
module.exports = { module.exports = {
name: 'example-command', name: 'example-command',
description: 'An example command', description: 'An example command',

View File

@ -1,7 +1,7 @@
const Discord = require('discord.js'); const Discord = require('discord.js');
const { version } = require('../package.json'); const { version } = require('../package.json');
const config = require('../config.json'); const config = require('../config.json');
const leeks = require('leeks.js'); const log = require(`../handlers/logger.js`);
module.exports = { module.exports = {
name: 'help', name: 'help',
description: 'Displays help menu', description: 'Displays help menu',
@ -47,7 +47,7 @@ module.exports = {
}) })
.catch(error => { .catch(error => {
// console.error(`Could not send help DM to ${message.author.tag}.\n`, error); // console.error(`Could not send help DM to ${message.author.tag}.\n`, error);
console.log(leeks.colors.yellow(leeks.styles.bold(`[WARN] Could not DM help menu to ${message.author.tag}, sending to server channel instead`))); log.warn(`Could not DM help menu to ${message.author.tag}, sending to server channel instead`);
message.channel.send(`:x: **Sorry!** There was an error whilst sending the help menu via DMs.`) message.channel.send(`:x: **Sorry!** There was an error whilst sending the help menu via DMs.`)
message.channel.send(data, { split: true }) message.channel.send(data, { split: true })
}); });
@ -92,7 +92,7 @@ module.exports = {
}) })
.catch(error => { .catch(error => {
// console.error(`Could not send help DM to ${message.author.tag}.\n`, error); // console.error(`Could not send help DM to ${message.author.tag}.\n`, error);
console.log(leeks.colors.yellow(leeks.styles.bold(`[WARN] Could not DM help menu to ${message.author.tag}, sending to server channel instead`))); log.warn(`Could not DM help menu to ${message.author.tag}, sending to server channel instead`);
message.channel.send(`:x: **Sorry!** There was an error whilst sending the help menu via DMs.`) message.channel.send(`:x: **Sorry!** There was an error whilst sending the help menu via DMs.`)
message.channel.send(data, { split: true }) message.channel.send(data, { split: true })
}); });

View File

@ -1,6 +1,6 @@
const Discord = require('discord.js'); const Discord = require('discord.js');
const config = require('../config.json'); const config = require('../config.json');
const leeks = require('leeks.js'); const log = require(`../handlers/logger.js`);
// const randomString = require('random-string'); // const randomString = require('random-string');
module.exports = { module.exports = {
name: 'new', name: 'new',
@ -25,13 +25,18 @@ module.exports = {
// special: false, // special: false,
// }); // });
let id = message.author.id.toString().substr(0,4) + message.author.discriminator; let id = message.author.id.toString().substr(0,4) + message.author.discriminator;
let chan = `ticket-${id}`;
if(message.guild.channels.some(channel => chan.includes(channel.name))) {
if(config.useEmbeds){
const err1 = new Discord.RichEmbed()
.setColor("#E74C3C")
.setDescription(`:x: You already have an open ticket.`)
return message.channel.send(err1)
} else {
message.channel.send(`:x: You already have an open ticket.`)
}
// if(message.guild.channels.some(channel => `ticket-${id}`)) {
if(message.guild.channels.some(includes(`ticket-${id}`))) {
const err1 = new Discord.RichEmbed()
.setColor("#E74C3C")
.setDescription(`:x: You already have a ticket open.`)
return message.channel.send(err1)
}; };
message.guild.createChannel(`ticket-${id}`).then(async c => { message.guild.createChannel(`ticket-${id}`).then(async c => {
@ -79,7 +84,7 @@ module.exports = {
// } else { // } else {
// client.channels.get(config.logChannel).send(`New ticket created by **${message.author.tag} (${message.author.id})**`); // client.channels.get(config.logChannel).send(`New ticket created by **${message.author.tag} (${message.author.id})**`);
// } // }
console.log(leeks.colors.cyan(`${message.author.tag} created a new ticket (#ticket-${id})`)) log.info(`${message.author.tag} created a new ticket (#ticket-${id})`)
// command ends here // command ends here
}, },

View File

@ -1,6 +1,6 @@
const Discord = require('discord.js'); const Discord = require('discord.js');
const config = require('../config.json'); const config = require('../config.json');
const leeks = require('leeks.js'); const log = require(`../handlers/logger.js`);
module.exports = { module.exports = {
name: 'ping', name: 'ping',
description: 'Calculate latency', description: 'Calculate latency',

View File

@ -1,6 +1,6 @@
const Discord = require('discord.js'); const Discord = require('discord.js');
const config = require('../config.json'); const config = require('../config.json');
const leeks = require('leeks.js'); const log = require(`../handlers/logger.js`);
module.exports = { module.exports = {
name: 'remove', name: 'remove',
description: 'Remove a member from a ticket', description: 'Remove a member from a ticket',

30
handlers/logger.js Normal file
View File

@ -0,0 +1,30 @@
const leeks = require('leeks.js');
const now = new Date();
const h = now.getHours();
const m = now.getMinutes();
const s = now.getSeconds();
let timestamp = `${h}:${('0' + m).slice(-2)}:${('0' + s).slice(-2)}`;
exports.basic = (m) => {
console.log(m)
};
exports.console = (m) => {
console.log(`[INFO | ${timestamp}] ${m}`)
};
exports.info = (m) => {
console.info(leeks.colours.cyan(`[INFO | ${timestamp}] ${m}`))
};
exports.success = (m) => {
console.info(leeks.colours.green(`[INFO | ${timestamp}] ${m}`))
};
exports.debug = (m) => {
console.info(leeks.colours.blueBright(`[DEBUG | ${timestamp}] ${m}`))
};
exports.warn = (m) => {
console.warn(leeks.colours.yellowBright(`[WARN | ${timestamp}] ${m}`))
};
exports.error = (m) => {
console.error(leeks.colours.red(`[ERROR | ${timestamp}] ${m}`))
};

View File

@ -41,6 +41,7 @@
const fs = require('fs'); const fs = require('fs');
const Discord = require('discord.js'); const Discord = require('discord.js');
const leeks = require('leeks.js'); const leeks = require('leeks.js');
const log = require(`./handlers/logger.js`);
const config = require('./config.json'); const config = require('./config.json');
const { version, homepage } = require('./package.json'); const { version, homepage } = require('./package.json');
const client = new Discord.Client(); const client = new Discord.Client();
@ -50,7 +51,7 @@ const now = Date.now();
const commands = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); const commands = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
console.log(leeks.colors.magentaBright(` log.basic(leeks.colours.magentaBright(`
######## #### ###### ###### ####### ######## ######## ######## #### ###### ###### ####### ######## ########
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
@ -68,26 +69,27 @@ console.log(leeks.colors.magentaBright(`
## #### ###### ## ## ######## ## ###### ## #### ###### ## ## ######## ## ######
`)); // banner appears in console `)); // banner appears in console
console.log(leeks.colors.yellow(leeks.styles.bold(`DiscordTickets v${version} - Made By Eartharoid`))); log.basic(leeks.colours.yellow(leeks.styles.bold(`DiscordTickets v${version} - Made By Eartharoid`)));
console.log(leeks.colors.yellow(leeks.styles.bold(homepage))); log.basic(leeks.colours.yellow(leeks.styles.bold(homepage)));
console.log(''); log.basic('\n\n');
console.log(`Starting up...`) log.basic(leeks.colours.bgGrey(leeks.colours.grey(`\n\n==========================================================================\n\n`)))
log.basic('\n\n');
log.info(`Starting up...`)
client.once('ready', () => { // after bot has logged in client.once('ready', () => { // after bot has logged in
console.log(leeks.colors.cyan(`Initialising bot...`)) log.info(`Initialising bot...`)
for (const file of commands) { for (const file of commands) {
const command = require(`./commands/${file}`); const command = require(`./commands/${file}`);
client.commands.set(command.name, command); client.commands.set(command.name, command);
console.log(`> Loading '${config.prefix}${command.name}' command`); log.console(`> Loading '${config.prefix}${command.name}' command`);
} }
console.log(leeks.colors.green(`Connected to Discord API`)) log.success(`Connected to Discord API`)
console.log(leeks.colors.green(`Logged in as ${client.user.tag}`)) log.success(`Logged in as ${client.user.tag}`)
client.user.setPresence({game: {name: config.playing, type: config.activityType},status: config.status}) client.user.setPresence({game: {name: config.playing, type: config.activityType},status: config.status})
// .then(console.log) // .then(log.basic)
.catch(console.error); .catch(log.error);
// console.log(leeks.colors.green(`Set playing status as `) + leeks.colors.yellow(`'${config.playing}${config.prefix}help'`));
if (config.useEmbeds) { if (config.useEmbeds) {
const embed = new Discord.RichEmbed() const embed = new Discord.RichEmbed()
@ -95,16 +97,14 @@ client.once('ready', () => { // after bot has logged in
.setColor("#2ECC71") .setColor("#2ECC71")
.setDescription(":white_check_mark: **Started succesfully**") .setDescription(":white_check_mark: **Started succesfully**")
.setFooter(`${client.guilds.get(config.guildID).name} : DiscordTickets by Eartharoid`); .setFooter(`${client.guilds.get(config.guildID).name} : DiscordTickets by Eartharoid`);
client.channels.get(config.logChannel).send({ client.channels.get(config.logChannel).send(embed)
embed
})
} else { } else {
client.channels.get(config.logChannel).send(":white_check_mark: **Started succesfully**") client.channels.get(config.logChannel).send(":white_check_mark: **Started succesfully**")
} }
if (client.guilds.get(config.guildID).member(client.user).hasPermission("ADMINISTRATOR", false)) { if (client.guilds.get(config.guildID).member(client.user).hasPermission("ADMINISTRATOR", false)) {
console.log(leeks.colors.bgYellowBright(leeks.colors.black(`Checking permissions...`))) log.info(`Checking permissions...`);
setTimeout(function() { setTimeout(function() {
console.log(leeks.colors.green(`Required permissions have been granted\n\n`)) log.success(`Required permissions have been granted\n\n`)
}, 1250); }, 1250);
if (config.useEmbeds) { if (config.useEmbeds) {
@ -113,15 +113,13 @@ client.once('ready', () => { // after bot has logged in
.setColor("#2ECC71") .setColor("#2ECC71")
.setDescription(":white_check_mark: **Required permissions have been granted**") .setDescription(":white_check_mark: **Required permissions have been granted**")
.setFooter(`${client.guilds.get(config.guildID).name} : DiscordTickets by Eartharoid`); .setFooter(`${client.guilds.get(config.guildID).name} : DiscordTickets by Eartharoid`);
client.channels.get(config.logChannel).send({ client.channels.get(config.logChannel).send(embed)
embed
})
} else { } else {
client.channels.get(config.logChannel).send(":white_check_mark: **Started succesfully**") client.channels.get(config.logChannel).send(":white_check_mark: **Started succesfully**")
} }
} else { } else {
console.log(leeks.colors.red(`Required permissions have not been granted`)) log.error(`Required permissions have not been granted`)
console.log(leeks.colors.red(`Please give the bot the 'ADMINISTRATOR' permission\n\n`)) log.error(`Please give the bot the 'ADMINISTRATOR' permission\n\n`)
if (config.useEmbeds) { if (config.useEmbeds) {
const embed = new Discord.RichEmbed() const embed = new Discord.RichEmbed()
.setAuthor(`${client.user.username} / Ticket Log`, client.user.avatarURL) .setAuthor(`${client.user.username} / Ticket Log`, client.user.avatarURL)
@ -152,9 +150,7 @@ client.on('message', async message => {
.addField("Username", message.author.tag, true) .addField("Username", message.author.tag, true)
.addField("Message", message.content, true) .addField("Message", message.content, true)
.setFooter(`${client.guilds.get(config.guildID).name} : DiscordTickets by Eartharoid`); .setFooter(`${client.guilds.get(config.guildID).name} : DiscordTickets by Eartharoid`);
client.channels.get(config.logChannel).send({ client.channels.get(config.logChannel).send(embed)
embed
})
} else { } else {
client.channels.get(config.logChannel).send(`DM received from **${message.author.tag} (${message.author.id})** : \n\n\`\`\`${message.content}\`\`\``); client.channels.get(config.logChannel).send(`DM received from **${message.author.tag} (${message.author.id})** : \n\n\`\`\`${message.content}\`\`\``);
} }
@ -235,24 +231,27 @@ client.on('message', async message => {
// client.commands.get(command).execute(message, args, config); // client.commands.get(command).execute(message, args, config);
command.execute(message, args); command.execute(message, args);
} catch (error) { } catch (error) {
console.error(error); log.error(error);
message.channel.send(`:x: **Oof!** An error occured whilst executing that command.\nThe issue has been reported.`); message.channel.send(`:x: **Oof!** An error occured whilst executing that command.\nThe issue has been reported.`);
console.log(leeks.colors.red(`[ERROR] An unknown error occured whilst executing the '${command.name}' command`)); log.error(`An unknown error occured whilst executing the '${command.name}' command`);
} }
}); });
client.on('error', error => { client.on('error', error => {
console.log(leeks.colors.bgYellow(leeks.colors.black(`[WARN] Potential error detected\n(likely Discord API connection issue)`))); log.warn(`Potential error detected\n(likely Discord API connection issue)\n`);
console.error(leeks.colors.red(`[ERROR] Client error:\n${error}`)); log.error(`Client error:\n${error}`);
}); });
client.on('warn', (e) => log.warn(`${e}`));
if(config.debugLevel == 1){ client.on('debug', (e) => log.debug(`${e}`)) };
process.on('unhandledRejection', error => { process.on('unhandledRejection', error => {
console.log(leeks.colors.yellow(leeks.styles.bold(`[WARN] An error was not caught`))); log.warn(`An error was not caught`);
console.error(leeks.colors.red(`[ERROR] Uncaught Promise Error: \n${error.stack}`)); log.error(`Uncaught error: \n${error.stack}`);
}); });
process.on('beforeExit', (code) => { process.on('beforeExit', (code) => {
console.log(leeks.colors.yellow(`Disconected from Discord API`)); log.basic(leeks.colours.yellowBright(`Disconected from Discord API`));
console.log(leeks.colors.yellow(`Exiting (${code})`)); log.basic(`Exiting (${code})`);
}); });
client.login(config.token); client.login(config.token);