From 153fa19ec2e83b9b3d9031a599f481f35f51bb38 Mon Sep 17 00:00:00 2001 From: Evorp <3vorpgaming@gmail.com> Date: Wed, 12 Jul 2023 12:55:59 -0700 Subject: [PATCH] add back /m and /z --- commands/other/magnify.js | 118 ++++++++++++++++++++++++++++++++++++++ resources/strings.json | 1 + 2 files changed, 119 insertions(+) create mode 100644 commands/other/magnify.js diff --git a/commands/other/magnify.js b/commands/other/magnify.js new file mode 100644 index 000000000..f27f0385c --- /dev/null +++ b/commands/other/magnify.js @@ -0,0 +1,118 @@ +const prefix = process.env.PREFIX; + +const strings = require("../../resources/strings.json"); + +const warnUser = require("../../helpers/warnUser"); +const { magnify } = require("../../functions/textures/magnify"); + +module.exports = { + name: "magnify", + aliases: ["m", "z"], + description: strings.command.description.magnify, + category: "Faithful", + guildOnly: false, + uses: strings.command.use.anyone, + example: `${prefix}m + file attached`, + async execute(client, message, args) { + let DATA; + + // + // image attached + if ((args[0] == undefined || args[0] == '') && message.attachments.size > 0) { + DATA = message.attachments.first().url; + return magnify(message, DATA); + } + + // replying to message + else if (message.reference) { + message.channel.messages.fetch(message.reference.messageId).then(msg => { + if (msg.attachments.size > 0) { + DATA = msg.attachments.first().url; + return magnify(message, DATA); + } + else return warnUser(message, strings.command.image.no_reply_attachment); + }).catch(error => { + return warnUser(message, error); + }) + } + + // previous image + else if ((args[0] == undefined || args[0] == '' || args[0] == 'up' || args[0] == '^' || args[0] == 'last') && message.attachments.size == 0) { + return PreviousImage(); + } + + // Discord message URL + else if (args[0].startsWith('https://discord.com/channels')) { + message.channel.messages.fetch(args[0].split('/').pop()).then(msg => { + if (msg.attachments.size > 0) { + DATA = msg.attachments.first().url; + return magnify(message, DATA); + } + else return warnUser(message, strings.command.image.not_attached.message) + }).catch(() => { return warnUser(message, strings.command.url.same_channel_only) }) + } + + // Image URL + else if (args[0].startsWith('https://') || args[0].startsWith('http://')) { + if (args[0].endsWith('.png') || args[0].endsWith('.jpeg') || args[0].endsWith('.jpg') || args[0].endsWith('.gif')) { + DATA = args[0]; + return magnify(message, DATA); + } else return warnUser(message, strings.command.image.invalid_extension) + } + + // Discord message ID + else if (!isNaN(args[0])) { + message.channel.messages.fetch(args[0]).then(msg => { + if (msg.attachments.size > 0) { + DATA = msg.attachments.first().url; + return magnify(message, DATA); + } + else return warnUser(message, strings.command.image.not_attached.id); + }).catch(error => { + return warnUser(message, error); + }) + } + + async function PreviousImage() { + /** + * DO NOT DELETE THE COMMENTS IN THIS FUNCTION! + * Right now this function is using a workaround for something that was broken by discord.js v13 and may possibly work again in the future. + */ + + var found = false; + //var messages = []; + var list_messages = await message.channel.messages.fetch({ limit: 10 }); + var lastMsg = list_messages.sort((a, b) => b.createdTimestamp - a.createdTimestamp).filter((m) => m.attachments.size > 0 || m.embeds[0] != undefined).first(); + //messages.push(...list_messages.array()); + + //for (var i in messages) { + //var msg = messages + var url = ''; + try { + if (lastMsg.attachments.size > 0) { + found = true; + url = lastMsg.attachments.first().url; + //break; + } + else if (lastMsg.embeds[0] != undefined && lastMsg.embeds[0] != null && lastMsg.embeds[0].image) { + found = true; + url = lastMsg.embeds[0].image.url; + //break; + } + else if (lastMsg.content.startsWith('https://') || lastMsg.content.startsWith('http://')) { + if (lastMsg.content.endsWith('.png') || lastMsg.content.endsWith('.jpeg') || lastMsg.content.endsWith('.jpg') || lastMsg.content.endsWith('.gif')) { + found = true; + url = lastMsg.content; + //break; + } + } + } catch (e) { + return warnUser(message, strings.command.image.not_found_in_10_last) + } + //} + + if (found) await magnify(message, url) + else return warnUser(message, strings.command.image.not_found_in_10_last) + } + } +}; \ No newline at end of file diff --git a/resources/strings.json b/resources/strings.json index d04016500..fc2799d1c 100644 --- a/resources/strings.json +++ b/resources/strings.json @@ -17,6 +17,7 @@ "reload": "Reloads a command", "say": "Make the bot send any message you specify", "shutdown": "Stops the bot", + "magnify": "Resize an image.", "status": "Changes the bot's status: \n**Activity**:\nPLAYING, STREAMING, LISTENING, WATCHING, COMPETING, CUSTOM_STATUS (does not work)\n**Presence**:\nonline, idle, dnd", "animate": "Animates a texture." },