From 015351afe5f94af060a12974e4d28c1a2fd77485 Mon Sep 17 00:00:00 2001 From: "Isaac (eartharoid)" Date: Tue, 25 Aug 2020 15:55:20 +0100 Subject: [PATCH] Finished web archives, added suppor for custom emoji for panel reaction, minor fixes --- src/commands/close.js | 26 +++++---- src/commands/panel.js | 3 +- src/commands/tickets.js | 2 +- src/commands/transcript.js | 2 +- src/events/messageReactionAdd.js | 4 +- src/modules/archive.js | 93 +++++++++++++++++++------------- user/data.json | 1 + 7 files changed, 80 insertions(+), 51 deletions(-) create mode 100644 user/data.json diff --git a/src/commands/close.js b/src/commands/close.js index 74a49a0..f956ea8 100644 --- a/src/commands/close.js +++ b/src/commands/close.js @@ -8,7 +8,9 @@ const ChildLogger = require('leekslazylogger').ChildLogger; const log = new ChildLogger(); -const { MessageEmbed } = require('discord.js'); +const { + MessageEmbed +} = require('discord.js'); const fs = require('fs'); const archive = require('../modules/archive'); @@ -19,7 +21,10 @@ module.exports = { aliases: ['none'], example: 'close #ticket-17', args: false, - async execute(client, message, args, {config, Ticket}) { + async execute(client, message, args, { + config, + Ticket + }) { const guild = client.guilds.cache.get(config.guild); @@ -127,7 +132,7 @@ module.exports = { } catch (e) { log.warn(`Could not create DM channel with ${u.tag}`); } - + let res = {}; const embed = new MessageEmbed() @@ -138,17 +143,16 @@ module.exports = { if (fs.existsSync(`user/transcripts/text/${ticket.get('channel')}.txt`)) { embed.addField('Text transcript', 'See attachment'); - res.files = [ - { - attachment: `user/transcripts/text/${ticket.get('channel')}.txt`, - name: `ticket-${ticket.id}-${ticket.get('channel')}.txt` - } - ]; + res.files = [{ + attachment: `user/transcripts/text/${ticket.get('channel')}.txt`, + name: `ticket-${ticket.id}-${ticket.get('channel')}.txt` + }]; } - if (fs.existsSync(`user/transcripts/raw/${ticket.get('channel')}.log`)) + if (fs.existsSync(`user/transcripts/raw/${ticket.get('channel')}.log`)) embed.addField('Web archive', `${await archive.export(Ticket, channel)}`); - + + if (embed.fields.length < 1) embed.setDescription(`No text transcripts or archive data exists for ticket ${ticket.id}`); diff --git a/src/commands/panel.js b/src/commands/panel.js index 7709a45..e5d4d3c 100644 --- a/src/commands/panel.js +++ b/src/commands/panel.js @@ -58,7 +58,8 @@ module.exports = { .setFooter(guild.name, guild.iconURL()) ); // send new panel - panel.react(config.panel.reaction); // add reaction + let emoji = panel.guild.emojis.cache.get(config.panel.reaction) || config.panel.reaction; + panel.react(emoji); // add reaction Setting.update({ value: message.channel.id }, { where: { key: 'panel_chan_id' }}); // update database Setting.update({ value: panel.id }, { where: { key: 'panel_msg_id' }}); // update database diff --git a/src/commands/tickets.js b/src/commands/tickets.js index 048971f..d125865 100644 --- a/src/commands/tickets.js +++ b/src/commands/tickets.js @@ -91,7 +91,7 @@ module.exports = { let desc = closedTickets.rows[t].topic.substring(0, 30); let transcript = ''; let c = closedTickets.rows[t].channel; - if(fs.existsSync(`user/transcripts/text/${c}.txt`) || fs.existsSync(`user/transcripts/raw/${c}.log`)) + if(fs.existsSync(`user/transcripts/text/${c}.txt`) || config.transcripts.web.enabled) transcript = `\n> Type \`${config.prefix}transcript ${closedTickets.rows[t].id}\` to view.`; closed.push(`> **#${closedTickets.rows[t].id}**: \`${desc}${desc.length > 20 ? '...' : ''}\`${transcript}`); diff --git a/src/commands/transcript.js b/src/commands/transcript.js index 7ae3882..5c6be40 100644 --- a/src/commands/transcript.js +++ b/src/commands/transcript.js @@ -69,7 +69,7 @@ module.exports = { const BASE_URL = config.transcripts.web.server; - if (fs.existsSync(`user/transcripts/raw/${ticket.channel}.log`)) + if (config.transcripts.web.enabled) embed.addField('Web archive', `${BASE_URL}/${ticket.creator}/${ticket.channel}`); if (embed.fields.length < 1) diff --git a/src/events/messageReactionAdd.js b/src/events/messageReactionAdd.js index 4e48eb2..36b0c9c 100644 --- a/src/events/messageReactionAdd.js +++ b/src/events/messageReactionAdd.js @@ -28,7 +28,9 @@ module.exports = { if(r.message.id !== panelID.get('value')) return; - if (r.emoji.name !== config.panel.reaction || u.id === client.user.id) return; + if(u.id === client.user.id) return; + + if (r.emoji.name !== config.panel.reaction && r.emoji.id !== config.panel.reaction) return; let channel = r.message.channel; diff --git a/src/modules/archive.js b/src/modules/archive.js index ab7a913..375acec 100644 --- a/src/modules/archive.js +++ b/src/modules/archive.js @@ -6,10 +6,14 @@ * */ + +const ChildLogger = require('leekslazylogger').ChildLogger; +const log = new ChildLogger(); const lineReader = require('line-reader'); const fs = require('fs'); const dtf = require('@eartharoid/dtf'); const config = require('../../user/' + require('../').config); +const fetch = require('node-fetch'); module.exports.add = (message) => { @@ -45,7 +49,6 @@ module.exports.add = (message) => { // channel entities if (!fs.existsSync(json)) fs.writeFileSync(json, JSON.stringify({ - channel_name: message.channel.name, entities: { users: {}, channels: {}, @@ -71,7 +74,7 @@ module.exports.add = (message) => { avatar: m.user.avatarURL(), username: m.user.username, discriminator: m.user.discriminator, - displayName: m.user.displayName, + displayName: m.user.displayName || m.user.username, color: m.displayColor, badge: m.user.bot ? 'bot' : null }); @@ -92,47 +95,65 @@ module.exports.add = (message) => { module.exports.export = (Ticket, channel) => new Promise((resolve, reject) => { - let ticket = (async () => await Ticket.findOne({ - where: { - channel: channel.id - } - }))(); - - let raw = `user/transcripts/raw/${channel.id}.log`, - json = `user/transcripts/raw/entities/${channel.id}.json`; - - if (!config.transcripts.web.enabled || !fs.existsSync(raw) || !fs.existsSync(json)) - return reject(false); + (async () => { + let ticket = await Ticket.findOne({ + where: { + channel: channel.id + } + }); - let data = JSON.parse(fs.readFileSync(json)); + let raw = `user/transcripts/raw/${channel.id}.log`, + json = `user/transcripts/raw/entities/${channel.id}.json`; + + if (!config.transcripts.web.enabled || !fs.existsSync(raw) || !fs.existsSync(json)) + return reject(false); + + let data = JSON.parse(fs.readFileSync(json)); - data.ticket = { - id: ticket.id, - name: channel.name, - creator: ticket.creator, - channel: channel.id, - topic: channel.topic - }; + data.ticket = { + id: ticket.id, + name: channel.name, + creator: ticket.creator, + channel: channel.id, + topic: channel.topic + }; - data.messages = []; + data.messages = []; - lineReader.eachLine(raw, line => { - let message = JSON.parse(line); - // data.messages[message.id] = message; - let index = data.messages.findIndex(m => m.id === message.id); - - if (index === -1) - data.messages.push(message); - else - data.messages[index] = message; - }, () => { - // fs.writeFileSync('user/data.json', JSON.stringify(data)); // FOR TESTING + lineReader.eachLine(raw, line => { + let message = JSON.parse(line); + let index = data.messages.findIndex(m => m.id === message.id); + if (index === -1) + data.messages.push(message); + else + data.messages[index] = message; + }, () => { + fs.writeFileSync('user/data.json', JSON.stringify(data)); // FOR TESTING - /** + /** * @todo post(data).then() * @todo if 200 OK delete raw .json and .log */ + let endpoint = config.transcripts.web.server; + if (endpoint[endpoint.length - 1] === '/') + endpoint = endpoint.slice(0, -1); + endpoint += `/${data.ticket.creator}/${data.ticket.channel}/?key=${process.env.ARCHIVES_KEY}`; + fetch(endpoint, { + method: 'post', + body: JSON.stringify(data), + headers: { 'Content-Type': 'application/json' }, + }) + .then(res => res.json()) + .then(json => { + if (json.status !== 200) { + log.warn(json); + return resolve(new Error(`${json.status} (${json.message})`)); + } + log.success(`Uploaded ${ticket.id} archive to server`); + resolve(json.url); + }); + }); + + })(); - resolve(config.transcripts.web.server); // json.url - }); }); diff --git a/user/data.json b/user/data.json new file mode 100644 index 0000000..a5cdb28 --- /dev/null +++ b/user/data.json @@ -0,0 +1 @@ +{"entities":{"users":{"475371285531066368":{"avatar":"https://cdn.discordapp.com/avatars/475371285531066368/962b420d036d3f310e4a920f8517d01c.webp","username":"Roadside Assistance","discriminator":"2057","displayName":"Roadside Assistance","color":39321,"badge":"bot"},"319467558166069248":{"avatar":"https://cdn.discordapp.com/avatars/319467558166069248/c0a069ef452737cc75609430a5b6f14f.webp","username":"Eartharoid","discriminator":"2006","displayName":"Eartharoid","color":15277667,"badge":null}},"channels":{},"roles":{}},"ticket":{"id":55,"name":"ticket-55","creator":"319467558166069248","channel":"747831136801259652","topic":"<@319467558166069248> | No topic given"},"messages":[{"id":"747831139024240691","author":"475371285531066368","content":"@here,\n<@319467558166069248> has created a new ticket","time":1598367237574,"embeds":[],"attachments":[]},{"id":"747831144455602268","author":"475371285531066368","content":"","time":1598367238869,"embeds":[],"attachments":[{"attachment":"https://cdn.discordapp.com/attachments/747831136801259652/747831143986102342/how-can-we-help.gif","name":"how-can-we-help.gif","id":"747831143986102342","size":1721116,"url":"https://cdn.discordapp.com/attachments/747831136801259652/747831143986102342/how-can-we-help.gif","proxyURL":"https://media.discordapp.net/attachments/747831136801259652/747831143986102342/how-can-we-help.gif","height":278,"width":498}]},{"id":"747831145437200475","author":"475371285531066368","content":"","time":1598367239103,"embeds":[{"type":"rich","description":"Hello there, <@319467558166069248>!\n\t\tA member of staff will assist you shortly.\n\t\tIn the mean time, please describe your issue in as much detail as possible! :)","timestamp":null,"color":39321,"fields":[{"value":"`No topic given`","name":"Topic","inline":false}],"thumbnail":null,"image":null,"author":{"name":"Eartharoid","icon_url":"https://cdn.discordapp.com/avatars/319467558166069248/c0a069ef452737cc75609430a5b6f14f.webp"},"footer":{"text":"Planet Earth","icon_url":"https://cdn.discordapp.com/icons/451745464480432129/ec04e2401da3b6a1c0b75dfed2beaf15.webp"}}],"attachments":[]},{"id":"747831155121979552","author":"319467558166069248","content":"fjndsg","time":1598367241412,"embeds":[],"attachments":[]},{"id":"747831156703101131","author":"319467558166069248","content":"dsg","time":1598367241789,"embeds":[],"attachments":[]},{"id":"747831158913630238","author":"319467558166069248","content":"dfg","time":1598367242316,"embeds":[],"attachments":[]},{"id":"747831160993742928","author":"319467558166069248","content":"fdg","time":1598367242812,"embeds":[],"attachments":[]},{"id":"747831162193313913","author":"319467558166069248","content":"dfgasdfdsgdfgdf","time":1598367243098,"embeds":[],"attachments":[],"edited":true},{"id":"747831163803926580","author":"319467558166069248","content":"fdg","time":1598367243482,"embeds":[],"attachments":[],"edited":false,"deleted":true},{"id":"747831165091840054","author":"319467558166069248","content":"fdg","time":1598367243789,"embeds":[],"attachments":[],"edited":false,"deleted":true},{"id":"747831166677155852","author":"319467558166069248","content":"fdg","time":1598367244167,"embeds":[],"attachments":[],"edited":false,"deleted":true},{"id":"747831168682164365","author":"319467558166069248","content":"dfg","time":1598367244645,"embeds":[],"attachments":[],"edited":false,"deleted":true},{"id":"747831178194714734","author":"319467558166069248","content":"fdg","time":1598367246913,"embeds":[],"attachments":[],"edited":false,"deleted":true},{"id":"747831180325421107","author":"319467558166069248","content":"fdg","time":1598367247421,"embeds":[],"attachments":[],"edited":false,"deleted":true},{"id":"747831209265987744","author":"319467558166069248","content":"sdg","time":1598367254321,"embeds":[],"attachments":[],"edited":false,"deleted":true},{"id":"747831222423519342","author":"319467558166069248","content":"degfdsg","time":1598367257458,"embeds":[],"attachments":[]},{"id":"747831228748529774","author":"319467558166069248","content":"-close","time":1598367258966,"embeds":[],"attachments":[]},{"id":"747831229558161488","author":"475371285531066368","content":"","time":1598367259159,"embeds":[{"title":":grey_question: Are you sure?","type":"rich","description":"You will be able to view an archived version later with `-transcript 55`\n**React with :white_check_mark: to confirm.**","timestamp":null,"color":39321,"fields":[],"thumbnail":null,"image":null,"author":{"name":"Eartharoid","icon_url":"https://cdn.discordapp.com/avatars/319467558166069248/c0a069ef452737cc75609430a5b6f14f.webp"},"footer":{"text":"Planet Earth | Expires in 15 seconds","icon_url":"https://cdn.discordapp.com/icons/451745464480432129/ec04e2401da3b6a1c0b75dfed2beaf15.webp"}}],"attachments":[]}]} \ No newline at end of file