mrmBot-Matrix/events/interactionCreate.js

74 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-03-22 20:43:26 +00:00
import database from "../utils/database.js";
import * as logger from "../utils/logger.js";
import { commands, messageCommands } from "../utils/collections.js";
2022-03-22 20:43:26 +00:00
import { clean } from "../utils/misc.js";
2022-07-18 22:05:01 +00:00
import { upload } from "../utils/tempimages.js";
2022-03-22 20:43:26 +00:00
// run when a slash command is executed
export default async (client, interaction) => {
if (interaction?.type !== 2) return;
2022-03-22 20:43:26 +00:00
// check if command exists and if it's enabled
const command = interaction.data.name;
let cmd = commands.get(command);
if (!cmd) {
cmd = messageCommands.get(command);
if (!cmd) return;
}
2022-03-22 20:43:26 +00:00
const invoker = interaction.member ?? interaction.user;
// actually run the command
logger.log("log", `${invoker.username} (${invoker.id}) ran application command ${command}`);
2022-03-22 20:43:26 +00:00
try {
await database.addCount(command);
// eslint-disable-next-line no-unused-vars
const commandClass = new cmd(client, { type: "application", interaction });
2022-03-22 20:43:26 +00:00
const result = await commandClass.run();
const replyMethod = interaction.acknowledged ? "editOriginalMessage" : "createMessage";
if (typeof result === "string") {
await interaction[replyMethod]({
content: result,
flags: commandClass.success ? 0 : 64
});
} else if (typeof result === "object" && result.embeds) {
await interaction[replyMethod](Object.assign(result, {
flags: result.flags ?? (commandClass.success ? 0 : 64)
}));
2022-03-22 20:43:26 +00:00
} else if (typeof result === "object" && result.file) {
const fileSize = 8388119;
2022-03-22 20:43:26 +00:00
if (result.file.length > fileSize) {
if (process.env.TEMPDIR && process.env.TEMPDIR !== "") {
await upload(client, result, interaction, true);
2022-03-22 20:43:26 +00:00
} else {
await interaction[replyMethod]({
content: "The resulting image was more than 8MB in size, so I can't upload it.",
flags: 64
});
2022-03-22 20:43:26 +00:00
}
} else {
await interaction[replyMethod](result.text ? result.text : {}, result);
2022-03-22 20:43:26 +00:00
}
}
} catch (error) {
const replyMethod = interaction.acknowledged ? "editOriginalMessage" : "createMessage";
2022-03-22 20:43:26 +00:00
if (error.toString().includes("Request entity too large")) {
await interaction[replyMethod]({ content: "The resulting file was too large to upload. Try again with a smaller image if possible.", flags: 64 });
2022-03-22 20:43:26 +00:00
} else if (error.toString().includes("Job ended prematurely")) {
await interaction[replyMethod]({ content: "Something happened to the image servers before I could receive the image. Try running your command again.", flags: 64 });
2022-03-22 20:43:26 +00:00
} else if (error.toString().includes("Timed out")) {
await interaction[replyMethod]({ content: "The request timed out before I could download that image. Try uploading your image somewhere else or reducing its size.", flags: 64 });
2022-03-22 20:43:26 +00:00
} else {
logger.error(`Error occurred with application command ${command} with arguments ${JSON.stringify(interaction.data.options)}: ${error.stack || error}`);
2022-03-22 20:43:26 +00:00
try {
2022-09-11 04:18:44 +00:00
let err = error;
if (error?.constructor?.name == "Promise") err = await error;
await interaction[replyMethod]("Uh oh! I ran into an error while running this command. Please report the content of the attached file at the following link or on the esmBot Support server: <https://github.com/esmBot/esmBot/issues>", {
2022-09-11 04:18:44 +00:00
file: `Message: ${clean(err)}\n\nStack Trace: ${clean(err.stack)}`,
2022-03-22 20:43:26 +00:00
name: "error.txt"
});
2022-03-22 20:43:26 +00:00
} catch { /* silently ignore */ }
}
}
};