Finished web archives, added suppor for custom emoji for panel reaction, minor fixes

This commit is contained in:
Isaac (eartharoid)
2020-08-25 15:55:20 +01:00
parent 4009daffbb
commit 015351afe5
7 changed files with 80 additions and 51 deletions

View File

@@ -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}`);

View File

@@ -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

View File

@@ -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}`);

View File

@@ -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)

View File

@@ -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;

View File

@@ -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
});
});