From 5b31d5f88c877d10ca8151796e47bc69f85b093d Mon Sep 17 00:00:00 2001 From: Matthew R Date: Mon, 13 Jul 2020 00:01:24 -0400 Subject: [PATCH] add store messages command for archival purposes --- src/commands/index.ts | 1 + src/commands/storemessages.ts | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/commands/storemessages.ts diff --git a/src/commands/index.ts b/src/commands/index.ts index f31b84d..614abd0 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -19,6 +19,7 @@ export { default as page } from './page'; export { default as ping } from './ping'; export { default as rank } from './rank'; export { default as roleinfo } from './roleinfo'; +export { default as storemessages } from './storemessages'; export { default as unban } from './unban'; export { default as unmute } from './unmute'; export { default as whois } from './whois'; diff --git a/src/commands/storemessages.ts b/src/commands/storemessages.ts new file mode 100644 index 0000000..ef93c6b --- /dev/null +++ b/src/commands/storemessages.ts @@ -0,0 +1,52 @@ +import { randomBytes } from 'crypto'; +import { Message, TextChannel } from 'eris'; +import { Client, Command, LocalStorage } from '../class'; + +export default class StoreMessages extends Command { + constructor(client: Client) { + super(client); + this.name = 'storemessages'; + this.description = 'Fetches 1000 messages from the specified channel and stores them in a HTML file.'; + this.usage = `${this.client.config.prefix}storemessages [member ID]`; + this.aliases = ['sm']; + this.permissions = 3; + this.guildOnly = true; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (!args[0]) return this.client.commands.get('help').run(message, [this.name]); + const check = this.client.util.resolveGuildChannel(args[0], this.mainGuild, false); + if (!check || check.type !== 0) return this.error(message.channel, 'The channel you specified either doesn\'t exist or isn\'t a textable guild channel.'); + const chan = this.mainGuild.channels.get(check.id); + const loadingMessage = await this.loading(message.channel, 'Fetching messages...'); + let messages = await chan.getMessages(10000); + if (args[1]) { + messages = messages.filter((m) => m.author.id === args[1]); + } + let html = `

Library of Code sp-us

Channel: ${chan.name} (${chan.id})
Generated by: ${message.author.username}#${message.author.discriminator}
Generated at: ${new Date().toLocaleString('en-us')}

`; + for (const msg of messages) { + html += `(${new Date(msg.timestamp).toLocaleString('en-us')}) [${msg.author.username}#${msg.author.discriminator} - ${msg.author.id}]: ${msg.cleanContent}
`; + } + message.delete(); + const identifier = randomBytes(10).toString('hex'); + + const comp = await LocalStorage.compress(html); + const file = new this.client.db.File({ + name: `${chan.name}-${new Date().toLocaleString('en-us')}.html.gz`, + identifier, + mimeType: 'application/gzip', + data: comp, + downloaded: 0, + maxDownloads: 1, + }); + await file.save(); + loadingMessage.delete(); + this.client.getDMChannel(message.author.id).then((c) => c.createMessage(`https://loc.sh/m/${identifier}.html.gz`)).catch(() => this.error(message.channel, 'Could not send a DM to you.')); + return this.success(message.channel, `Fetched messages for <#${chan.id}>. Check your DMs for link to access.`); + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +}