mrmBot-Matrix/events/messageCreate.js

78 lines
3.8 KiB
JavaScript
Raw Normal View History

const fs = require("fs");
const { promisify } = require("util");
2019-09-13 20:02:41 +00:00
const client = require("../utils/client.js");
const database = require("../utils/database.js");
const misc = require("../utils/misc.js");
const logger = require("../utils/logger.js");
const collections = require("../utils/collections.js");
// run when someone sends a message
module.exports = async (message) => {
// ignore dms and other bots
if (message.author.bot) return;
if (!message.channel.guild) return;
// don't run command if bot can't send messages
2019-12-16 23:55:08 +00:00
if (!message.channel.guild.members.get(client.user.id).permission.has("sendMessages") || !message.channel.permissionsOf(client.user.id).has("sendMessages")) return;
2019-09-13 20:02:41 +00:00
// prefix can be a mention or a set of special characters
2020-05-31 20:01:17 +00:00
const guildDB = await database.guilds.findOne({ id: message.channel.guild.id }).lean().exec();
2019-09-13 20:02:41 +00:00
const prefixMention = new RegExp(`^<@!?${client.user.id}> `);
const prefix = prefixMention.test(message.content) ? message.content.match(prefixMention)[0] : guildDB.prefix;
2019-09-13 20:02:41 +00:00
// ignore other stuff
2020-03-14 23:22:09 +00:00
if (message.content.startsWith(prefix) === false) return;
2019-09-13 20:02:41 +00:00
// separate commands and args
2020-03-14 23:22:09 +00:00
const prefixRegex = new RegExp(`^(${misc.regexEscape(prefix)})`);
const content = message.content.replace(prefixRegex, "").trim();
const args = content.split(/ +/g);
2019-09-13 20:02:41 +00:00
const command = args.shift().toLowerCase();
// don't run if message is in a disabled channel
2020-04-26 22:17:32 +00:00
if (guildDB.disabledChannels && guildDB.disabledChannels.includes(message.channel.id) && command != "channel") return;
2019-09-13 20:02:41 +00:00
// check if command exists
const cmd = collections.commands.get(command) || collections.commands.get(collections.aliases.get(command));
if (!cmd) return;
// actually run the command
logger.log("info", `${message.author.username} (${message.author.id}) ran command ${command}`);
try {
const global = (await database.global.findOne({}).exec());
2020-05-31 20:01:17 +00:00
global.cmdCounts.set(collections.aliases.has(command) ? collections.aliases.get(command) : command, ((isNaN(global.cmdCounts.get(command)) ? 0 : parseFloat(global.cmdCounts.get(command))) + 1).toFixed());
await global.save();
const result = await cmd(message, args, content.replace(command, "").trim()); // we also provide the message content as a parameter for cases where we need more accuracy
if (typeof result === "string" || (typeof result === "object" && result.embed)) {
2019-09-13 20:02:41 +00:00
await client.createMessage(message.channel.id, result);
} else if (typeof result === "object" && result.file) {
if (result.file.length > 8388119 && process.env.TEMPDIR !== "") {
const filename = `${Math.random().toString(36).substring(2, 15)}.${result.name.split(".")[1]}`;
await promisify(fs.writeFile)(`${process.env.TEMPDIR}/${filename}`, result.file);
await client.createMessage(message.channel.id, {
embed: {
color: 16711680,
title: "What's this?",
url: "https://projectlounge.pw/esmBot#faq-large",
image: {
url: `https://projectlounge.pw/tmp/${filename}`
}
}
});
} else {
await client.createMessage(message.channel.id, result.text ? result.text : "", result);
}
2019-09-13 20:02:41 +00:00
}
} catch (error) {
if (!error.toString().includes("Request entity too large")) {
logger.error(error.toString());
await client.createMessage(message.channel.id, "Uh oh! I ran into an error while running this command. Please report the content of the attached file here or on the esmBot Support server: <https://github.com/TheEssem/esmBot/issues>", [{
file: Buffer.from(`Message: ${error}\n\nStack Trace: ${error.stack}`),
name: "error.txt"
}]);
} else {
await client.createMessage(message.channel.id, `${message.author.mention}, the resulting file was too large to upload. Try again with a smaller image if possible.`);
}
2019-09-13 20:02:41 +00:00
}
};