initial update

This commit is contained in:
murm 2023-03-15 10:09:09 -04:00
parent 3272429cf6
commit db9b70bf66
280 changed files with 11772 additions and 11966 deletions

164
app.js
View file

@ -20,7 +20,13 @@ config({ path: resolve(dirname(fileURLToPath(import.meta.url)), ".env") });
import { reloadImageConnections } from "./utils/image.js"; import { reloadImageConnections } from "./utils/image.js";
// main services // main services
import { Client } from "oceanic.js"; // import { Client } from "oceanic.js";
import * as sdk from "matrix-js-sdk";
const AutojoinRoomsMixin = sdk.AutojoinRoomsMixin;
import pm2 from "pm2"; import pm2 from "pm2";
// some utils // some utils
import { promises, readFileSync } from "fs"; import { promises, readFileSync } from "fs";
@ -44,15 +50,15 @@ const { types } = JSON.parse(readFileSync(new URL("./config/commands.json", impo
const esmBotVersion = JSON.parse(readFileSync(new URL("./package.json", import.meta.url))).version; const esmBotVersion = JSON.parse(readFileSync(new URL("./package.json", import.meta.url))).version;
process.env.ESMBOT_VER = esmBotVersion; process.env.ESMBOT_VER = esmBotVersion;
const intents = [ // const intents = [
"GUILD_VOICE_STATES", // "GUILD_VOICE_STATES",
"DIRECT_MESSAGES", // "DIRECT_MESSAGES",
"GUILDS" // "GUILDS"
]; // ];
if (types.classic) { // if (types.classic) {
intents.push("GUILD_MESSAGES"); // intents.push("GUILD_MESSAGES");
intents.push("MESSAGE_CONTENT"); // intents.push("MESSAGE_CONTENT");
} // }
async function* getFiles(dir) { async function* getFiles(dir) {
const dirents = await promises.readdir(dir, { withFileTypes: true }); const dirents = await promises.readdir(dir, { withFileTypes: true });
@ -126,103 +132,59 @@ esmBot ${esmBotVersion} (${process.env.GIT_REV})
if (process.env.API_TYPE === "ws") await reloadImageConnections(); if (process.env.API_TYPE === "ws") await reloadImageConnections();
// create the oceanic client // create the oceanic client
const client = new Client({ // const client = new Client({
auth: `Bot ${process.env.TOKEN}`, // auth: `Bot ${process.env.TOKEN}`,
allowedMentions: { // allowedMentions: {
everyone: false, // everyone: false,
roles: false, // roles: false,
users: true, // users: true,
repliedUser: true // repliedUser: true
}, // },
gateway: { // gateway: {
concurrency: "auto", // concurrency: "auto",
maxShards: "auto", // maxShards: "auto",
shardIDs: process.env.SHARDS ? JSON.parse(process.env.SHARDS)[process.env.pm_id - 1] : null, // shardIDs: process.env.SHARDS ? JSON.parse(process.env.SHARDS)[process.env.pm_id - 1] : null,
presence: { // presence: {
status: "idle", // status: "idle",
activities: [{ // activities: [{
type: 0, // type: 0,
name: "Starting esmBot..." // name: "Starting esmBot..."
}] // }]
}, // },
intents // intents
}, // },
collectionLimits: { // collectionLimits: {
messages: 50 // messages: 50
} // }
// });
const myUserId = process.env.MATRIX_USERNAME;
const myAccessToken = process.env.TOKEN;
const matrixClient = sdk.createClient({
baseUrl: process.env.MATRIX_BASEURL,
accessToken: myAccessToken,
userId: myUserId,
}); });
// register events // register events
logger.log("info", "Attempting to load events..."); logger.log("info", "Attempting to load events...");
for await (const file of getFiles(resolve(dirname(fileURLToPath(import.meta.url)), "./events/"))) { // for await (const file of getFiles(resolve(dirname(fileURLToPath(import.meta.url)), "./events/"))) {
logger.log("main", `Loading event from ${file}...`); // logger.log("main", `Loading event from ${file}...`);
const eventArray = file.split("/"); // const eventArray = file.split("/");
const eventName = eventArray[eventArray.length - 1].split(".")[0]; // const eventName = eventArray[eventArray.length - 1].split(".")[0];
if (eventName === "interactionCreate" && !types.application) { // if (eventName === "interactionCreate" && !types.application) {
logger.log("warn", `Skipped loading event from ${file} because application commands are disabled`); // logger.log("warn", `Skipped loading event from ${file} because application commands are disabled`);
continue; // continue;
} // }
const { default: event } = await import(file); // const { default: event } = await import(file);
client.on(eventName, event.bind(null, client)); // // client.on(eventName, event.bind(null, client));
} // }
const { default: event } = await import("./events/roommessage.js");
matrixClient.on("Room.timeline", event.bind(null,matrixClient));
logger.log("info", "Finished loading events."); logger.log("info", "Finished loading events.");
// PM2-specific handling matrixClient.startClient({ initialSyncLimit: 0 }).then(() => logger.log("info", "Client started!"));
if (process.env.PM2_USAGE) {
// callback hell :)
pm2.launchBus((err, pm2Bus) => {
if (err) {
logger.error(err);
return;
}
pm2.list((err, list) => {
if (err) {
logger.error(err);
return;
}
const managerProc = list.filter((v) => v.name === "esmBot-manager")[0];
pm2Bus.on("process:msg", async (packet) => {
switch (packet.data?.type) {
case "reload":
var path = paths.get(packet.data.message);
await load(client, path, true);
break;
case "soundreload":
await reload(client);
break;
case "imagereload":
await reloadImageConnections();
break;
case "broadcastStart":
startBroadcast(client, packet.data.message);
break;
case "broadcastEnd":
endBroadcast(client);
break;
case "serverCounts":
pm2.sendDataToProcessId(managerProc.pm_id, {
id: managerProc.pm_id,
type: "process:msg",
data: {
type: "serverCounts",
guilds: client.guilds.size,
shards: client.shards.size
},
topic: true
}, (err) => {
if (err) logger.error(err);
});
break;
}
});
});
});
}
// connect to lavalink
if (!connected) connect(client);
client.connect();
} }
init(); init();

View file

@ -1,15 +1,15 @@
class Command { class Command {
success = true; success = true;
constructor(client, options) { constructor(matrixClient, options) {
this.client = client; this.client = matrixClient;
this.origOptions = options; this.origOptions = options;
this.type = options.type; this.type = options.type;
this.args = options.args; this.args = options.args;
if (options.type === "classic") { if (options.type === "classic") {
this.message = options.message; this.message = options.message;
this.channel = options.message.channel; this.channel = options.message.room_id;
this.guild = options.message.guild; this.guild = options.message.guild;
this.author = options.message.author; this.author = options.message.sender;
this.member = options.message.member; this.member = options.message.member;
this.content = options.content; this.content = options.content;
this.options = options.specialArgs; this.options = options.specialArgs;
@ -48,8 +48,8 @@ class Command {
async acknowledge() { async acknowledge() {
if (this.type === "classic") { if (this.type === "classic") {
const channel = this.channel ?? await this.client.rest.channels.get(this.message.channelID); const channel = this.channel;
await channel.sendTyping(); await this.client.sendTyping(channel, true, 5);
} else if (!this.interaction.acknowledged) { } else if (!this.interaction.acknowledged) {
await this.interaction.defer(); await this.interaction.defer();
} }

View file

@ -17,35 +17,35 @@ class ImageCommand extends Command {
const timestamp = this.type === "classic" ? this.message.createdAt : Math.floor((this.interaction.id / 4194304) + 1420070400000); const timestamp = this.type === "classic" ? this.message.createdAt : Math.floor((this.interaction.id / 4194304) + 1420070400000);
// check if this command has already been run in this channel with the same arguments, and we are awaiting its result // 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 so, don't re-run it
if (runningCommands.has(this.author.id) && (new Date(runningCommands.get(this.author.id)) - new Date(timestamp)) < 5000) { if (runningCommands.has(this.author) && (new Date(runningCommands.get(this.author)) - new Date(timestamp)) < 5000) {
return "Please slow down a bit."; return "Please slow down a bit.";
} }
// before awaiting the command result, add this command to the set of running commands // before awaiting the command result, add this command to the set of running commands
runningCommands.set(this.author.id, timestamp); runningCommands.set(this.author, timestamp);
const imageParams = { const imageParams = {
cmd: this.constructor.command, cmd: this.constructor.command,
params: { params: {
togif: !!this.options.togif togif: !!this.options.togif
}, },
id: (this.interaction ?? this.message).id id: this.message.event_id
}; };
if (this.type === "application") await this.acknowledge(); // if (this.type === "application") await this.acknowledge();
if (this.constructor.requiresImage) { if (this.constructor.requiresImage) {
try { try {
const selection = selectedImages.get(this.author.id); const selection = selectedImages.get(this.author);
const image = selection ?? await imageDetect(this.client, this.message, this.interaction, this.options, true); const image = await imageDetect(this.client, this.message, this.interaction, this.options, true);
if (selection) selectedImages.delete(this.author.id); if (selection) selectedImages.delete(this.author);
if (image === undefined) { if (image === undefined) {
runningCommands.delete(this.author.id); runningCommands.delete(this.author);
return `${this.constructor.noImage} (Tip: try right-clicking/holding on a message and press Apps -> Select Image, then try again.)`; return `${this.constructor.noImage} (Tip: try right-clicking/holding on a message and press Apps -> Select Image, then try again.)`;
} else if (image.type === "large") { } else if (image.type === "large") {
runningCommands.delete(this.author.id); runningCommands.delete(this.author);
return "That image is too large (>= 25MB)! Try using a smaller image."; return "That image is too large (>= 25MB)! Try using a smaller image.";
} else if (image.type === "tenorlimit") { } else if (image.type === "tenorlimit") {
runningCommands.delete(this.author.id); runningCommands.delete(this.author);
return "I've been rate-limited by Tenor. Please try uploading your GIF elsewhere."; return "I've been rate-limited by Tenor. Please try uploading your GIF elsewhere.";
} }
imageParams.path = image.path; imageParams.path = image.path;
@ -54,7 +54,7 @@ class ImageCommand extends Command {
imageParams.name = image.name; imageParams.name = image.name;
if (this.constructor.requiresGIF) imageParams.onlyGIF = true; if (this.constructor.requiresGIF) imageParams.onlyGIF = true;
} catch (e) { } catch (e) {
runningCommands.delete(this.author.id); runningCommands.delete(this.author);
throw e; throw e;
} }
} }
@ -62,7 +62,7 @@ class ImageCommand extends Command {
if (this.constructor.requiresText) { if (this.constructor.requiresText) {
const text = this.options.text ?? this.args.join(" ").trim(); const text = this.options.text ?? this.args.join(" ").trim();
if (text.length === 0 || !await this.criteria(text, imageParams.url)) { if (text.length === 0 || !await this.criteria(text, imageParams.url)) {
runningCommands.delete(this.author.id); runningCommands.delete(this.author);
return this.constructor.noText; return this.constructor.noText;
} }
} }
@ -75,7 +75,7 @@ class ImageCommand extends Command {
let status; let status;
if (imageParams.params.type === "image/gif" && this.type === "classic") { if (imageParams.params.type === "image/gif" && this.type === "classic") {
status = await this.processMessage(this.message.channel ?? await this.client.rest.channels.get(this.message.channelID)); status = await this.processMessage(this.message.room_id ?? await this.client.rest.channels.get(this.message.room_id));
} }
try { try {
@ -99,14 +99,21 @@ class ImageCommand extends Command {
} catch { } catch {
// no-op // no-op
} }
runningCommands.delete(this.author.id); runningCommands.delete(this.author);
} }
} }
processMessage(channel) { processMessage(channel) {
return channel.createMessage({ this.client.send
content: `${random(emotes) || process.env.PROCESSING_EMOJI || "<a:processing:479351417102925854>"} Processing... This might take a while` const content = {
body: "Processing... This might take a while",
msgtype: "m.text",
};
this.client.sendEvent(channel, "m.room.message", content, "", (err, res) => {
// console.log(res)
logger.log("error", err)
return res
}); });
} }

View file

@ -5,13 +5,13 @@ import { paths } from "../../utils/collections.js";
class ReloadCommand extends Command { class ReloadCommand extends Command {
async run() { async run() {
const owners = process.env.OWNER.split(","); const owners = process.env.OWNER.split(",");
if (!owners.includes(this.author.id)) return "Only the bot owner can reload commands!"; if (!owners.includes(this.sender)) return "Only the bot owner can reload commands!";
const commandName = this.options.cmd ?? this.args.join(" "); const commandName = this.options.cmd ?? this.args.join(" ");
if (!commandName || !commandName.trim()) return "You need to provide a command to reload!"; if (!commandName || !commandName.trim()) return "You need to provide a command to reload!";
await this.acknowledge(); await this.acknowledge();
const path = paths.get(commandName); const path = paths.get(commandName);
if (!path) return "I couldn't find that command!"; if (!path) return "I couldn't find that command!";
const result = await load(this.client, path, true); const result = await load(this.matrixClient, path, true);
if (result !== commandName) return "I couldn't reload that command!"; if (result !== commandName) return "I couldn't reload that command!";
if (process.env.PM2_USAGE) { if (process.env.PM2_USAGE) {
process.send({ process.send({

View file

@ -2,7 +2,7 @@ import { request } from "undici";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
const { searx } = JSON.parse(readFileSync(new URL("../../config/servers.json", import.meta.url))); const { searx } = JSON.parse(readFileSync(new URL("../../config/servers.json", import.meta.url)));
import { random } from "../../utils/misc.js"; import { random } from "../../utils/misc.js";
import paginator from "../../utils/pagination/pagination.js"; // import paginator from "../../utils/pagination/pagination.js";
import Command from "../../classes/command.js"; import Command from "../../classes/command.js";
class YouTubeCommand extends Command { class YouTubeCommand extends Command {
@ -14,11 +14,10 @@ class YouTubeCommand extends Command {
const messages = []; const messages = [];
const videos = await request(`${random(searx)}/search?format=json&safesearch=1&categories=videos&q=!youtube%20${encodeURIComponent(query)}`).then(res => res.body.json()); const videos = await request(`${random(searx)}/search?format=json&safesearch=1&categories=videos&q=!youtube%20${encodeURIComponent(query)}`).then(res => res.body.json());
if (videos.results.length === 0) return "I couldn't find any results!"; if (videos.results.length === 0) return "I couldn't find any results!";
for (const [i, value] of videos.results.entries()) { // console.log(videos.results[0])
messages.push({ content: `Page ${i + 1} of ${videos.results.length}\n<:youtube:637020823005167626> **${value.title.replaceAll("*", "\\*")}**\nUploaded by **${value.author.replaceAll("*", "\\*")}**\n${value.url}` });
}
this.success = true; this.success = true;
return paginator(this.client, { type: this.type, message: this.message, interaction: this.interaction, channel: this.channel, author: this.author }, messages); return videos.results[0].url
// return paginator(this.client, { type: this.type, message: this.message, interaction: this.interaction, channel: this.channel, author: this.author }, messages);
} }
static flags = [{ static flags = [{

View file

@ -1,7 +1,7 @@
{ {
"types": { "types": {
"classic": true, "classic": true,
"application": true "application": false
}, },
"blacklist": [] "blacklist": []
} }

View file

@ -1,6 +0,0 @@
import { log } from "../utils/logger.js";
// run when the bot is added to a guild
export default async (client, guild) => {
log(`[GUILD JOIN] ${guild.name} (${guild.id}) added the bot.`);
};

View file

@ -1,6 +0,0 @@
import { log } from "../utils/logger.js";
// run when the bot is removed from a guild
export default async (client, guild) => {
log(`[GUILD LEAVE] ${guild.name} (${guild.id}) removed the bot.`);
};

View file

@ -1,89 +0,0 @@
import database from "../utils/database.js";
import * as logger from "../utils/logger.js";
import { commands, messageCommands } from "../utils/collections.js";
import { clean } from "../utils/misc.js";
import { upload } from "../utils/tempimages.js";
// run when a slash command is executed
export default async (client, interaction) => {
if (interaction?.type !== 2) return;
// 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;
}
if (cmd.dbRequired && !database) {
await interaction["createMessage"]({ content: "This command is unavailable on stateless instances of esmBot.", flags: 64 });
return;
}
const invoker = interaction.member ?? interaction.user;
// actually run the command
logger.log("log", `${invoker.username} (${invoker.id}) ran application command ${command}`);
try {
if (database) {
await database.addCount(command);
}
// eslint-disable-next-line no-unused-vars
const commandClass = new cmd(client, { type: "application", interaction });
const result = await commandClass.run();
const replyMethod = interaction.acknowledged ? "editOriginal" : "createMessage";
if (typeof result === "string") {
await interaction[replyMethod]({
content: result,
flags: commandClass.success ? 0 : 64
});
} else if (typeof result === "object") {
if (result.contents && result.name) {
const fileSize = 8388119;
if (result.contents.length > fileSize) {
if (process.env.TEMPDIR && process.env.TEMPDIR !== "") {
await upload(client, result, interaction, true);
} else {
await interaction[replyMethod]({
content: "The resulting image was more than 8MB in size, so I can't upload it.",
flags: 64
});
}
} else {
await interaction[replyMethod](result.text ? result.text : { files: [result] });
}
} else {
await interaction[replyMethod](Object.assign({
flags: result.flags ?? (commandClass.success ? 0 : 64)
}, result));
}
} else {
logger.warn(`Unknown return type for command ${command}: ${result} (${typeof result})`);
await interaction[replyMethod](Object.assign({
flags: commandClass.success ? 0 : 64
}, result));
}
} catch (error) {
const replyMethod = interaction.acknowledged ? "editOriginal" : "createMessage";
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 });
} 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 });
} else {
logger.error(`Error occurred with application command ${command} with arguments ${JSON.stringify(interaction.data.optionsArray)}: ${error.stack || error}`);
try {
let err = error;
if (error?.constructor?.name == "Promise") err = await error;
await interaction[replyMethod]({
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: <https://github.com/esmBot/esmBot/issues>",
files: [{
contents: `Message: ${clean(err)}\n\nStack Trace: ${clean(err.stack)}`,
name: "error.txt"
}]
});
} catch (e) {
logger.error(`While attempting to send the previous error message, another error occurred: ${e.stack || e}`);
}
}
}
};

View file

@ -1,184 +0,0 @@
import database from "../utils/database.js";
import { log, error as _error } 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 { upload } from "../utils/tempimages.js";
import { ThreadChannel } from "oceanic.js";
let mentionRegex;
// run when someone sends a message
export default async (client, message) => {
// ignore other bots
if (message.author.bot) return;
// don't run command if bot can't send messages
let permChannel = message.channel;
if (permChannel instanceof ThreadChannel && !permChannel.parent) {
try {
permChannel = await client.rest.channels.get(message.channel.parentID);
} catch {
return;
}
}
if (message.guildID && !permChannel.permissionsOf(client.user.id.toString()).has("SEND_MESSAGES")) return;
if (!mentionRegex) mentionRegex = new RegExp(`^<@!?${client.user.id}> `);
let guildDB;
let text;
const mentionResult = message.content.match(mentionRegex);
if (mentionResult) {
text = message.content.substring(mentionResult[0].length).trim();
} else if (message.guildID && database) {
const cachedPrefix = prefixCache.get(message.guildID);
if (cachedPrefix && message.content.startsWith(cachedPrefix)) {
text = message.content.substring(cachedPrefix.length).trim();
} else {
guildDB = await database.getGuild(message.guildID);
if (message.content.startsWith(guildDB.prefix)) {
text = message.content.substring(guildDB.prefix.length).trim();
prefixCache.set(message.guildID, guildDB.prefix);
} else {
return;
}
}
} else if (message.content.startsWith(process.env.PREFIX)) {
text = message.content.substring(process.env.PREFIX.length).trim();
} else if (!message.guildID) {
text = message.content;
} else {
return;
}
// separate commands and args
const preArgs = text.split(/\s+/g);
const command = preArgs.shift().toLowerCase();
const aliased = aliases.get(command);
// check if command exists and if it's enabled
const cmd = commands.get(aliased ?? command);
if (!cmd) return;
// block certain commands from running in DMs
if (!cmd.directAllowed && !message.guildID) return;
if (cmd.dbRequired && !database) {
await client.rest.channels.createMessage(message.channelID, {
content: "This command is unavailable on stateless instances of esmBot."
});
return;
}
// don't run if message is in a disabled channel
if (message.guildID && database) {
let disabled = disabledCache.get(message.guildID);
if (!disabled) {
if (!guildDB) guildDB = await database.getGuild(message.guildID);
disabledCache.set(message.guildID, guildDB.disabled);
disabled = guildDB.disabled;
}
if (disabled.includes(message.channelID) && command != "channel") return;
let disabledCmds = disabledCmdCache.get(message.guildID);
if (!disabledCmds) {
if (!guildDB) guildDB = await database.getGuild(message.guildID);
disabledCmdCache.set(message.guildID, guildDB.disabled_commands ?? guildDB.disabledCommands);
disabledCmds = guildDB.disabled_commands ?? guildDB.disabledCommands;
}
if (disabledCmds.includes(aliased ?? command)) return;
}
// actually run the command
log("log", `${message.author.username} (${message.author.id}) ran classic command ${command}`);
const reference = {
messageReference: {
channelID: message.channelID,
messageID: message.id,
guildID: message.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(client, { type: "classic", message, 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") {
reference.allowedMentions.repliedUser = true;
await client.rest.channels.createMessage(message.channelID, Object.assign({
content: result
}, reference));
} else if (typeof result === "object") {
if (result.contents && result.name) {
let fileSize = 8388119;
if (message.guildID) {
switch (message.guild.premiumTier) {
case 2:
fileSize = 52428308;
break;
case 3:
fileSize = 104856616;
break;
}
}
if (result.contents.length > fileSize) {
if (process.env.TEMPDIR && process.env.TEMPDIR !== "") {
await upload(client, result, message);
} else {
await client.rest.channels.createMessage(message.channelID, {
content: "The resulting image was more than 8MB in size, so I can't upload it."
});
}
} else {
await client.rest.channels.createMessage(message.channelID, Object.assign({
content: result.text ? result.text : undefined,
files: [result]
}, reference));
}
} else {
await client.rest.channels.createMessage(message.channelID, Object.assign(result, reference));
}
}
} catch (error) {
if (error.toString().includes("Request entity too large")) {
await client.rest.channels.createMessage(message.channelID, 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(message.channelID, 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(message.channelID, 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 ${message.content}: ${error.stack || error}`);
try {
let err = error;
if (error?.constructor?.name == "Promise") err = await error;
await client.rest.channels.createMessage(message.channelID, 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: <https://github.com/esmBot/esmBot/issues>",
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}`);
}
}
}
};

135
events/roommessage.js Normal file
View file

@ -0,0 +1,135 @@
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)
// 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: <https://github.com/esmBot/esmBot/issues>",
// 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;
};

View file

@ -1,17 +0,0 @@
import { players, errHandle } from "../utils/soundplayer.js";
export default async (client, id) => {
for (const player of players.values()) {
if (id !== player.voiceChannel.guild.shard.id) return;
try {
await player.player.connection.connect({
guildId: player.voiceChannel.guildID,
channelId: player.voiceChannel.id,
shardId: player.voiceChannel.guild.shard.id,
deaf: true
});
} catch (e) {
errHandle(e, client, player.player, player.playingMessage, player.voiceChannel, { type: "classic" }, true);
}
}
};

View file

@ -1,115 +0,0 @@
import { players, queues, skipVotes } from "../utils/soundplayer.js";
import AwaitRejoin from "../utils/awaitrejoin.js";
import { random } from "../utils/misc.js";
import { logger } from "../utils/logger.js";
const isWaiting = new Map();
export default async (client, member, oldChannel) => {
if (!oldChannel) return;
const connection = players.get(oldChannel.guildID);
if (oldChannel.id === connection?.voiceChannel.id) {
if (oldChannel.voiceMembers.filter((i) => i.id !== client.user.id && !i.bot).length === 0) {
if (isWaiting.has(oldChannel.id)) return;
isWaiting.set(oldChannel.id, true);
connection.player.setPaused(true);
const waitMessage = await client.rest.channels.createMessage(connection.originalChannel.id, {
content: "🔊 Waiting 10 seconds for someone to return..."
});
const awaitRejoin = new AwaitRejoin(oldChannel, true, member.id);
awaitRejoin.once("end", async (rejoined, newMember, cancel) => {
isWaiting.delete(oldChannel.id);
if (rejoined) {
if (cancel) return;
connection.player.setPaused(false);
if (member.id !== newMember.id) {
players.set(connection.voiceChannel.guildID, { player: connection.player, type: connection.type, host: newMember.id, voiceChannel: connection.voiceChannel, originalChannel: connection.originalChannel, loop: connection.loop, shuffle: connection.shuffle, playMessage: connection.playMessage });
waitMessage.edit({
content: `🔊 ${newMember.mention} is the new voice channel host.`
});
} else {
try {
await waitMessage.delete();
} catch {
logger.warn(`Failed to delete wait message ${waitMessage.id}`);
}
}
} else {
try {
if (waitMessage.channel.messages.has(waitMessage.id)) await waitMessage.delete();
} catch {
logger.warn(`Failed to delete wait message ${waitMessage.id}`);
}
if (cancel) return;
try {
connection.player.node.leaveChannel(connection.originalChannel.guildID);
} catch {
logger.warn(`Failed to leave voice channel ${connection.originalChannel.guildID}`);
}
players.delete(connection.originalChannel.guildID);
queues.delete(connection.originalChannel.guildID);
skipVotes.delete(connection.originalChannel.guildID);
client.rest.channels.createMessage(connection.originalChannel.id, {
content: `🔊 The voice channel session in \`${connection.voiceChannel.name}\` has ended.`
});
}
});
} else if (member.id === connection.host) {
if (isWaiting.has(oldChannel.id)) return;
isWaiting.set(oldChannel.id, true);
const waitMessage = await client.rest.channels.createMessage(connection.originalChannel.id, {
content: "🔊 Waiting 10 seconds for the host to return..."
});
const awaitRejoin = new AwaitRejoin(oldChannel, false, member.id);
awaitRejoin.once("end", async (rejoined) => {
isWaiting.delete(oldChannel.id);
if (rejoined) {
try {
if (waitMessage.channel.messages.has(waitMessage.id)) await waitMessage.delete();
} catch {
logger.warn(`Failed to delete wait message ${waitMessage.id}`);
}
} else {
const members = oldChannel.voiceMembers.filter((i) => i.id !== client.user.id && !i.bot);
if (members.length === 0) {
try {
if (waitMessage.channel.messages.has(waitMessage.id)) await waitMessage.delete();
} catch {
logger.warn(`Failed to delete wait message ${waitMessage.id}`);
}
try {
connection.player.node.leaveChannel(connection.originalChannel.guildID);
} catch {
logger.warn(`Failed to leave voice channel ${connection.originalChannel.guildID}`);
}
players.delete(connection.originalChannel.guildID);
queues.delete(connection.originalChannel.guildID);
skipVotes.delete(connection.originalChannel.guildID);
client.rest.channels.createMessage(connection.originalChannel.id, {
content: `🔊 The voice channel session in \`${connection.voiceChannel.name}\` has ended.`
});
} else {
const randomMember = random(members);
players.set(connection.voiceChannel.guildID, { player: connection.player, type: connection.type, host: randomMember.id, voiceChannel: connection.voiceChannel, originalChannel: connection.originalChannel, loop: connection.loop, shuffle: connection.shuffle, playMessage: connection.playMessage });
waitMessage.edit({
content: `🔊 ${randomMember.mention} is the new voice channel host.`
});
}
}
});
} else if (member.id === client.user.id) {
isWaiting.delete(oldChannel.id);
try {
connection.player.node.leaveChannel(connection.originalChannel.guildID);
} catch {
logger.warn(`Failed to leave voice channel ${connection.originalChannel.guildID}`);
}
players.delete(connection.originalChannel.guildID);
queues.delete(connection.originalChannel.guildID);
skipVotes.delete(connection.originalChannel.guildID);
await client.rest.channels.createMessage(connection.originalChannel.id, {
content: `🔊 The voice channel session in \`${connection.voiceChannel.name}\` has ended.`
});
}
}
};

View file

@ -1,5 +0,0 @@
import leaveHandler from "./voiceChannelLeave.js";
export default async (client, member, newChannel, oldChannel) => {
await leaveHandler(client, member, oldChannel);
};

View file

@ -31,7 +31,9 @@
"emoji-regex": "^10.2.1", "emoji-regex": "^10.2.1",
"file-type": "^18.2.1", "file-type": "^18.2.1",
"format-duration": "^3.0.2", "format-duration": "^3.0.2",
"image-size": "^1.0.2",
"jsqr": "^1.4.0", "jsqr": "^1.4.0",
"matrix-js-sdk": "^23.4.0",
"node-addon-api": "^5.1.0", "node-addon-api": "^5.1.0",
"node-emoji": "^1.11.0", "node-emoji": "^1.11.0",
"oceanic.js": "1.5.1", "oceanic.js": "1.5.1",

View file

@ -15,7 +15,9 @@ specifiers:
eslint-plugin-unicorn: ^46.0.0 eslint-plugin-unicorn: ^46.0.0
file-type: ^18.2.1 file-type: ^18.2.1
format-duration: ^3.0.2 format-duration: ^3.0.2
image-size: ^1.0.2
jsqr: ^1.4.0 jsqr: ^1.4.0
matrix-js-sdk: ^23.4.0
node-addon-api: ^5.1.0 node-addon-api: ^5.1.0
node-emoji: ^1.11.0 node-emoji: ^1.11.0
oceanic.js: 1.5.1 oceanic.js: 1.5.1
@ -36,7 +38,9 @@ dependencies:
emoji-regex: 10.2.1 emoji-regex: 10.2.1
file-type: 18.2.1 file-type: 18.2.1
format-duration: 3.0.2 format-duration: 3.0.2
image-size: 1.0.2
jsqr: 1.4.0 jsqr: 1.4.0
matrix-js-sdk: 23.4.0
node-addon-api: 5.1.0 node-addon-api: 5.1.0
node-emoji: 1.11.0 node-emoji: 1.11.0
oceanic.js: 1.5.1_bufferutil@4.0.7 oceanic.js: 1.5.1_bufferutil@4.0.7
@ -340,6 +344,13 @@ packages:
- supports-color - supports-color
dev: true dev: true
/@babel/runtime/7.21.0:
resolution: {integrity: sha512-xwII0//EObnq89Ji5AKYQaRYiW/nZ3llSv29d49IuxPhKbtJoLP+9QUUZ4nVragQVtaVGeZrpB+ZtG/Pdy/POw==}
engines: {node: '>=6.9.0'}
dependencies:
regenerator-runtime: 0.13.11
dev: false
/@babel/template/7.20.7: /@babel/template/7.20.7:
resolution: {integrity: sha512-8SegXApWe6VoNw0r9JHpSteLKTpTiLZ4rMlGIm9JQ18KiCtyQiAMEazujAHrUS5flrcqYZa75ukev3P6QmUwUw==} resolution: {integrity: sha512-8SegXApWe6VoNw0r9JHpSteLKTpTiLZ4rMlGIm9JQ18KiCtyQiAMEazujAHrUS5flrcqYZa75ukev3P6QmUwUw==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
@ -499,6 +510,11 @@ packages:
'@jridgewell/sourcemap-codec': 1.4.14 '@jridgewell/sourcemap-codec': 1.4.14
dev: true dev: true
/@matrix-org/matrix-sdk-crypto-js/0.1.0-alpha.4:
resolution: {integrity: sha512-mdaDKrw3P5ZVCpq0ioW0pV6ihviDEbS8ZH36kpt9stLKHwwDSopPogE6CkQhi0B1jn1yBUtOYi32mBV/zcOR7g==}
engines: {node: '>= 10'}
dev: false
/@nicolo-ribaudo/eslint-scope-5-internals/5.1.1-v1: /@nicolo-ribaudo/eslint-scope-5-internals/5.1.1-v1:
resolution: {integrity: sha512-54/JRvkLIzzDWshCWfuhadfrfZVPiElY8Fcgmg1HroEly/EDSszzhBAsarCux+D/kOslTRquNzuyGSmUSTTHGg==} resolution: {integrity: sha512-54/JRvkLIzzDWshCWfuhadfrfZVPiElY8Fcgmg1HroEly/EDSszzhBAsarCux+D/kOslTRquNzuyGSmUSTTHGg==}
dependencies: dependencies:
@ -636,6 +652,10 @@ packages:
dev: false dev: false
optional: true optional: true
/@types/events/3.0.0:
resolution: {integrity: sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==}
dev: false
/@types/node/18.14.6: /@types/node/18.14.6:
resolution: {integrity: sha512-93+VvleD3mXwlLI/xASjw0FzKcwzl3OdTCzm1LaRfqgS21gfFtK3zDXM5Op9TeeMsJVOaJ2VRDpT9q4Y3d0AvA==} resolution: {integrity: sha512-93+VvleD3mXwlLI/xASjw0FzKcwzl3OdTCzm1LaRfqgS21gfFtK3zDXM5Op9TeeMsJVOaJ2VRDpT9q4Y3d0AvA==}
dev: false dev: false
@ -645,6 +665,10 @@ packages:
resolution: {integrity: sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==} resolution: {integrity: sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==}
dev: true dev: true
/@types/retry/0.12.0:
resolution: {integrity: sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==}
dev: false
/@types/triple-beam/1.3.2: /@types/triple-beam/1.3.2:
resolution: {integrity: sha512-txGIh+0eDFzKGC25zORnswy+br1Ha7hj5cMVwKIU7+s0U2AxxJru/jZSMU6OC9MJWP6+pc/hc6ZjyZShpsyY2g==} resolution: {integrity: sha512-txGIh+0eDFzKGC25zORnswy+br1Ha7hj5cMVwKIU7+s0U2AxxJru/jZSMU6OC9MJWP6+pc/hc6ZjyZShpsyY2g==}
dev: false dev: false
@ -706,6 +730,10 @@ packages:
dev: false dev: false
optional: true optional: true
/another-json/0.2.0:
resolution: {integrity: sha512-/Ndrl68UQLhnCdsAzEXLMFuOR546o2qbYRqCglaNHbjXrwG1ayTcdwr3zkSGOGtGXDyR5X9nCFfnyG2AFJIsqg==}
dev: false
/ansi-colors/4.1.3: /ansi-colors/4.1.3:
resolution: {integrity: sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==} resolution: {integrity: sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==}
engines: {node: '>=6'} engines: {node: '>=6'}
@ -815,6 +843,10 @@ packages:
/balanced-match/1.0.2: /balanced-match/1.0.2:
resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==} resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==}
/base-x/4.0.0:
resolution: {integrity: sha512-FuwxlW4H5kh37X/oW59pwTzzTKRzfrrQwhmyspRM7swOEZcHtDZSCt45U6oKgtuFE+WYPblePMVIPR4RZrh/hw==}
dev: false
/base64-js/1.5.1: /base64-js/1.5.1:
resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==} resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==}
dev: false dev: false
@ -886,6 +918,12 @@ packages:
update-browserslist-db: 1.0.10_browserslist@4.21.5 update-browserslist-db: 1.0.10_browserslist@4.21.5
dev: true dev: true
/bs58/5.0.0:
resolution: {integrity: sha512-r+ihvQJvahgYT50JD05dyJNKlmmSlMoOGwn1lCcEzanPglg7TxYjioQUYehQ9mAR/+hOSd2jRc/Z2y5UxBymvQ==}
dependencies:
base-x: 4.0.0
dev: false
/buffer-from/1.1.2: /buffer-from/1.1.2:
resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==} resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==}
dev: false dev: false
@ -1123,6 +1161,11 @@ packages:
resolution: {integrity: sha512-ty/fTekppD2fIwRvnZAVdeOiGd1c7YXEixbgJTNzqcxJWKQnjJ/V1bNEEE6hygpM3WjwHFUVK6HTjWSzV4a8sQ==} resolution: {integrity: sha512-ty/fTekppD2fIwRvnZAVdeOiGd1c7YXEixbgJTNzqcxJWKQnjJ/V1bNEEE6hygpM3WjwHFUVK6HTjWSzV4a8sQ==}
dev: true dev: true
/content-type/1.0.5:
resolution: {integrity: sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==}
engines: {node: '>= 0.6'}
dev: false
/continuation-local-storage/3.2.1: /continuation-local-storage/3.2.1:
resolution: {integrity: sha512-jx44cconVqkCEEyLSKWwkvUXwO561jXMa3LPjTPsm5QR22PA0/mhe33FT4Xb5y74JDvt/Cq+5lm8S8rskLv9ZA==} resolution: {integrity: sha512-jx44cconVqkCEEyLSKWwkvUXwO561jXMa3LPjTPsm5QR22PA0/mhe33FT4Xb5y74JDvt/Cq+5lm8S8rskLv9ZA==}
dependencies: dependencies:
@ -1525,6 +1568,11 @@ packages:
dev: false dev: false
optional: true optional: true
/events/3.3.0:
resolution: {integrity: sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==}
engines: {node: '>=0.8.x'}
dev: false
/expand-template/2.0.3: /expand-template/2.0.3:
resolution: {integrity: sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==} resolution: {integrity: sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==}
engines: {node: '>=6'} engines: {node: '>=6'}
@ -1888,6 +1936,14 @@ packages:
engines: {node: '>= 4'} engines: {node: '>= 4'}
dev: true dev: true
/image-size/1.0.2:
resolution: {integrity: sha512-xfOoWjceHntRb3qFCrh5ZFORYH8XCdYpASltMhZ/Q0KZiOwjdE/Yl2QCiWdwD+lygV5bMCvauzgu5PxBX/Yerg==}
engines: {node: '>=14.0.0'}
hasBin: true
dependencies:
queue: 6.0.2
dev: false
/import-fresh/3.3.0: /import-fresh/3.3.0:
resolution: {integrity: sha512-veYYhQa+D1QBKznvhUHxb8faxlrwUnxseDAbAp457E0wLNio2bOSKnjYDhMj+YiAq61xrMGhQk9iXVk5FzgQMw==} resolution: {integrity: sha512-veYYhQa+D1QBKznvhUHxb8faxlrwUnxseDAbAp457E0wLNio2bOSKnjYDhMj+YiAq61xrMGhQk9iXVk5FzgQMw==}
engines: {node: '>=6'} engines: {node: '>=6'}
@ -2151,6 +2207,11 @@ packages:
triple-beam: 1.3.0 triple-beam: 1.3.0
dev: false dev: false
/loglevel/1.8.1:
resolution: {integrity: sha512-tCRIJM51SHjAayKwC+QAg8hT8vg6z7GSgLJKGvzuPb1Wc+hLzqtuVLxp6/HzSPOozuK+8ErAhy7U/sVzw8Dgfg==}
engines: {node: '>= 0.6.0'}
dev: false
/lru-cache/5.1.1: /lru-cache/5.1.1:
resolution: {integrity: sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==} resolution: {integrity: sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==}
dependencies: dependencies:
@ -2162,6 +2223,35 @@ packages:
dependencies: dependencies:
yallist: 4.0.0 yallist: 4.0.0
/matrix-events-sdk/0.0.1:
resolution: {integrity: sha512-1QEOsXO+bhyCroIe2/A5OwaxHvBm7EsSQ46DEDn8RBIfQwN5HWBpFvyWWR4QY0KHPPnnJdI99wgRiAl7Ad5qaA==}
dev: false
/matrix-js-sdk/23.4.0:
resolution: {integrity: sha512-3gHT6IrDYBkFYzaZM052uZXv1WFGoN+q83rTvmTpMtxZYrwosLHb6Y/w/Lfl26y1N1gTDdyQ0Vd3NpSycKmpJA==}
engines: {node: '>=16.0.0'}
dependencies:
'@babel/runtime': 7.21.0
'@matrix-org/matrix-sdk-crypto-js': 0.1.0-alpha.4
another-json: 0.2.0
bs58: 5.0.0
content-type: 1.0.5
loglevel: 1.8.1
matrix-events-sdk: 0.0.1
matrix-widget-api: 1.2.0
p-retry: 4.6.2
sdp-transform: 2.14.1
unhomoglyph: 1.0.6
uuid: 9.0.0
dev: false
/matrix-widget-api/1.2.0:
resolution: {integrity: sha512-BkBTREtXjCUM3Kx4UBgDmKoz39w7AXfIjBIC/jISBdcJkg8upFUhpIy+zUrSCIrmfO2Ke8LOsSoFoQkOyhqGxQ==}
dependencies:
'@types/events': 3.0.0
events: 3.3.0
dev: false
/memory-stream/1.0.0: /memory-stream/1.0.0:
resolution: {integrity: sha512-Wm13VcsPIMdG96dzILfij09PvuS3APtcKNh7M28FsCA/w6+1mjR7hhPmfFNoilX9xU7wTdhsH5lJAm6XNzdtww==} resolution: {integrity: sha512-Wm13VcsPIMdG96dzILfij09PvuS3APtcKNh7M28FsCA/w6+1mjR7hhPmfFNoilX9xU7wTdhsH5lJAm6XNzdtww==}
dependencies: dependencies:
@ -2440,6 +2530,14 @@ packages:
p-limit: 3.1.0 p-limit: 3.1.0
dev: true dev: true
/p-retry/4.6.2:
resolution: {integrity: sha512-312Id396EbJdvRONlngUx0NydfrIQ5lsYu0znKVUzVvArzEIt08V1qhtyESbGVd1FGX7UKtiFp5uwKZdM8wIuQ==}
engines: {node: '>=8'}
dependencies:
'@types/retry': 0.12.0
retry: 0.13.1
dev: false
/p-try/2.2.0: /p-try/2.2.0:
resolution: {integrity: sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==} resolution: {integrity: sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==}
engines: {node: '>=6'} engines: {node: '>=6'}
@ -2757,6 +2855,12 @@ packages:
resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==} resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==}
dev: true dev: true
/queue/6.0.2:
resolution: {integrity: sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==}
dependencies:
inherits: 2.0.4
dev: false
/raw-body/2.5.2: /raw-body/2.5.2:
resolution: {integrity: sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==} resolution: {integrity: sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==}
engines: {node: '>= 0.8'} engines: {node: '>= 0.8'}
@ -2837,6 +2941,10 @@ packages:
dev: false dev: false
optional: true optional: true
/regenerator-runtime/0.13.11:
resolution: {integrity: sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==}
dev: false
/regexp-tree/0.1.24: /regexp-tree/0.1.24:
resolution: {integrity: sha512-s2aEVuLhvnVJW6s/iPgEGK6R+/xngd2jNQ+xy4bXNDKxZKJH6jpPHY6kVeVv1IeLCHgswRj+Kl3ELaDjG6V1iw==} resolution: {integrity: sha512-s2aEVuLhvnVJW6s/iPgEGK6R+/xngd2jNQ+xy4bXNDKxZKJH6jpPHY6kVeVv1IeLCHgswRj+Kl3ELaDjG6V1iw==}
hasBin: true hasBin: true
@ -2887,6 +2995,11 @@ packages:
path-parse: 1.0.7 path-parse: 1.0.7
supports-preserve-symlinks-flag: 1.0.0 supports-preserve-symlinks-flag: 1.0.0
/retry/0.13.1:
resolution: {integrity: sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==}
engines: {node: '>= 4'}
dev: false
/reusify/1.0.4: /reusify/1.0.4:
resolution: {integrity: sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==} resolution: {integrity: sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==}
engines: {iojs: '>=1.0.0', node: '>=0.10.0'} engines: {iojs: '>=1.0.0', node: '>=0.10.0'}
@ -2934,6 +3047,11 @@ packages:
dev: false dev: false
optional: true optional: true
/sdp-transform/2.14.1:
resolution: {integrity: sha512-RjZyX3nVwJyCuTo5tGPx+PZWkDMCg7oOLpSlhjDdZfwUoNqG1mM8nyj31IGHyaPWXhjbP7cdK3qZ2bmkJ1GzRw==}
hasBin: true
dev: false
/semver/5.7.1: /semver/5.7.1:
resolution: {integrity: sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==} resolution: {integrity: sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==}
hasBin: true hasBin: true
@ -3331,6 +3449,10 @@ packages:
busboy: 1.6.0 busboy: 1.6.0
dev: false dev: false
/unhomoglyph/1.0.6:
resolution: {integrity: sha512-7uvcWI3hWshSADBu4JpnyYbTVc7YlhF5GDW/oPD5AxIxl34k4wXR3WDkPnzLxkN32LiTCTKMQLtKVZiwki3zGg==}
dev: false
/universalify/0.1.2: /universalify/0.1.2:
resolution: {integrity: sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==} resolution: {integrity: sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==}
engines: {node: '>= 4.0.0'} engines: {node: '>= 4.0.0'}
@ -3384,7 +3506,6 @@ packages:
hasBin: true hasBin: true
requiresBuild: true requiresBuild: true
dev: false dev: false
optional: true
/validate-npm-package-license/3.0.4: /validate-npm-package-license/3.0.4:
resolution: {integrity: sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew==} resolution: {integrity: sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew==}

View file

@ -109,32 +109,41 @@ const getImage = async (image, image2, video, extraReturnTypes, gifv = false, ty
} }
}; };
const urlFromMxc = async (mxcUri) => {
const stripped = mxcUri.replace("mxc://", "")
return process.env.MATRIX_BASEURL+"/_matrix/media/r0/download/"+stripped
}
const checkImages = async (message, extraReturnTypes, video, sticker) => { const checkImages = async (message, extraReturnTypes, video, sticker) => {
let type; let type;
if (sticker && message.stickerItems) { // console.log(message)
type = message.stickerItems[0]; if (typeof message.content.info !== undefined) {
} else { if (message.content.msgtype == "m.image") {
// first check the embeds const url = await urlFromMxc(message.content.url)
if (message.embeds.length !== 0) { const fileNameNoExtension = message.content.body.slice(0, message.content.body.lastIndexOf("."));
// embeds can vary in types, we check for tenor gifs first type = {name: fileNameNoExtension, path: url, url: url, type: message.content.info.mimetype}
if (message.embeds[0].type === "gifv") {
type = await getImage(message.embeds[0].video.url, message.embeds[0].url, video, extraReturnTypes, true);
// then we check for other image types
} else if ((message.embeds[0].type === "video" || message.embeds[0].type === "image") && message.embeds[0].thumbnail) {
type = await getImage(message.embeds[0].thumbnail.proxyURL, message.embeds[0].thumbnail.url, video, extraReturnTypes);
// finally we check both possible image fields for "generic" embeds
} else if (message.embeds[0].type === "rich" || message.embeds[0].type === "article") {
if (message.embeds[0].thumbnail) {
type = await getImage(message.embeds[0].thumbnail.proxyURL, message.embeds[0].thumbnail.url, video, extraReturnTypes);
} else if (message.embeds[0].image) {
type = await getImage(message.embeds[0].image.proxyURL, message.embeds[0].image.url, video, extraReturnTypes);
}
}
// then check the attachments
} else if (message.attachments.size !== 0 && message.attachments.first().width) {
type = await getImage(message.attachments.first().proxyURL, message.attachments.first().url, video);
} }
} }
// // first check the embeds
// if (message.embeds.length !== 0) {
// // embeds can vary in types, we check for tenor gifs first
// if (message.embeds[0].type === "gifv") {
// type = await getImage(message.embeds[0].video.url, message.embeds[0].url, video, extraReturnTypes, true);
// // then we check for other image types
// } else if ((message.embeds[0].type === "video" || message.embeds[0].type === "image") && message.embeds[0].thumbnail) {
// type = await getImage(message.embeds[0].thumbnail.proxyURL, message.embeds[0].thumbnail.url, video, extraReturnTypes);
// // finally we check both possible image fields for "generic" embeds
// } else if (message.embeds[0].type === "rich" || message.embeds[0].type === "article") {
// if (message.embeds[0].thumbnail) {
// type = await getImage(message.embeds[0].thumbnail.proxyURL, message.embeds[0].thumbnail.url, video, extraReturnTypes);
// } else if (message.embeds[0].image) {
// type = await getImage(message.embeds[0].image.proxyURL, message.embeds[0].image.url, video, extraReturnTypes);
// }
// }
// // then check the attachments
// } else if (message.attachments.size !== 0 && message.attachments.first().width) {
// type = await getImage(message.attachments.first().proxyURL, message.attachments.first().url, video);
// }
// if the return value exists then return it // if the return value exists then return it
return type ?? false; return type ?? false;
}; };
@ -142,23 +151,15 @@ const checkImages = async (message, extraReturnTypes, video, sticker) => {
// this checks for the latest message containing an image and returns the url of the image // this checks for the latest message containing an image and returns the url of the image
export default async (client, cmdMessage, interaction, options, extraReturnTypes = false, video = false, sticker = false, singleMessage = false) => { export default async (client, cmdMessage, interaction, options, extraReturnTypes = false, video = false, sticker = false, singleMessage = false) => {
// we start by determining whether or not we're dealing with an interaction or a message // we start by determining whether or not we're dealing with an interaction or a message
if (interaction) {
// we can get a raw attachment or a URL in the interaction itself
if (options) {
if (options.image) {
const attachment = interaction.data.resolved.attachments.get(options.image);
const result = await getImage(attachment.proxyURL, attachment.url, video, attachment.contentType);
if (result !== false) return result;
} else if (options.link) {
const result = await getImage(options.link, options.link, video, extraReturnTypes, false, null, true);
if (result !== false) return result;
}
}
}
if (cmdMessage) { if (cmdMessage) {
// console.log(cmdMessage)
// let channel = await client.getRoom(cmdMessage.room_id);
// console.log(channel)
// check if the message is a reply to another message // check if the message is a reply to another message
if (cmdMessage.messageReference && !singleMessage) { // console.log(cmdMessage.content['m.relates_to'])
const replyMessage = await client.rest.channels.getMessage(cmdMessage.messageReference.channelID, cmdMessage.messageReference.messageID).catch(() => undefined); if (cmdMessage.content['m.relates_to'] !== undefined) {
const replyMessage = await client.fetchRoomEvent(cmdMessage.room_id, cmdMessage.content['m.relates_to']['m.in_reply_to'].event_id)
// console.log(replyMessage)
if (replyMessage) { if (replyMessage) {
const replyResult = await checkImages(replyMessage, extraReturnTypes, video, sticker); const replyResult = await checkImages(replyMessage, extraReturnTypes, video, sticker);
if (replyResult !== false) return replyResult; if (replyResult !== false) return replyResult;
@ -168,22 +169,22 @@ export default async (client, cmdMessage, interaction, options, extraReturnTypes
const result = await checkImages(cmdMessage, extraReturnTypes, video, sticker); const result = await checkImages(cmdMessage, extraReturnTypes, video, sticker);
if (result !== false) return result; if (result !== false) return result;
} }
if (!singleMessage) { // if (!singleMessage) {
// if there aren't any replies or interaction attachments then iterate over the last few messages in the channel // // if there aren't any replies or interaction attachments then iterate over the last few messages in the channel
try { // try {
const channel = (interaction ? interaction : cmdMessage).channel ?? await client.rest.channels.get((interaction ? interaction : cmdMessage).channelID); // const channel = (interaction ? interaction : cmdMessage).channel ?? await client.rest.channels.get((interaction ? interaction : cmdMessage).channelID);
const messages = await channel.getMessages(); // const messages = await channel.getMessages();
// iterate over each message // // iterate over each message
for (const message of messages) { // for (const message of messages) {
const result = await checkImages(message, extraReturnTypes, video, sticker); // const result = await checkImages(message, extraReturnTypes, video, sticker);
if (result === false) { // if (result === false) {
continue; // continue;
} else { // } else {
return result; // return result;
} // }
} // }
} catch { // } catch {
// no-op // // no-op
} // }
} // }
}; };

View file

@ -127,37 +127,38 @@ export function getServers(bot) {
// copied from eris // copied from eris
export function cleanMessage(message, content) { export function cleanMessage(message, content) {
let cleanContent = content && content.replace(/<a?(:\w+:)[0-9]+>/g, "$1") || ""; let cleanContent = content && content.replace(/<a?(:\w+:)[0-9]+>/g, "$1") || "";
// TODO: see if I need to fuck with this
const author = message.author ?? message.member ?? message.user; // const author = message.author ?? message.member ?? message.user;
let authorName = author.username; // let authorName = author.username;
if (message.member?.nick) { // if (message.member?.nick) {
authorName = message.member.nick; // authorName = message.member.nick;
} // }
cleanContent = cleanContent.replace(new RegExp(`<@!?${author.id}>`, "g"), `@${authorName}`); // cleanContent = cleanContent.replace(new RegExp(`<@!?${author.id}>`, "g"), `@${authorName}`);
if (message.mentions) { // if (message.mentions) {
for (const mention of message.mentions.members) { // for (const mention of message.mentions.members) {
if (mention.nick) { // if (mention.nick) {
cleanContent = cleanContent.replace(new RegExp(`<@!?${mention.id}>`, "g"), `@${mention.nick}`); // cleanContent = cleanContent.replace(new RegExp(`<@!?${mention.id}>`, "g"), `@${mention.nick}`);
} // }
cleanContent = cleanContent.replace(new RegExp(`<@!?${mention.id}>`, "g"), `@${mention.username}`); // cleanContent = cleanContent.replace(new RegExp(`<@!?${mention.id}>`, "g"), `@${mention.username}`);
} // }
if (message.guildID && message.mentions.roles) { // if (message.guildID && message.mentions.roles) {
for (const roleID of message.mentions.roles) { // for (const roleID of message.mentions.roles) {
const role = message.guild.roles.get(roleID); // const role = message.guild.roles.get(roleID);
const roleName = role ? role.name : "deleted-role"; // const roleName = role ? role.name : "deleted-role";
cleanContent = cleanContent.replace(new RegExp(`<@&${roleID}>`, "g"), `@${roleName}`); // cleanContent = cleanContent.replace(new RegExp(`<@&${roleID}>`, "g"), `@${roleName}`);
} // }
} // }
for (const id of message.mentions.channels) { // for (const id of message.mentions.channels) {
const channel = message.client.getChannel(id); // const channel = message.client.getChannel(id);
if (channel && channel.name && channel.mention) { // if (channel && channel.name && channel.mention) {
cleanContent = cleanContent.replace(channel.mention, `#${channel.name}`); // cleanContent = cleanContent.replace(channel.mention, `#${channel.name}`);
} // }
} // }
} // }
return textEncode(cleanContent); return textEncode(cleanContent);
} }