galactic-bot/structure/client/components/commands/developer/Log.js

75 lines
2.1 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const { MessageAttachment } = require('discord.js');
const { Command } = require('../../../../interfaces');
class LogCommand extends Command {
constructor(client) {
super(client, {
name: 'log',
module: 'developer',
usage: '<date>',
aliases: [
'logs'
],
arguments: [
{
name: 'error',
aliases: ['errors'],
type: 'BOOLEAN',
types: ['FLAG', 'VERBAL'],
default: true
},
{
name: 'private',
type: 'BOOLEAN',
types: ['FLAG'],
default: true
}
],
restricted: true,
showUsage: true,
archivable: false
});
}
async execute(message, { params, args }) {
let directory = path.join(process.cwd(), 'logs');
if(args.error) directory = path.join(directory, 'errors');
const resolved = this.client.resolver.resolveDate(params.join(' '));
if(!resolved) {
return message.respond(`Unable to find a valid date syntax, must use \`YYYY-MM-DD\`.`, {
emoji: 'failure'
});
}
const date = resolved.format('YYYY-MM-DD');
const filename = `${date}${args.error ? '-error' : ''}.log`;
directory = path.join(directory, filename);
if(!fs.existsSync(directory)) {
return message.respond(`Unable to find a valid log file with that date.`, {
emoji: 'failure'
});
}
const text = fs.readFileSync(directory, { encoding: 'utf8' });
const attachment = new MessageAttachment(Buffer.from(text), filename);
message.respond(`Attached the log file matching that date.`, {
files: [ attachment ],
emoji: 'success',
dm: Boolean(args.private)
});
}
}
module.exports = LogCommand;