import database from "../utils/database.js"; import { log, error as _error, logger } from "../utils/logger.js"; import { prefixCache, aliases, disabledCache, disabledCmdCache, commands } from "../utils/collections.js"; import parseCommand from "../utils/parseCommand.js"; import { clean } from "../utils/misc.js"; import sizeOf from "image-size"; // import { upload } from "../utils/tempimages.js"; // import { ThreadChannel } from "oceanic.js"; let mentionRegex; // run when someone sends a message export default async function (matrixClient, event, room, toStartOfTimeline) { // console.log(matrixClient) if (event.getType() == "m.room.message") { if (toStartOfTimeline) { return; // don't act on paginated results } if (event.event.sender == process.env.MATRIX_USERNAME) return; // console.log(event.event); let text; text = event.event.content.body; // if a reply, strip the reply from the formatting text = text.replace(/.*\n\n/g, "") if (text.startsWith(process.env.PREFIX)) { text = text.substring(process.env.PREFIX.length).trim(); } else { return; } // separate commands and args const preArgs = text.split(/\s+/g); const command = preArgs.shift().toLowerCase(); const aliased = aliases.get(command); const cmd = commands.get(aliased ?? command); if (!cmd) return; // command time :peachtime: log("log", `${event.sender.name} (${event.event.sender}) ran command ${command}`); const reference = { messageReference: { channelID: event.event.room_id, messageID: event.event.event_id, guildID: undefined, failIfNotExists: false }, allowedMentions: { repliedUser: false } }; try { // parse args const parsed = parseCommand(preArgs); // if (database) { // await database.addCount(aliases.get(command) ?? command); // } const startTime = new Date(); // eslint-disable-next-line no-unused-vars const commandClass = new cmd(matrixClient, { type: "classic", message: event.event, args: parsed._, content: text.replace(command, "").trim(), specialArgs: (({ _, ...o }) => o)(parsed) }); // we also provide the message content as a parameter for cases where we need more accuracy const result = await commandClass.run(); const endTime = new Date(); if ((endTime - startTime) >= 180000) reference.allowedMentions.repliedUser = true; if (typeof result === "string") { const content = { body: result, msgtype: "m.text", }; matrixClient.sendEvent(event.event.room_id, "m.room.message", content, "", (err, res) => { console.log(err); }); } else if (typeof result === "object") { // console.log(result) if (result.contents && result.name) { let fileSize = 52428308; if (result.contents.length > fileSize) { if (process.env.TEMPDIR && process.env.TEMPDIR !== "") { await upload(client, result, message); } else { const content = { body: "imag too big :(", msgtype: "m.text", }; matrixClient.sendEvent(event.event.room_id, "m.room.message", content, "", (err, res) => { console.log(err); }); } } else { const mxcUri = await matrixClient.uploadContent(result.contents); console.log(mxcUri.content_uri) // TODO: make info object get width, height, and mime from natives so i dont need to read the buffer const imgsize = sizeOf(result.contents) await matrixClient.sendImageMessage(event.event.room_id, mxcUri.content_uri, {h: imgsize.height, w: imgsize.width, mimetype: `image/${imgsize.type}`, size: result.contents.length, thumbnail_info: {h: imgsize.height, w: imgsize.width, mimetype: `image/${imgsize.type}`, size: result.contents.length}}, result.name) } } else { // await client.rest.channels.createMessage(message.channelID, Object.assign(result, reference)); } } else { console.log(typeof result) } } catch (error) { logger.log("error", error.stack) try { const errorcontent = { body: `Message: ${clean(err)}\n\nStack Trace: ${clean(err.stack)}`, msgtype: "m.text" } matrixClient.sendEvent(event.event.room_id, "m.room.message", errorcontent, "", (err, res) => { console.log(err); }); } catch (e) { _error(`While attempting to send the previous error message, another error occurred: ${e.stack || e}`); } // if (error.toString().includes("Request entity too large")) { // await client.rest.channels.createMessage(event.event.room_id, Object.assign({ // content: "The resulting file was too large to upload. Try again with a smaller image if possible." // }, reference)); // } else if (error.toString().includes("Job ended prematurely")) { // await client.rest.channels.createMessage(event.event.room_id, Object.assign({ // content: "Something happened to the image servers before I could receive the image. Try running your command again." // }, reference)); // } else if (error.toString().includes("Timed out")) { // await client.rest.channels.createMessage(event.event.room_id, Object.assign({ // content: "The request timed out before I could download that image. Try uploading your image somewhere else or reducing its size." // }, reference)); // } else { // _error(`Error occurred with command message ${event.event.content.body}: ${error.stack || error}`); // try { // let err = error; // if (error?.constructor?.name == "Promise") err = await error; // await client.rest.channels.createMessage(event.event.room_id, Object.assign({ // content: "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: ", // files: [{ // contents: `Message: ${clean(err)}\n\nStack Trace: ${clean(err.stack)}`, // name: "error.txt" // }] // }, reference)); // } catch (e) { // _error(`While attempting to send the previous error message, another error occurred: ${e.stack || e}`); // } // } } return; } return; };