mrmBot-Matrix/classes/imageCommand.js
Essem 40223ec8b5
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively

* Sort commands

* Missed a couple of spots

* missed even more spots apparently

* Ported commands in "fun" category to new class-based format, added babel eslint plugin

* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of

* Missed a spot

* Removed unnecessary abort-controller package, add deprecation warning for mongo database

* Added imagereload, clarified premature end message

* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts

* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner

* Converted music/soundboard commands to class format

* Cleanup unnecessary logs

* awful tag command class port

* I literally somehow just learned that you can leave out the constructor in classes

* Pass client directly to commands/events, cleaned up command handler

* Migrated bot to eris-sharder, fixed some error handling stuff

* Remove unused modules

* Fixed type returning

* Switched back to Eris stable

* Some fixes and cleanup

* might wanna correct this

* Implement image command ratelimiting

* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 11:16:12 -05:00

125 lines
No EOL
4.3 KiB
JavaScript

const Command = require("./command.js");
const magick = require("../utils/image.js");
const imageDetect = require("../utils/imagedetect.js");
const collections = require("../utils/collections.js");
class ImageCommand extends Command {
/*this.embed = {
"title": "Your image is being generated! (PRELIMINARY EMBED)",
"description": "The current progress is outlined below:",
"color": 16711680,
"footer": {
"text": "Step 2/3"
},
"author": {
"name": "Processing...",
"icon_url": "https://cdn.discordapp.com/avatars/429305856241172480/a20f739886ae47cfb10fa069416e8ed3.jpg"
},
"fields": [
{
"name": "Downloading...",
"value": "✅ Done!"
},
{
"name": "Processing...",
"value": "<a:processing:818243325891051581> In progress"
},
{
"name": "Uploading...",
"value": "<a:processing:818243325891051581> Waiting for previous steps to complete"
}
]
};*/
criteria() {
return true;
}
async run() {
// check if this command has already been run in this channel with the same arguments, and we are awaiting its result
// if so, don't re-run it
if (collections.runningCommands.has(this.message.author.id) && (new Date(collections.runningCommands.get(this.message.author.id)) - new Date(this.message.createdAt)) < 5000) {
return `${this.message.author.mention}, please slow down a bit.`;
}
// before awaiting the command result, add this command to the set of running commands
collections.runningCommands.set(this.message.author.id, this.message.createdAt);
const magickParams = {
cmd: this.constructor.command
};
if (this.constructor.requiresImage) {
try {
const image = await imageDetect(this.client, this.message);
if (image === undefined) {
collections.runningCommands.delete(this.message.author.id);
return `${this.message.author.mention}, ${this.constructor.noImage}`;
}
magickParams.path = image.path;
magickParams.type = image.type;
magickParams.url = image.url; // technically not required but can be useful for text filtering
magickParams.delay = image.delay;
if (this.constructor.requiresGIF) magickParams.onlyGIF = true;
} catch (e) {
collections.runningCommands.delete(this.message.author.id);
throw e;
}
}
if (this.constructor.requiresText) {
if (this.args.length === 0 || !this.criteria(this.args)) {
collections.runningCommands.delete(this.message.author.id);
return `${this.message.author.mention}, ${this.constructor.noText}`;
}
}
switch (typeof this.params) {
case "function":
Object.assign(magickParams, this.params(this.args, magickParams.url));
break;
case "object":
Object.assign(magickParams, this.params);
break;
}
let status;
if (magickParams.type === "image/gif") {
status = await this.processMessage(this.message);
} else {
this.message.channel.sendTyping();
}
try {
const { buffer, type } = await magick.run(magickParams).catch(e => {
throw e;
});
if (status && status.channel.messages.get(status.id)) await status.delete();
if (type === "nogif" && this.constructor.requiresGIF) return `${this.message.author.mention}, that isn't a GIF!`;
return {
file: buffer,
name: `${this.constructor.command}.${type}`
};
} catch (e) {
if (status && status.channel.messages.get(status.id)) await status.delete();
if (e.toString().includes("Not connected to image server")) return `${this.message.author.mention}, I'm still trying to connect to the image servers. Please wait a little bit.`;
throw e;
} finally {
collections.runningCommands.delete(this.message.author.id);
}
}
processMessage(message) {
return message.channel.createMessage(`${process.env.PROCESSING_EMOJI || "<a:processing:479351417102925854>"} Processing... This might take a while`);
}
static requiresImage = true;
static requiresText = false;
static requiresGIF = false;
static noImage = "you need to provide an image!";
static noText = "you need to provide some text!";
static command = "";
}
module.exports = ImageCommand;