v2.1.1 - Paths are no longer relative

This commit is contained in:
Isaac (eartharoid) 2020-10-14 22:37:57 +01:00
parent 726d326656
commit 16876b1408
15 changed files with 68 additions and 58 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@eartharoid/discordtickets", "name": "@eartharoid/discordtickets",
"version": "2.1.0", "version": "2.1.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@eartharoid/discordtickets", "name": "@eartharoid/discordtickets",
"version": "2.1.0", "version": "2.1.1",
"private": true, "private": true,
"description": "An open-source & self-hosted Discord bot for ticket management.", "description": "An open-source & self-hosted Discord bot for ticket management.",
"main": "src/index.js", "main": "src/index.js",

View File

@ -8,10 +8,9 @@
const Logger = require('leekslazylogger'); const Logger = require('leekslazylogger');
const log = new Logger(); const log = new Logger();
const { const { MessageEmbed } = require('discord.js');
MessageEmbed
} = require('discord.js');
const fs = require('fs'); const fs = require('fs');
const { join } = require('path');
const archive = require('../modules/archive'); const archive = require('../modules/archive');
module.exports = { module.exports = {
@ -74,8 +73,8 @@ module.exports = {
); );
let success; let success;
let pre = fs.existsSync(`user/transcripts/text/${channel.id}.txt`) || let pre = fs.existsSync(join(__dirname, `../../user/transcripts/text/${channel.id}.txt`)) ||
fs.existsSync(`user/transcripts/raw/${channel.id}.log`) ? fs.existsSync(join(__dirname, `../../user/transcripts/raw/${channel.id}.log`)) ?
`You will be able to view an archived version later with \`${config.prefix}transcript ${ticket.id}\`` : `You will be able to view an archived version later with \`${config.prefix}transcript ${ticket.id}\`` :
''; '';
@ -136,15 +135,15 @@ module.exports = {
.setTitle(`Ticket ${ticket.id}`) .setTitle(`Ticket ${ticket.id}`)
.setFooter(guild.name, guild.iconURL()); .setFooter(guild.name, guild.iconURL());
if (fs.existsSync(`user/transcripts/text/${ticket.get('channel')}.txt`)) { if (fs.existsSync(join(__dirname, `../../user/transcripts/text/${ticket.get('channel')}.txt`))) {
embed.addField('Text transcript', 'See attachment'); embed.addField('Text transcript', 'See attachment');
res.files = [{ res.files = [{
attachment: `user/transcripts/text/${ticket.get('channel')}.txt`, attachment: join(__dirname, `../../user/transcripts/text/${ticket.get('channel')}.txt`),
name: `ticket-${ticket.id}-${ticket.get('channel')}.txt` name: `ticket-${ticket.id}-${ticket.get('channel')}.txt`
}]; }];
} }
if (fs.existsSync(`user/transcripts/raw/${ticket.get('channel')}.log`) && fs.existsSync(`user/transcripts/raw/entities/${ticket.get('channel')}.json`)) { if (fs.existsSync(join(__dirname, `../../user/transcripts/raw/${ticket.get('channel')}.log`)) && fs.existsSync(join(__dirname, `../../user/transcripts/raw/entities/${ticket.get('channel')}.json`))) {
embed.addField('Web archive', await archive.export(Ticket, channel)); embed.addField('Web archive', await archive.export(Ticket, channel));
} }
@ -154,7 +153,12 @@ module.exports = {
res.embed = embed; res.embed = embed;
try {
dm.send(res).then(); dm.send(res).then();
} catch (e) {
message.channel.send(':x: Couldn\'t send DM');
}
} }
} }

View File

@ -12,6 +12,7 @@ const {
MessageEmbed MessageEmbed
} = require('discord.js'); } = require('discord.js');
const fs = require('fs'); const fs = require('fs');
const { join } = require('path');
module.exports = { module.exports = {
name: 'delete', name: 'delete',
@ -117,9 +118,9 @@ module.exports = {
.setFooter(guild.name, guild.iconURL()) .setFooter(guild.name, guild.iconURL())
); );
let txt = `user/transcripts/text/${ticket.get('channel')}.txt`, let txt = join(__dirname, `../../user/transcripts/text/${ticket.get('channel')}.txt`),
raw = `user/transcripts/raw/${ticket.get('channel')}.log`, raw = join(__dirname, `../../user/transcripts/raw/${ticket.get('channel')}.log`),
json = `user/transcripts/raw/entities/${ticket.get('channel')}.json`; json = join(__dirname, `../../user/transcripts/raw/entities/${ticket.get('channel')}.json`);
if (fs.existsSync(txt)) fs.unlinkSync(txt); if (fs.existsSync(txt)) fs.unlinkSync(txt);
if (fs.existsSync(raw)) fs.unlinkSync(raw); if (fs.existsSync(raw)) fs.unlinkSync(raw);

View File

@ -10,6 +10,7 @@ const Logger = require('leekslazylogger');
const log = new Logger(); const log = new Logger();
const { MessageEmbed } = require('discord.js'); const { MessageEmbed } = require('discord.js');
const fs = require('fs'); const fs = require('fs');
const { join } = require('path');
module.exports = { module.exports = {
name: 'new', name: 'new',
@ -149,11 +150,10 @@ module.exports = {
await c.send(ping + `${message.author} has created a new ticket`); await c.send(ping + `${message.author} has created a new ticket`);
if (config.tickets.send_img) { if (config.tickets.send_img) {
const images = fs.readdirSync('user/images'); const images = fs.readdirSync(join(__dirname, '../../user/images'));
await c.send({ await c.send({
files: [ files: [
'user/images/' + join(__dirname, '../../user/images', images[Math.floor(Math.random() * images.length)])
images[Math.floor(Math.random() * images.length)]
] ]
}); });
} }

View File

@ -15,7 +15,6 @@ module.exports = {
description: 'Create or a panel widget in the channel the command is used in. Note that there can only be 1 panel.', description: 'Create or a panel widget in the channel the command is used in. Note that there can only be 1 panel.',
usage: '', usage: '',
aliases: ['widget'], aliases: ['widget'],
example: '',
args: false, args: false,
permission: 'MANAGE_GUILD', permission: 'MANAGE_GUILD',
async execute(client, message, _args, {config, Setting}) { async execute(client, message, _args, {config, Setting}) {

View File

@ -13,7 +13,7 @@ module.exports = {
description: 'Rename a ticket channel', description: 'Rename a ticket channel',
usage: '<new name>', usage: '<new name>',
aliases: ['none'], aliases: ['none'],
example: '', example: 'rename important-ticket',
args: true, args: true,
async execute(client, message, args, {config, Ticket}) { async execute(client, message, args, {config, Ticket}) {
const guild = client.guilds.cache.get(config.guild); const guild = client.guilds.cache.get(config.guild);
@ -49,7 +49,7 @@ module.exports = {
.setFooter(guild.name, guild.iconURL()) .setFooter(guild.name, guild.iconURL())
); );
message.channel.setName(args.join(' ')); // new channel name message.channel.setName(args.join('-')); // new channel name
message.channel.send( message.channel.send(
new MessageEmbed() new MessageEmbed()

View File

@ -13,7 +13,7 @@ module.exports = {
description: 'View ticket stats.', description: 'View ticket stats.',
usage: '', usage: '',
aliases: ['data', 'statistics'], aliases: ['data', 'statistics'],
example: '',
args: false, args: false,
async execute(client, message, _args, {config, Ticket}) { async execute(client, message, _args, {config, Ticket}) {
const guild = client.guilds.cache.get(config.guild); const guild = client.guilds.cache.get(config.guild);

View File

@ -8,13 +8,13 @@
const { MessageEmbed } = require('discord.js'); const { MessageEmbed } = require('discord.js');
const fs = require('fs'); const fs = require('fs');
const { join } = require('path');
module.exports = { module.exports = {
name: 'tickets', name: 'tickets',
description: 'List your recent tickets to access transcripts / archives.', description: 'List your recent tickets to access transcripts / archives.',
usage: '[@member]', usage: '[@member]',
aliases: ['list'], aliases: ['list'],
example: '',
args: false, args: false,
async execute(client, message, args, {config, Ticket}) { async execute(client, message, args, {config, Ticket}) {
const guild = client.guilds.cache.get(config.guild); const guild = client.guilds.cache.get(config.guild);
@ -88,7 +88,7 @@ module.exports = {
let desc = closedTickets.rows[t].topic.substring(0, 30); let desc = closedTickets.rows[t].topic.substring(0, 30);
let transcript = ''; let transcript = '';
let c = closedTickets.rows[t].channel; let c = closedTickets.rows[t].channel;
if (fs.existsSync(`user/transcripts/text/${c}.txt`) || config.transcripts.web.enabled) { if (config.transcripts.web.enabled || fs.existsSync(join(__dirname, `../../user/transcripts/text/${c}.txt`))) {
transcript = `\n> Type \`${config.prefix}transcript ${closedTickets.rows[t].id}\` to view.`; transcript = `\n> Type \`${config.prefix}transcript ${closedTickets.rows[t].id}\` to view.`;
} }

View File

@ -7,6 +7,8 @@
*/ */
const fs = require('fs'); const fs = require('fs');
const { join } = require('path');
const { const {
MessageEmbed MessageEmbed
} = require('discord.js'); } = require('discord.js');
@ -59,11 +61,12 @@ module.exports = {
.setTitle(`Ticket ${id}`) .setTitle(`Ticket ${id}`)
.setFooter(guild.name, guild.iconURL()); .setFooter(guild.name, guild.iconURL());
if (fs.existsSync(`user/transcripts/text/${ticket.channel}.txt`)) { let file = `../../user/transcripts/text/${ticket.channel}.txt`;
if (fs.existsSync(join(__dirname, file))) {
embed.addField('Text transcript', 'See attachment'); embed.addField('Text transcript', 'See attachment');
res.files = [ res.files = [
{ {
attachment: `user/transcripts/text/${ticket.channel}.txt`, attachment: join(__dirname, file),
name: `ticket-${id}-${ticket.channel}.txt` name: `ticket-${id}-${ticket.channel}.txt`
} }
]; ];

View File

@ -9,6 +9,7 @@
const Logger = require('leekslazylogger'); const Logger = require('leekslazylogger');
const log = new Logger(); const log = new Logger();
const fs = require('fs'); const fs = require('fs');
const { join } = require('path');
module.exports = { module.exports = {
event: 'messageDelete', event: 'messageDelete',
@ -29,11 +30,11 @@ module.exports = {
if (!ticket) return; if (!ticket) return;
let path = `user/transcripts/raw/${message.channel.id}.log`; let path = `../../user/transcripts/raw/${message.channel.id}.log`;
let embeds = []; let embeds = [];
for (let embed in message.embeds) embeds.push(message.embeds[embed].toJSON()); for (let embed in message.embeds) embeds.push(message.embeds[embed].toJSON());
fs.appendFileSync(path, JSON.stringify({ fs.appendFileSync(join(__dirname, path), JSON.stringify({
id: message.id, id: message.id,
author: message.author.id, author: message.author.id,
content: message.content, // do not use cleanContent! content: message.content, // do not use cleanContent!

View File

@ -10,6 +10,7 @@ const Logger = require('leekslazylogger');
const log = new Logger(); const log = new Logger();
const { MessageEmbed } = require('discord.js'); const { MessageEmbed } = require('discord.js');
const fs = require('fs'); const fs = require('fs');
const { join } = require('path');
module.exports = { module.exports = {
event: 'messageReactionAdd', event: 'messageReactionAdd',
@ -148,11 +149,10 @@ module.exports = {
await c.send(ping + `${u} has created a new ticket`); await c.send(ping + `${u} has created a new ticket`);
if (config.tickets.send_img) { if (config.tickets.send_img) {
const images = fs.readdirSync('user/images'); const images = fs.readdirSync(join(__dirname, '../../user/images'));
await c.send({ await c.send({
files: [ files: [
'user/images/' + join(__dirname, '../../user/images', images[Math.floor(Math.random() * images.length)])
images[Math.floor(Math.random() * images.length)]
] ]
}); });
} }

View File

@ -9,6 +9,7 @@
const Logger = require('leekslazylogger'); const Logger = require('leekslazylogger');
const log = new Logger(); const log = new Logger();
const fs = require('fs'); const fs = require('fs');
const { join } = require('path');
module.exports = { module.exports = {
event: 'messageUpdate', event: 'messageUpdate',
@ -36,11 +37,11 @@ module.exports = {
let ticket = await Ticket.findOne({ where: { channel: n.channel.id } }); let ticket = await Ticket.findOne({ where: { channel: n.channel.id } });
if (!ticket) return; if (!ticket) return;
let path = `user/transcripts/raw/${n.channel.id}.log`; let path = `../../user/transcripts/raw/${n.channel.id}.log`;
let embeds = []; let embeds = [];
for (let embed in n.embeds) embeds.push({ ...n.embeds[embed] }); for (let embed in n.embeds) embeds.push({ ...n.embeds[embed] });
fs.appendFileSync(path, JSON.stringify({ fs.appendFileSync(join(__dirname, path), JSON.stringify({
id: n.id, id: n.id,
author: n.author.id, author: n.author.id,
content: n.content, // do not use cleanContent! content: n.content, // do not use cleanContent!

View File

@ -15,14 +15,14 @@ const version = Number(process.version.split('.')[0].replace('v', ''));
if (!version === 12 || !version > 12) return console.log('Please upgrade to Node v12 or higher'); if (!version === 12 || !version > 12) return console.log('Please upgrade to Node v12 or higher');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const { join } = require('path');
let dev = fs.existsSync('user/dev.env') && fs.existsSync('user/dev.config.js'); let dev = fs.existsSync(join(__dirname, '../user/dev.env')) && fs.existsSync(join(__dirname, '../user/dev.config.js'));
require('dotenv').config({ path: path.join('user/', dev ? 'dev.env' : '.env') }); require('dotenv').config({ path: join(__dirname, '../user/', dev ? 'dev.env' : '.env') });
module.exports.config = dev ? 'dev.config.js' : 'config.js'; module.exports.config = dev ? 'dev.config.js' : 'config.js';
const config = require(path.join('../user/', module.exports.config)); const config = require(join(__dirname, '../user/', module.exports.config));
const Discord = require('discord.js'); const Discord = require('discord.js');
const client = new Discord.Client({ const client = new Discord.Client({
@ -91,7 +91,7 @@ default:
log.info('Using SQLite storage'); log.info('Using SQLite storage');
sequelize = new Sequelize({ sequelize = new Sequelize({
dialect: 'sqlite', dialect: 'sqlite',
storage: 'user/storage.db', storage: join(__dirname, '../user/storage.db'),
logging: log.debug logging: log.debug
}); });
} }
@ -122,7 +122,7 @@ Setting.sync();
/** /**
* event loader * event loader
*/ */
const events = fs.readdirSync('src/events').filter(file => file.endsWith('.js')); const events = fs.readdirSync(join(__dirname, 'events')).filter(file => file.endsWith('.js'));
for (const file of events) { for (const file of events) {
const event = require(`./events/${file}`); const event = require(`./events/${file}`);
client.events.set(event.event, event); client.events.set(event.event, event);
@ -134,7 +134,7 @@ for (const file of events) {
/** /**
* command loader * command loader
*/ */
const commands = fs.readdirSync('src/commands').filter(file => file.endsWith('.js')); const commands = fs.readdirSync(join(__dirname, 'commands')).filter(file => file.endsWith('.js'));
for (const file of commands) { for (const file of commands) {
const command = require(`./commands/${file}`); const command = require(`./commands/${file}`);
client.commands.set(command.name, command); client.commands.set(command.name, command);
@ -144,14 +144,14 @@ for (const file of commands) {
log.info(`Loaded ${events.length} events and ${commands.length} commands`); log.info(`Loaded ${events.length} events and ${commands.length} commands`);
const one_day = 1000 * 60 * 60 * 24; const one_day = 1000 * 60 * 60 * 24;
const txt = 'user/transcripts/text'; const txt = '../user/transcripts/text';
const clean = () => { const clean = () => {
const files = fs.readdirSync(txt).filter(file => file.endsWith('.txt')); const files = fs.readdirSync(join(__dirname, txt)).filter(file => file.endsWith('.txt'));
let total = 0; let total = 0;
for (const file of files) { for (const file of files) {
let diff = (new Date() - new Date(fs.statSync(`${txt}/${file}`).mtime)); let diff = (new Date() - new Date(fs.statSync(join(__dirname, txt, file)).mtime));
if (Math.floor(diff / one_day) > config.transcripts.text.keep_for) { if (Math.floor(diff / one_day) > config.transcripts.text.keep_for) {
fs.unlinkSync(`${txt}/${file}`); fs.unlinkSync(join(__dirname, txt, file));
total++; total++;
} }
} }

View File

@ -11,6 +11,7 @@ const Logger = require('leekslazylogger');
const log = new Logger(); const log = new Logger();
const lineReader = require('line-reader'); const lineReader = require('line-reader');
const fs = require('fs'); const fs = require('fs');
const { join } = require('path');
const dtf = require('@eartharoid/dtf'); const dtf = require('@eartharoid/dtf');
const config = require('../../user/' + require('../').config); const config = require('../../user/' + require('../').config);
const fetch = require('node-fetch'); const fetch = require('node-fetch');
@ -20,23 +21,23 @@ module.exports.add = (message) => {
if (message.type !== 'DEFAULT') return; if (message.type !== 'DEFAULT') return;
if (config.transcripts.text.enabled) { // text transcripts if (config.transcripts.text.enabled) { // text transcripts
let path = `user/transcripts/text/${message.channel.id}.txt`, let path = `../../user/transcripts/text/${message.channel.id}.txt`,
time = dtf('HH:mm:ss n_D MMM YY', message.createdAt), time = dtf('HH:mm:ss n_D MMM YY', message.createdAt),
msg = message.cleanContent; msg = message.cleanContent;
message.attachments.each(a => msg += '\n' + a.url); message.attachments.each(a => msg += '\n' + a.url);
let string = `[${time}] [${message.author.tag}] :> ${msg}`; let string = `[${time}] [${message.author.tag}] :> ${msg}`;
fs.appendFileSync(path, string + '\n'); fs.appendFileSync(join(__dirname, path), string + '\n');
} }
if (config.transcripts.web.enabled) { // web archives if (config.transcripts.web.enabled) { // web archives
let raw = `user/transcripts/raw/${message.channel.id}.log`, let raw = `../../user/transcripts/raw/${message.channel.id}.log`,
json = `user/transcripts/raw/entities/${message.channel.id}.json`; json = `../../user/transcripts/raw/entities/${message.channel.id}.json`;
let embeds = []; let embeds = [];
for (let embed in message.embeds) embeds.push({ ...message.embeds[embed] }); for (let embed in message.embeds) embeds.push({ ...message.embeds[embed] });
// message // message
fs.appendFileSync(raw, JSON.stringify({ fs.appendFileSync(join(__dirname, raw), JSON.stringify({
id: message.id, id: message.id,
author: message.author.id, author: message.author.id,
content: message.content, // do not use cleanContent! content: message.content, // do not use cleanContent!
@ -46,8 +47,8 @@ module.exports.add = (message) => {
}) + '\n'); }) + '\n');
// channel entities // channel entities
if (!fs.existsSync(json)) if (!fs.existsSync(join(__dirname, json)))
fs.writeFileSync(json, JSON.stringify({ fs.writeFileSync(join(__dirname, json), JSON.stringify({
entities: { entities: {
users: {}, users: {},
channels: {}, channels: {},
@ -55,7 +56,7 @@ module.exports.add = (message) => {
} }
})); // create new })); // create new
let data = JSON.parse(fs.readFileSync(json)); let data = JSON.parse(fs.readFileSync(join(__dirname, json)));
// if (!data.entities.users[message.author.id]) // if (!data.entities.users[message.author.id])
data.entities.users[message.author.id] = { data.entities.users[message.author.id] = {
@ -86,7 +87,7 @@ module.exports.add = (message) => {
color: r.color === 0 ? 7506394 : r.color color: r.color === 0 ? 7506394 : r.color
}); });
fs.writeFileSync(json, JSON.stringify(data)); fs.writeFileSync(join(__dirname, json), JSON.stringify(data));
} }
}; };
@ -100,12 +101,12 @@ module.exports.export = (Ticket, channel) => new Promise((resolve, reject) => {
} }
}); });
let raw = `user/transcripts/raw/${channel.id}.log`, let raw = `../../user/transcripts/raw/${channel.id}.log`,
json = `user/transcripts/raw/entities/${channel.id}.json`; json = `../../user/transcripts/raw/entities/${channel.id}.json`;
if (!config.transcripts.web.enabled || !fs.existsSync(raw) || !fs.existsSync(json)) return reject(false); if (!config.transcripts.web.enabled || !fs.existsSync(join(__dirname, raw)) || !fs.existsSync(join(__dirname, json))) return reject(false);
let data = JSON.parse(fs.readFileSync(json)); let data = JSON.parse(fs.readFileSync(join(__dirname, json)));
data.ticket = { data.ticket = {
id: ticket.id, id: ticket.id,
@ -143,8 +144,8 @@ module.exports.export = (Ticket, channel) => new Promise((resolve, reject) => {
log.success(`Uploaded ticket #${ticket.id} archive to server`); log.success(`Uploaded ticket #${ticket.id} archive to server`);
fs.unlinkSync(raw); fs.unlinkSync(join(__dirname, raw));
fs.unlinkSync(json); fs.unlinkSync(join(__dirname, json));
resolve(res.url); resolve(res.url);
}).catch(e => { }).catch(e => {