This commit is contained in:
Loïc Bersier 2019-07-08 22:45:41 +02:00
parent 94b9e77437
commit 57f4c797e9

View file

@ -1,8 +1,8 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const { MessageEmbed } = require('discord.js'); const Tag = require('../../models').Tag;
const reload = require('auto-reload');
const fs = require('fs'); const fs = require('fs');
class taglistCommand extends Command { class taglistCommand extends Command {
constructor() { constructor() {
super('taglist', { super('taglist', {
@ -12,7 +12,8 @@ class taglistCommand extends Command {
args: [ args: [
{ {
id: 'raw', id: 'raw',
type: 'string', match: 'rest',
optional: true
} }
], ],
description: { description: {
@ -24,46 +25,22 @@ class taglistCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
try { if (args.raw) {
var customresponse = reload(`../../tag/${message.guild.id}.json`); let tagList = await Tag.findOne({attributes: ['trigger','response','ownerID'], where: {trigger: args.raw, serverID: message.guild.id}});
var count = Object.keys(customresponse).length;
} catch (err) {
message.channel.send('An error has occured, do you have any tags on the server?');
console.error(err);
}
await fs.readFile(`./tag/${message.guild.id}.json`, 'utf8', function readFileCallback(err, data) {
if (err) {
console.log(err);
return;
}
let json = JSON.parse(data);
if (args.raw) {
const tagEmbed = new MessageEmbed()
.setColor('#ff9900')
.setTitle(args.raw)
.setDescription(json[args.raw]);
return message.channel.send(tagEmbed); return message.channel.send(JSON.stringify(tagList, null, 2), {code: true});
} } else {
let tagList = await Tag.findAll({attributes: ['trigger','response','ownerID'], where: {serverID: message.guild.id}});
json = JSON.stringify(data); let tagArray = [];
json = json.replace(/[{}'\\]+/g, ''); tagList.forEach(tag => {
json = json.replace(/,+/g, '\n'); tagArray.push(tag['dataValues']);
json = json.replace(/"/g, ' ');
const tagEmbed = new MessageEmbed()
.setColor('#ff9900')
.setTitle('Tags list')
.setDescription(`Trigger:Response\n\n${json}`)
.setFooter(`You have ${count} tags on this server`);
message.channel.send(tagEmbed).catch(() => {
return message.channel.send('There is too much tag to be shown in an embed, sending file', {files: [`./tag/${message.guild.id}.json`]});
}); });
}); fs.writeFile('/tmp/tagslist.txt',JSON.stringify(tagArray, null, 2), function(err) {
if (err) return console.error(err);
return message.channel.send('Here are your tags', {files: ['/tmp/tagslist.txt']});
});
}
} }
} }
module.exports = taglistCommand; module.exports = taglistCommand;