2

My main goal is to be able to run a small command like, $yoink, that will take the last sent emoji in the chat and make it into an image I can download. I've already got something like this but, it only detects regular images and embedded images (using it to make Jimp function with my bot) Here's what I have so far:

async function GetImageFromMsg(msg) {
    var imageUrl = null;
    const urlsnifferpattern = /(https?:\/\/[^\s]+\.(?:png|jpg|jpeg|bmp|gif|tiff|webp)(?:$|[^\s]+))/i;

    if (msg.attachments && (getImg = msg.attachments.find(val => val.height && val.url)) && getImg.url.match(urlsnifferpattern)) // user uploads directly to chat.
        imageUrl = getImg.url;
    if (msg.content && (getImg = msg.content.match(urlsnifferpattern))) // Here first to avoid caching issues on the next two checks.
        imageUrl = getImg[0].replace(">", "");
    if (msg.embeds && (getImg = msg.embeds.find(val => val.thumbnail && val.thumbnail.url))) // small image ONLY on  bot-made embeds - (small??>)big image on LINK-made embeds - random image URLS with no embeds.
        imageUrl = getImg.thumbnail.url;
    if (msg.embeds && (getImg = msg.embeds.find(val => val.image && val.image.url))) // big image on bot-made embeds.
        imageUrl = getImg.image.url;

    if (!imageUrl)
        return null;

    return imageUrl;
}

How should I go about adding in support to also grab emoji?

FerretPaws
  • 83
  • 11

1 Answers1

2

You can check if your message contains any emoji with regular expressions.

Here is one example how to do it (its Python but RegEx is the same trash everywhere) and another detailed explanation how Discord handles emojis can be found here.

To get the plain image, you need to get the emoji id first with

client.emojis.find(emoji => emoji.name === "emoji_name");

Then you can go on and get the plain image by inserting the id to this url:

function getEmoji(id){
   var linkToMyEmoji = `https://cdn.discordapp.com/emojis/${id}.png`
   // ...
}

Hope this can help you :)

julianYaman
  • 310
  • 4
  • 13
  • I know this is a dumb question but, where would be the best place to put this code? Should it go within the command structure or put it outside of my array list? – FerretPaws Sep 24 '20 at 01:23
  • So if you put it outside of your command structure, you might need to give some parameters to the function like I did in the example code (e.g. the id or some more). Then the only thing you need to do is to return the link (or something else, depends on your use case) If you just want to have the url and don't do any fancy thing with it, it might be better to just write this into your command structure. But like I said, it depends on your use case. – julianYaman Sep 24 '20 at 05:55