Haha-Yes/commands/utility/seetag.js

108 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-12-30 01:20:24 +01:00
const { Command } = require('discord-akairo');
2019-07-08 22:45:41 +02:00
const Tag = require('../../models').Tag;
2018-12-30 02:14:43 +01:00
const fs = require('fs');
const os = require('os');
2019-07-08 22:45:41 +02:00
2019-07-12 17:48:50 +02:00
class seetagCommand extends Command {
2019-01-02 08:09:45 +01:00
constructor() {
super('taglist', {
2019-07-12 17:48:50 +02:00
aliases: ['seetag', 'taglist', 'tags'],
2019-01-02 08:09:45 +01:00
category: 'utility',
2019-11-22 20:56:54 +01:00
clientPermissions: ['SEND_MESSAGES', 'EMBED_LINKS', 'ATTACH_FILES'],
2020-03-19 23:24:31 +01:00
channel: 'guild',
2019-02-10 14:57:39 +01:00
args: [
{
id: 'raw',
2019-07-08 22:45:41 +02:00
match: 'rest',
optional: true
2019-07-09 01:47:24 +02:00
},
{
2019-07-12 05:43:32 +02:00
id: 'all',
2019-07-09 01:47:24 +02:00
match: 'flag',
2019-07-12 05:43:32 +02:00
flag: '--all',
2019-02-10 14:57:39 +01:00
}
],
2019-01-02 08:09:45 +01:00
description: {
2019-07-17 01:17:10 +02:00
content: 'Show the list of tag for this server. --all to get a txt file with info about every tag on the server',
usage: '[name of tag]',
2019-01-02 08:09:45 +01:00
examples: ['']
}
});
}
2018-12-07 01:45:40 +01:00
2019-02-10 14:57:39 +01:00
async exec(message, args) {
2019-07-08 22:45:41 +02:00
if (args.raw) {
let tagList = await Tag.findOne({attributes: ['trigger','response','ownerID'], where: {trigger: args.raw, serverID: message.guild.id}});
2019-11-19 17:27:46 +01:00
if (!tagList) return message.channel.send('Tag not found.');
2019-07-12 05:43:32 +02:00
this.client.users.fetch(tagList.dataValues.ownerID)
.then(user => {
2019-11-22 12:30:20 +01:00
const TagEmbed = this.client.util.embed()
2020-03-22 21:54:19 +01:00
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
2019-07-12 05:43:32 +02:00
.setTitle(message.guild.name)
.addField('Trigger:', tagList['dataValues']['trigger'])
.addField('Response:', tagList['dataValues']['response'])
2019-07-12 05:51:00 +02:00
.addField('Creator:', `${user.username}#${user.discriminator} (${user.id})`);
2019-07-12 05:43:32 +02:00
return message.channel.send(TagEmbed)
.catch(() => {
tagTxt(args.raw, tagList)
.then(path => {
return message.channel.send('This tag is to big to be shown on discord! Sending it as a file', {files: [path]});
});
});
2019-07-17 01:17:10 +02:00
})
.catch(() => {
2019-11-22 12:30:20 +01:00
const TagEmbed = this.client.util.embed()
2020-03-22 21:54:19 +01:00
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
2019-07-17 01:17:10 +02:00
.setTitle(message.guild.name)
.addField('Trigger:', tagList['dataValues']['trigger'])
.addField('Response:', tagList['dataValues']['response'])
.addField('Creator:', 'No user info.');
return message.channel.send(TagEmbed)
.catch(() => {
tagTxt(args.raw, tagList)
.then(path => {
return message.channel.send('This tag is to big to be shown on discord! Sending it as a file', {files: [path]});
});
});
2019-07-12 05:43:32 +02:00
});
} else if (args.all) {
2019-07-08 22:45:41 +02:00
let tagList = await Tag.findAll({attributes: ['trigger','response','ownerID'], where: {serverID: message.guild.id}});
2019-11-19 17:27:46 +01:00
if (!tagList[0]) return message.channel.send('This guild do not have any tag.');
2019-07-12 05:43:32 +02:00
var tagArray = [];
2019-07-08 22:45:41 +02:00
tagList.forEach(tag => {
2019-07-12 05:43:32 +02:00
tagArray.push(tag.dataValues);
});
tagTxt('taglist', tagArray)
.then(path => {
return message.channel.send('Here are all your tags!', {files: [path]});
});
2019-07-12 05:43:32 +02:00
} else {
let tagList = await Tag.findAll({attributes: ['trigger'], where: {serverID: message.guild.id}});
const tagString = tagList.map(t => t.trigger).join(', ') || 'No tags set.';
2019-11-22 12:30:20 +01:00
const TagEmbed = this.client.util.embed()
2020-03-22 21:54:19 +01:00
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
2019-07-12 05:43:32 +02:00
.setTitle('List of tags')
.setDescription(tagString)
.setFooter('Use this command with the name of the tag to see more info about it!');
return message.channel.send(TagEmbed)
.catch(() => {
tagTxt('tags', tagList)
.then(path => {
2019-11-21 23:44:05 +01:00
return message.channel.send('There is too much tag to be shown on discord! Sending it as a file', {files: [path]});
});
});
}
async function tagTxt(name, tagArray) {
let path = `${os.tmpdir()}/${name.substring(0, 10)}.json`;
fs.writeFile(path,JSON.stringify(tagArray, null, 2), function(err) {
if (err) return console.error(err);
});
return path;
2019-07-08 22:45:41 +02:00
}
2019-01-02 08:09:45 +01:00
}
2018-12-30 01:20:24 +01:00
}
2019-07-12 17:48:50 +02:00
module.exports = seetagCommand;