From e67c0d96ca4b771d58d1de83e8d3cd633f2375b3 Mon Sep 17 00:00:00 2001 From: murm Date: Thu, 16 Mar 2023 20:23:01 -0400 Subject: [PATCH] stuff and things --- .gitignore | 3 +- classes/imageCommand.js | 2 +- commands/fun/ancient.js | 2 +- commands/fun/bird.js | 2 +- commands/fun/cat.js | 2 +- commands/fun/dog.js | 2 +- commands/fun/wikihow.js | 2 +- commands/general/avatar.js | 53 ----------------------------- commands/general/banner.js | 55 ------------------------------- commands/general/donate.js | 2 +- commands/general/eval.js | 2 +- commands/general/exec.js | 2 +- commands/general/image.js | 2 +- commands/general/imagereload.js | 2 +- commands/general/imagestats.js | 21 +++--------- commands/general/info.js | 2 +- commands/general/lengthen.js | 2 +- commands/general/qrcreate.js | 2 +- commands/general/qrread.js | 2 +- commands/general/raw.js | 2 +- commands/general/reload.js | 2 +- commands/general/soundreload.js | 2 +- commands/general/youtube.js | 2 +- commands/message/select-image.js | 2 +- commands/music/music.js | 2 +- commands/soundboard/soundboard.js | 2 +- events/roommessage.js | 8 +++-- utils/imagedetect.js | 20 +++++++++++ 28 files changed, 54 insertions(+), 150 deletions(-) delete mode 100644 commands/general/avatar.js delete mode 100644 commands/general/banner.js diff --git a/.gitignore b/.gitignore index d79dcf6..456c91d 100644 --- a/.gitignore +++ b/.gitignore @@ -118,4 +118,5 @@ libvips/ # Databases data/ -*.sqlite \ No newline at end of file +*.sqlite +*.sqlite-journal \ No newline at end of file diff --git a/classes/imageCommand.js b/classes/imageCommand.js index 163af8e..9129f7f 100644 --- a/classes/imageCommand.js +++ b/classes/imageCommand.js @@ -31,7 +31,7 @@ class ImageCommand extends Command { id: this.message.event_id }; - // if (this.type === "application") await this.acknowledge(); + // if (this.type === "application") // await this.acknowledge(); if (this.constructor.requiresImage) { try { diff --git a/commands/fun/ancient.js b/commands/fun/ancient.js index c32a814..775c549 100644 --- a/commands/fun/ancient.js +++ b/commands/fun/ancient.js @@ -3,7 +3,7 @@ import Command from "../../classes/command.js"; class AncientCommand extends Command { async run() { - await this.acknowledge(); + // await this.acknowledge(); const controller = new AbortController(); const timeout = setTimeout(() => { controller.abort(); diff --git a/commands/fun/bird.js b/commands/fun/bird.js index 491aa18..4ed8e65 100644 --- a/commands/fun/bird.js +++ b/commands/fun/bird.js @@ -3,7 +3,7 @@ import Command from "../../classes/command.js"; class BirdCommand extends Command { async run() { - await this.acknowledge(); + // await this.acknowledge(); const controller = new AbortController(); const timeout = setTimeout(() => { controller.abort(); diff --git a/commands/fun/cat.js b/commands/fun/cat.js index 8977341..bc48e50 100644 --- a/commands/fun/cat.js +++ b/commands/fun/cat.js @@ -3,7 +3,7 @@ import Command from "../../classes/command.js"; class CatCommand extends Command { async run() { - await this.acknowledge(); + // await this.acknowledge(); const controller = new AbortController(); const timeout = setTimeout(() => { controller.abort(); diff --git a/commands/fun/dog.js b/commands/fun/dog.js index 83ee96c..af6eeed 100644 --- a/commands/fun/dog.js +++ b/commands/fun/dog.js @@ -3,7 +3,7 @@ import Command from "../../classes/command.js"; class DogCommand extends Command { async run() { - await this.acknowledge(); + // await this.acknowledge(); const controller = new AbortController(); const timeout = setTimeout(() => { controller.abort(); diff --git a/commands/fun/wikihow.js b/commands/fun/wikihow.js index 6581a2f..36b881a 100644 --- a/commands/fun/wikihow.js +++ b/commands/fun/wikihow.js @@ -3,7 +3,7 @@ import Command from "../../classes/command.js"; class WikihowCommand extends Command { async run() { - await this.acknowledge(); + // await this.acknowledge(); const controller = new AbortController(); const timeout = setTimeout(() => { controller.abort(); diff --git a/commands/general/avatar.js b/commands/general/avatar.js deleted file mode 100644 index 8e7ea42..0000000 --- a/commands/general/avatar.js +++ /dev/null @@ -1,53 +0,0 @@ -import Command from "../../classes/command.js"; -const mentionRegex = /^?$/; - -class AvatarCommand extends Command { - async run() { - const member = this.options.member ?? this.args[0]; - const self = this.client.users.get(this.author) ?? await this.client.rest.users.get(this.author); - if (this.type === "classic" && this.message.mentions.users[0]) { - return this.message.mentions.users[0].avatarURL(null, 512); - } else if (member && member > 21154535154122752n) { - const user = this.client.users.get(member) ?? await this.client.rest.users.get(member); - if (user) { - return user.avatarURL(null, 512); - } else if (mentionRegex.test(member)) { - const id = member.match(mentionRegex)[1]; - if (id < 21154535154122752n) { - this.success = false; - return "That's not a valid mention!"; - } - try { - const user = this.client.users.get(id) ?? await this.client.rest.users.get(id); - return user.avatarURL(null, 512); - } catch { - return self.avatarURL(null, 512); - } - } else { - return self.avatarURL(null, 512); - } - } else if (this.args.join(" ") !== "" && this.guild) { - const searched = await this.guild.searchMembers({ - query: this.args.join(" "), - limit: 1 - }); - if (searched.length === 0) return self.avatarURL(null, 512); - const user = this.client.users.get(searched[0].user.id) ?? await this.client.rest.users.get(searched[0].user.id); - return user ? user.avatarURL(null, 512) : self.avatarURL(null, 512); - } else { - return self.avatarURL(null, 512); - } - } - - static description = "Gets a user's avatar"; - static aliases = ["pfp", "ava"]; - static arguments = ["{mention/id}"]; - static flags = [{ - name: "member", - type: 6, - description: "The member to get the avatar from", - required: false - }]; -} - -export default AvatarCommand; diff --git a/commands/general/banner.js b/commands/general/banner.js deleted file mode 100644 index b2a8727..0000000 --- a/commands/general/banner.js +++ /dev/null @@ -1,55 +0,0 @@ -import Command from "../../classes/command.js"; -import { Routes } from "oceanic.js"; -const mentionRegex = /^?$/; - -class BannerCommand extends Command { - // this command sucks - async run() { - const member = this.options.member ?? this.args[0]; - const self = await this.client.rest.users.get(this.author); // banners are only available over REST - if (this.type === "classic" && this.message.mentions.users[0] && this.message.mentions.users[0].banner) { - return this.client.util.formatImage(Routes.BANNER(this.message.mentions.users[0].id, this.message.mentions.users[0].banner), null, 512); - } else if (member && member > 21154535154122752n) { - const user = await this.client.rest.users.get(member); - if (user && user.banner) { - return this.client.util.formatImage(Routes.BANNER(user.id, user.banner), null, 512); - } else if (mentionRegex.test(member)) { - const id = member.match(mentionRegex)[1]; - if (id < 21154535154122752n) { - this.success = false; - return "That's not a valid mention!"; - } - try { - const user = await this.client.rest.users.get(id); - return user.banner ? this.client.util.formatImage(Routes.BANNER(user.id, user.banner), null, 512) : "This user doesn't have a banner!"; - } catch { - return self.banner ? this.client.util.formatImage(Routes.BANNER(self.id, self.banner), null, 512) : "You don't have a banner!"; - } - } else { - return "This user doesn't have a banner!"; - } - } else if (this.args.join(" ") !== "" && this.guild) { - const searched = await this.guild.searchMembers({ - query: this.args.join(" "), - limit: 1 - }); - if (searched.length === 0) return self.banner ? this.client.util.formatImage(Routes.BANNER(self.id, self.banner), null, 512) : "This user doesn't have a banner!"; - const user = await this.client.rest.users.get(searched[0].user.id); - return user.banner ? this.client.util.formatImage(Routes.BANNER(user.id, user.banner), null, 512) : (self.banner ? this.client.util.formatImage(Routes.BANNER(self.id, self.banner), null, 512) : "This user doesn't have a banner!"); - } else { - return self.banner ? this.client.util.formatImage(Routes.BANNER(self.id, self.banner), null, 512) : "You don't have a banner!"; - } - } - - static description = "Gets a user's banner"; - static aliases = ["userbanner"]; - static arguments = ["{mention/id}"]; - static flags = [{ - name: "member", - type: 6, - description: "The member to get the banner from", - required: false - }]; -} - -export default BannerCommand; diff --git a/commands/general/donate.js b/commands/general/donate.js index d6b2964..e7f8913 100644 --- a/commands/general/donate.js +++ b/commands/general/donate.js @@ -3,7 +3,7 @@ import Command from "../../classes/command.js"; class DonateCommand extends Command { async run() { - await this.acknowledge(); + // await this.acknowledge(); let prefix = ""; const controller = new AbortController(); const timeout = setTimeout(() => { diff --git a/commands/general/eval.js b/commands/general/eval.js index 083e02c..7c14866 100644 --- a/commands/general/eval.js +++ b/commands/general/eval.js @@ -8,7 +8,7 @@ class EvalCommand extends Command { this.success = false; return "Only the bot owner can use eval!"; } - await this.acknowledge(); + // await this.acknowledge(); const code = this.options.code ?? this.args.join(" "); try { let evaled = eval(code); diff --git a/commands/general/exec.js b/commands/general/exec.js index 0f6d94f..d1744e7 100644 --- a/commands/general/exec.js +++ b/commands/general/exec.js @@ -11,7 +11,7 @@ class ExecCommand extends Command { this.success = false; return "Only the bot owner can use exec!"; } - await this.acknowledge(); + // await this.acknowledge(); const code = this.options.cmd ?? this.args.join(" "); try { const execed = await exec(code); diff --git a/commands/general/image.js b/commands/general/image.js index 1a8e07f..12acba3 100644 --- a/commands/general/image.js +++ b/commands/general/image.js @@ -11,7 +11,7 @@ class ImageSearchCommand extends Command { if (this.channel && !this.channel.permissionsOf(this.client.user.id.toString()).has("EMBED_LINKS")) return "I don't have the `Embed Links` permission!"; const query = this.options.query ?? this.args.join(" "); if (!query || !query.trim()) return "You need to provide something to search for!"; - await this.acknowledge(); + // await this.acknowledge(); const embeds = []; const rawImages = await request(`${random(searx)}/search?format=json&safesearch=2&categories=images&q=!goi%20!ddi%20${encodeURIComponent(query)}`).then(res => res.body.json()); if (rawImages.results.length === 0) return "I couldn't find any results!"; diff --git a/commands/general/imagereload.js b/commands/general/imagereload.js index c37f7d2..3821899 100644 --- a/commands/general/imagereload.js +++ b/commands/general/imagereload.js @@ -8,7 +8,7 @@ class ImageReloadCommand extends Command { this.success = false; return "Only the bot owner can reload the image servers!"; } - await this.acknowledge(); + // await this.acknowledge(); const length = await reloadImageConnections(); if (!length) { if (process.env.PM2_USAGE) { diff --git a/commands/general/imagestats.js b/commands/general/imagestats.js index 247c08c..8d2a98a 100644 --- a/commands/general/imagestats.js +++ b/commands/general/imagestats.js @@ -3,28 +3,15 @@ import { connections } from "../../utils/image.js"; class ImageStatsCommand extends Command { async run() { - await this.acknowledge(); - const embed = { - embeds: [{ - "author": { - "name": "esmBot Image Statistics", - "iconURL": this.client.user.avatarURL() - }, - "color": 16711680, - "description": `The bot is currently connected to ${connections.size} image server(s).`, - "fields": [] - }] - }; + // await this.acknowledge(); + let desc = `The bot is currently connected to ${connections.size} image server(s).\n` let i = 0; for (const connection of connections.values()) { const count = await connection.getCount(); if (!count) continue; - embed.embeds[0].fields.push({ - name: `Server ${i++}`, - value: `Running Jobs: ${count}` - }); + desc = desc + `Server ${i++}\nRunning Jobs: ${count}` } - return embed; + return desc; } static description = "Gets some statistics about the image servers"; diff --git a/commands/general/info.js b/commands/general/info.js index 12ee147..746a11e 100644 --- a/commands/general/info.js +++ b/commands/general/info.js @@ -8,7 +8,7 @@ class InfoCommand extends Command { let owner = this.client.users.get(process.env.OWNER.split(",")[0]); if (!owner) owner = await this.client.rest.users.get(process.env.OWNER.split(",")[0]); const servers = await getServers(this.client); - await this.acknowledge(); + // await this.acknowledge(); return { embeds: [{ color: 16711680, diff --git a/commands/general/lengthen.js b/commands/general/lengthen.js index b25f154..f4a6e8b 100644 --- a/commands/general/lengthen.js +++ b/commands/general/lengthen.js @@ -4,7 +4,7 @@ import Command from "../../classes/command.js"; class LengthenCommand extends Command { async run() { - await this.acknowledge(); + // await this.acknowledge(); const input = this.options.url ?? this.args.join(" "); this.success = false; if (!input || !input.trim() || !urlCheck(input)) return "You need to provide a short URL to lengthen!"; diff --git a/commands/general/qrcreate.js b/commands/general/qrcreate.js index 49f6829..b64d679 100644 --- a/commands/general/qrcreate.js +++ b/commands/general/qrcreate.js @@ -8,7 +8,7 @@ class QrCreateCommand extends Command { this.success = false; return "You need to provide some text to generate a QR code!"; } - await this.acknowledge(); + // await this.acknowledge(); const writable = new PassThrough(); qrcode.toFileStream(writable, this.content, { margin: 1 }); const file = await this.streamToBuf(writable); diff --git a/commands/general/qrread.js b/commands/general/qrread.js index 84bcc1d..8136d89 100644 --- a/commands/general/qrread.js +++ b/commands/general/qrread.js @@ -10,7 +10,7 @@ class QrReadCommand extends Command { const image = await imageDetect(this.client, this.message, this.interaction, this.options); this.success = false; if (image === undefined) return "You need to provide an image/GIF with a QR code to read!"; - await this.acknowledge(); + // await this.acknowledge(); const data = Buffer.from(await (await request(image.path)).body.arrayBuffer()); const rawData = await sharp(data).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); const qrBuffer = jsqr(rawData.data, rawData.info.width, rawData.info.height); diff --git a/commands/general/raw.js b/commands/general/raw.js index 28bdb6e..bfd90ee 100644 --- a/commands/general/raw.js +++ b/commands/general/raw.js @@ -3,7 +3,7 @@ import imageDetect from "../../utils/imagedetect.js"; class RawCommand extends Command { async run() { - await this.acknowledge(); + // await this.acknowledge(); const image = await imageDetect(this.client, this.message, this.interaction, this.options); if (image === undefined) { this.success = false; diff --git a/commands/general/reload.js b/commands/general/reload.js index ca7180c..5f382cd 100644 --- a/commands/general/reload.js +++ b/commands/general/reload.js @@ -8,7 +8,7 @@ class ReloadCommand extends Command { if (!owners.includes(this.author)) return "Only the bot owner can reload commands!"; const commandName = this.options.cmd ?? this.args.join(" "); if (!commandName || !commandName.trim()) return "You need to provide a command to reload!"; - await this.acknowledge(); + // await this.acknowledge(); const path = paths.get(commandName); if (!path) return "I couldn't find that command!"; const result = await load(this.client, path, true); diff --git a/commands/general/soundreload.js b/commands/general/soundreload.js index a405a29..84f48e0 100644 --- a/commands/general/soundreload.js +++ b/commands/general/soundreload.js @@ -8,7 +8,7 @@ class SoundReloadCommand extends Command { this.success = false; return "Only the bot owner can reload Lavalink!"; } - await this.acknowledge(); + // await this.acknowledge(); const length = await reload(); if (process.env.PM2_USAGE) { process.send({ diff --git a/commands/general/youtube.js b/commands/general/youtube.js index 17b43b8..514da42 100644 --- a/commands/general/youtube.js +++ b/commands/general/youtube.js @@ -10,7 +10,7 @@ class YouTubeCommand extends Command { const query = this.options.query ?? this.args.join(" "); this.success = false; if (!query || !query.trim()) return "You need to provide something to search for!"; - await this.acknowledge(); + // await this.acknowledge(); 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()); if (videos.results.length === 0) return "I couldn't find any results!"; diff --git a/commands/message/select-image.js b/commands/message/select-image.js index 22f5b68..23bab19 100644 --- a/commands/message/select-image.js +++ b/commands/message/select-image.js @@ -4,7 +4,7 @@ import { selectedImages } from "../../utils/collections.js"; class SelectImageCommand extends Command { async run() { - await this.acknowledge(); + // await this.acknowledge(); const message = this.interaction.data.target; const image = await imageDetect(this.client, message, this.interaction, this.options, true, false, false, true); this.success = false; diff --git a/commands/music/music.js b/commands/music/music.js index 17b1cc2..3be88bc 100644 --- a/commands/music/music.js +++ b/commands/music/music.js @@ -6,7 +6,7 @@ class MusicAIOCommand extends Command { async run() { let cmd = this.type === "classic" ? this.args[0] : this.optionsArray[0].name; if (cmd === "music" || this.constructor.aliases.includes(cmd)) return "https://esmbot.net/robotdance.gif"; - await this.acknowledge(); + // await this.acknowledge(); if (this.type === "classic") { this.origOptions.args.shift(); } else { diff --git a/commands/soundboard/soundboard.js b/commands/soundboard/soundboard.js index 9358905..2696b99 100644 --- a/commands/soundboard/soundboard.js +++ b/commands/soundboard/soundboard.js @@ -11,7 +11,7 @@ class SoundboardAIOCommand extends Command { return "You need to provide a sound to play!"; } const name = sounds.get(soundName); - await this.acknowledge(); + // await this.acknowledge(); return await play(this.client, name, { channel: this.channel, member: this.member, type: this.type, interaction: this.interaction }); } diff --git a/events/roommessage.js b/events/roommessage.js index 76e5c75..51b180b 100644 --- a/events/roommessage.js +++ b/events/roommessage.js @@ -89,13 +89,17 @@ export default async function (matrixClient, event, room, toStartOfTimeline) { const mxcUri = await matrixClient.uploadContent(result.contents); // 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) + let mime = imgsize.type; + if (mime == "jpg") { + mime = "jpeg"; + } + await matrixClient.sendImageMessage(event.event.room_id, mxcUri.content_uri, {h: imgsize.height, w: imgsize.width, mimetype: `image/${mime}`, size: result.contents.length, thumbnail_info: {h: imgsize.height, w: imgsize.width, mimetype: `image/${mime}`, size: result.contents.length}}, result.name) } } else { // await client.rest.channels.createMessage(message.channelID, Object.assign(result, reference)); } } else { - console.log(typeof result) + // console.log(typeof result) } } catch (error) { logger.log("error", error.stack) diff --git a/utils/imagedetect.js b/utils/imagedetect.js index 9e43148..859464d 100644 --- a/utils/imagedetect.js +++ b/utils/imagedetect.js @@ -38,6 +38,17 @@ const combined = [...tenorURLs, ...giphyURLs, ...giphyMediaURLs, ...imgurURLs, . const imageFormats = ["image/jpeg", "image/png", "image/webp", "image/gif", "large"]; const videoFormats = ["video/mp4", "video/webm", "video/mov"]; +// check if url contents is valid url +const isValidUrl = urlString=> { + var urlPattern = new RegExp('^(https?:\\/\\/)?'+ // validate protocol + '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // validate domain name + '((\\d{1,3}\\.){3}\\d{1,3}))'+ // validate OR ip (v4) address + '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // validate port and path + '(\\?[;&a-z\\d%_.~+=-]*)?'+ // validate query string + '(\\#[-a-z\\d_]*)?$','i'); // validate fragment locator + return !!urlPattern.test(urlString); +} + // gets the proper image paths const getImage = async (image, image2, video, extraReturnTypes, gifv = false, type = null, link = false) => { try { @@ -125,7 +136,16 @@ const checkImages = async (message, extraReturnTypes, video, sticker) => { const fileNameNoExtension = message.content.body.slice(0, message.content.body.lastIndexOf(".")); type = {name: fileNameNoExtension, path: url, url: url, type: message.content.info.mimetype} } + } + if (message.content.msgtype == "m.text") { + let url = message.content.body + url = url.replace(/.*\n\n/g, "") + if (isValidUrl(url)){ + const mimetype = await getType(url, extraReturnTypes) + type = {name: "image", path: url, url: url, type: mimetype} + } } + // // first check the embeds // if (message.embeds.length !== 0) { // // embeds can vary in types, we check for tenor gifs first