From 16876b14088e8485cf9ce3c31e1d43a1853725e2 Mon Sep 17 00:00:00 2001 From: "Isaac (eartharoid)" Date: Wed, 14 Oct 2020 22:37:57 +0100 Subject: [PATCH] v2.1.1 - Paths are no longer relative --- package-lock.json | 2 +- package.json | 2 +- src/commands/close.js | 22 +++++++++++++--------- src/commands/delete.js | 7 ++++--- src/commands/new.js | 6 +++--- src/commands/panel.js | 1 - src/commands/rename.js | 4 ++-- src/commands/stats.js | 2 +- src/commands/tickets.js | 4 ++-- src/commands/transcript.js | 7 +++++-- src/events/messageDelete.js | 5 +++-- src/events/messageReactionAdd.js | 6 +++--- src/events/messageUpdate.js | 5 +++-- src/index.js | 22 +++++++++++----------- src/modules/archive.js | 31 ++++++++++++++++--------------- 15 files changed, 68 insertions(+), 58 deletions(-) diff --git a/package-lock.json b/package-lock.json index e6e3b5b..4949b97 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@eartharoid/discordtickets", - "version": "2.1.0", + "version": "2.1.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index e7d10e5..ac2020c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@eartharoid/discordtickets", - "version": "2.1.0", + "version": "2.1.1", "private": true, "description": "An open-source & self-hosted Discord bot for ticket management.", "main": "src/index.js", diff --git a/src/commands/close.js b/src/commands/close.js index ddd0f7e..c2225ac 100644 --- a/src/commands/close.js +++ b/src/commands/close.js @@ -8,10 +8,9 @@ const Logger = require('leekslazylogger'); const log = new Logger(); -const { - MessageEmbed -} = require('discord.js'); +const { MessageEmbed } = require('discord.js'); const fs = require('fs'); +const { join } = require('path'); const archive = require('../modules/archive'); module.exports = { @@ -74,8 +73,8 @@ module.exports = { ); let success; - let pre = fs.existsSync(`user/transcripts/text/${channel.id}.txt`) || - fs.existsSync(`user/transcripts/raw/${channel.id}.log`) ? + let pre = fs.existsSync(join(__dirname, `../../user/transcripts/text/${channel.id}.txt`)) || + 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}\`` : ''; @@ -136,15 +135,15 @@ module.exports = { .setTitle(`Ticket ${ticket.id}`) .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'); 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` }]; } - 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)); } @@ -154,7 +153,12 @@ module.exports = { res.embed = embed; - dm.send(res).then(); + + try { + dm.send(res).then(); + } catch (e) { + message.channel.send(':x: Couldn\'t send DM'); + } } } diff --git a/src/commands/delete.js b/src/commands/delete.js index 0e1966e..792c0e0 100644 --- a/src/commands/delete.js +++ b/src/commands/delete.js @@ -12,6 +12,7 @@ const { MessageEmbed } = require('discord.js'); const fs = require('fs'); +const { join } = require('path'); module.exports = { name: 'delete', @@ -117,9 +118,9 @@ module.exports = { .setFooter(guild.name, guild.iconURL()) ); - let txt = `user/transcripts/text/${ticket.get('channel')}.txt`, - raw = `user/transcripts/raw/${ticket.get('channel')}.log`, - json = `user/transcripts/raw/entities/${ticket.get('channel')}.json`; + let txt = join(__dirname, `../../user/transcripts/text/${ticket.get('channel')}.txt`), + raw = join(__dirname, `../../user/transcripts/raw/${ticket.get('channel')}.log`), + json = join(__dirname, `../../user/transcripts/raw/entities/${ticket.get('channel')}.json`); if (fs.existsSync(txt)) fs.unlinkSync(txt); if (fs.existsSync(raw)) fs.unlinkSync(raw); diff --git a/src/commands/new.js b/src/commands/new.js index 544cd5c..74c93f3 100644 --- a/src/commands/new.js +++ b/src/commands/new.js @@ -10,6 +10,7 @@ const Logger = require('leekslazylogger'); const log = new Logger(); const { MessageEmbed } = require('discord.js'); const fs = require('fs'); +const { join } = require('path'); module.exports = { name: 'new', @@ -149,11 +150,10 @@ module.exports = { await c.send(ping + `${message.author} has created a new ticket`); if (config.tickets.send_img) { - const images = fs.readdirSync('user/images'); + const images = fs.readdirSync(join(__dirname, '../../user/images')); await c.send({ files: [ - 'user/images/' + - images[Math.floor(Math.random() * images.length)] + join(__dirname, '../../user/images', images[Math.floor(Math.random() * images.length)]) ] }); } diff --git a/src/commands/panel.js b/src/commands/panel.js index 622f6a7..f7ee9bb 100644 --- a/src/commands/panel.js +++ b/src/commands/panel.js @@ -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.', usage: '', aliases: ['widget'], - example: '', args: false, permission: 'MANAGE_GUILD', async execute(client, message, _args, {config, Setting}) { diff --git a/src/commands/rename.js b/src/commands/rename.js index a6160f6..062cdef 100644 --- a/src/commands/rename.js +++ b/src/commands/rename.js @@ -13,7 +13,7 @@ module.exports = { description: 'Rename a ticket channel', usage: '', aliases: ['none'], - example: '', + example: 'rename important-ticket', args: true, async execute(client, message, args, {config, Ticket}) { const guild = client.guilds.cache.get(config.guild); @@ -49,7 +49,7 @@ module.exports = { .setFooter(guild.name, guild.iconURL()) ); - message.channel.setName(args.join(' ')); // new channel name + message.channel.setName(args.join('-')); // new channel name message.channel.send( new MessageEmbed() diff --git a/src/commands/stats.js b/src/commands/stats.js index 7a3f53d..c8d7520 100644 --- a/src/commands/stats.js +++ b/src/commands/stats.js @@ -13,7 +13,7 @@ module.exports = { description: 'View ticket stats.', usage: '', aliases: ['data', 'statistics'], - example: '', + args: false, async execute(client, message, _args, {config, Ticket}) { const guild = client.guilds.cache.get(config.guild); diff --git a/src/commands/tickets.js b/src/commands/tickets.js index 9c5d5f5..32beb47 100644 --- a/src/commands/tickets.js +++ b/src/commands/tickets.js @@ -8,13 +8,13 @@ const { MessageEmbed } = require('discord.js'); const fs = require('fs'); +const { join } = require('path'); module.exports = { name: 'tickets', description: 'List your recent tickets to access transcripts / archives.', usage: '[@member]', aliases: ['list'], - example: '', args: false, async execute(client, message, args, {config, Ticket}) { const guild = client.guilds.cache.get(config.guild); @@ -88,7 +88,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`) || 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.`; } diff --git a/src/commands/transcript.js b/src/commands/transcript.js index 25b6ff3..deb2a62 100644 --- a/src/commands/transcript.js +++ b/src/commands/transcript.js @@ -7,6 +7,8 @@ */ const fs = require('fs'); +const { join } = require('path'); + const { MessageEmbed } = require('discord.js'); @@ -59,11 +61,12 @@ module.exports = { .setTitle(`Ticket ${id}`) .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'); res.files = [ { - attachment: `user/transcripts/text/${ticket.channel}.txt`, + attachment: join(__dirname, file), name: `ticket-${id}-${ticket.channel}.txt` } ]; diff --git a/src/events/messageDelete.js b/src/events/messageDelete.js index e0a0b02..24b0878 100644 --- a/src/events/messageDelete.js +++ b/src/events/messageDelete.js @@ -9,6 +9,7 @@ const Logger = require('leekslazylogger'); const log = new Logger(); const fs = require('fs'); +const { join } = require('path'); module.exports = { event: 'messageDelete', @@ -29,11 +30,11 @@ module.exports = { if (!ticket) return; - let path = `user/transcripts/raw/${message.channel.id}.log`; + let path = `../../user/transcripts/raw/${message.channel.id}.log`; let embeds = []; 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, author: message.author.id, content: message.content, // do not use cleanContent! diff --git a/src/events/messageReactionAdd.js b/src/events/messageReactionAdd.js index 3817f87..716221c 100644 --- a/src/events/messageReactionAdd.js +++ b/src/events/messageReactionAdd.js @@ -10,6 +10,7 @@ const Logger = require('leekslazylogger'); const log = new Logger(); const { MessageEmbed } = require('discord.js'); const fs = require('fs'); +const { join } = require('path'); module.exports = { event: 'messageReactionAdd', @@ -148,11 +149,10 @@ module.exports = { await c.send(ping + `${u} has created a new ticket`); if (config.tickets.send_img) { - const images = fs.readdirSync('user/images'); + const images = fs.readdirSync(join(__dirname, '../../user/images')); await c.send({ files: [ - 'user/images/' + - images[Math.floor(Math.random() * images.length)] + join(__dirname, '../../user/images', images[Math.floor(Math.random() * images.length)]) ] }); } diff --git a/src/events/messageUpdate.js b/src/events/messageUpdate.js index f8d6f04..4a54d5c 100644 --- a/src/events/messageUpdate.js +++ b/src/events/messageUpdate.js @@ -9,6 +9,7 @@ const Logger = require('leekslazylogger'); const log = new Logger(); const fs = require('fs'); +const { join } = require('path'); module.exports = { event: 'messageUpdate', @@ -36,11 +37,11 @@ module.exports = { let ticket = await Ticket.findOne({ where: { channel: n.channel.id } }); if (!ticket) return; - let path = `user/transcripts/raw/${n.channel.id}.log`; + let path = `../../user/transcripts/raw/${n.channel.id}.log`; let embeds = []; 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, author: n.author.id, content: n.content, // do not use cleanContent! diff --git a/src/index.js b/src/index.js index 4cd9777..2798721 100644 --- a/src/index.js +++ b/src/index.js @@ -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'); 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'; -const config = require(path.join('../user/', module.exports.config)); +const config = require(join(__dirname, '../user/', module.exports.config)); const Discord = require('discord.js'); const client = new Discord.Client({ @@ -91,7 +91,7 @@ default: log.info('Using SQLite storage'); sequelize = new Sequelize({ dialect: 'sqlite', - storage: 'user/storage.db', + storage: join(__dirname, '../user/storage.db'), logging: log.debug }); } @@ -122,7 +122,7 @@ Setting.sync(); /** * 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) { const event = require(`./events/${file}`); client.events.set(event.event, event); @@ -134,7 +134,7 @@ for (const file of events) { /** * 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) { const command = require(`./commands/${file}`); 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`); const one_day = 1000 * 60 * 60 * 24; -const txt = 'user/transcripts/text'; +const txt = '../user/transcripts/text'; 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; 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) { - fs.unlinkSync(`${txt}/${file}`); + fs.unlinkSync(join(__dirname, txt, file)); total++; } } diff --git a/src/modules/archive.js b/src/modules/archive.js index c50739c..4d01f5d 100644 --- a/src/modules/archive.js +++ b/src/modules/archive.js @@ -11,6 +11,7 @@ const Logger = require('leekslazylogger'); const log = new Logger(); const lineReader = require('line-reader'); const fs = require('fs'); +const { join } = require('path'); const dtf = require('@eartharoid/dtf'); const config = require('../../user/' + require('../').config); const fetch = require('node-fetch'); @@ -20,23 +21,23 @@ module.exports.add = (message) => { if (message.type !== 'DEFAULT') return; 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), msg = message.cleanContent; message.attachments.each(a => msg += '\n' + a.url); 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 - let raw = `user/transcripts/raw/${message.channel.id}.log`, - json = `user/transcripts/raw/entities/${message.channel.id}.json`; + let raw = `../../user/transcripts/raw/${message.channel.id}.log`, + json = `../../user/transcripts/raw/entities/${message.channel.id}.json`; let embeds = []; for (let embed in message.embeds) embeds.push({ ...message.embeds[embed] }); // message - fs.appendFileSync(raw, JSON.stringify({ + fs.appendFileSync(join(__dirname, raw), JSON.stringify({ id: message.id, author: message.author.id, content: message.content, // do not use cleanContent! @@ -46,8 +47,8 @@ module.exports.add = (message) => { }) + '\n'); // channel entities - if (!fs.existsSync(json)) - fs.writeFileSync(json, JSON.stringify({ + if (!fs.existsSync(join(__dirname, json))) + fs.writeFileSync(join(__dirname, json), JSON.stringify({ entities: { users: {}, channels: {}, @@ -55,7 +56,7 @@ module.exports.add = (message) => { } })); // 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]) data.entities.users[message.author.id] = { @@ -86,7 +87,7 @@ module.exports.add = (message) => { 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`, - json = `user/transcripts/raw/entities/${channel.id}.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); + 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 = { 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`); - fs.unlinkSync(raw); - fs.unlinkSync(json); + fs.unlinkSync(join(__dirname, raw)); + fs.unlinkSync(join(__dirname, json)); resolve(res.url); }).catch(e => {