So I made an enlarge command that enlarges custom emojis in servers, and it works but I want to get it to where it does default emojis as well. I don’t know how to do default ones, so can someone help?
Here’s the current code:
#commands.command()
async def enlarge(self, ctx, emoji: typing.Union[discord.Emoji, discord.PartialEmoji, str]):
if type(emoji) is str:
return await ctx.reply('Please enter an emoji to enlarge!')
else:
await ctx.send(emoji.url)
The default discord emoji are just unicode characters.
This is also the reason why custom emoji return an Emoji-Object, while default ones only return a string or single character. What you want to do is convert the unicode emoji characters to an image that represents them.
For this, you need some source of those images.
As described in this github gist, discord uses Twemoji as their default emoji source.
In the Twemoji-Github, they mention MaxCDN as their CDN supporter.
While this is meant to primarily address Javascript-Developers who want to use the Twemoji-scripts on their websites, it also provides the emoji icons.
It is possible to fetch those icons via
https://twemoji.maxcdn.com/v/latest/EMOJI_SIZE/EMOJI_ID.png In this url, you need to replace EMOJI_SIZE by the image size (72x72 is currently the only one available, but it should fit your needs quite well).
You also need to determine your emojis “id”/Unicode codepoint (denoted by EMOJI_ID in above link), an integer in hexadecimal notation which represents your emoji character.
If emoji stores your character, you can obtain this integer using
emoji_id = ord(emoji)
Putting it all together, your command could look like this:
#bot.command()
async def enlarge(ctx, emoji: typing.Union[discord.Emoji, discord.PartialEmoji, str]):
if type(emoji) is str:
emoji_id = ord(emoji[0]) # Emoji is a string, we will use the first character
await ctx.send(f'https://twemoji.maxcdn.com/v/latest/72x72/{emoji_id:x}.png')
else:
await ctx.send(emoji.url)
Note that I use Python's f-Strings for formatting which is supported since Python 3.6.
If you are running an older version, you have to use another way of formatting.
Keep in mind that you will need the hexadecimal representation in your url.
If you don't want to use MaxCDN to serve the images, you could also download them at the Twemoji Github and store them locally.
Inside discord, it is also possible to right-click an emoji and get a discordcdn-link, but I wasn't able to construct those links from a given emoji.
Maybe someone else knows how to do this in order to avoid external services like MaxCDN.
Related
Hello so i try to scrape off the author image url from the given video link using the urllib3 module, but due to different lengths of the url it causes to join other properties like the width and height
https://yt3.ggpht.com/ytc/AKedOLS-Bwwebj7zfYDDo43sYPxD8LN7q4Lq4EvqfyoDbw=s400-c-k-c0x00ffffff-no-rj","width":400,"height"
instead of this author image link which i want :
https://yt3.ggpht.com/ytc/AKedOLS-Bwwebj7zfYDDo43sYPxD8LN7q4Lq4EvqfyoDbw=s400-c-k-c0x00ffffff-no-rj
the code that i worked
import re
import urllib.request
def get_author(uri):
html = urllib.request.urlopen(uri)
author_image = re.findall(r'yt3.ggpht.com/(\S{99})', html.read().decode())
return f"https://yt3.ggpht.com/{author_image[1]}"
sorry for my bad english, thanks in advance =)
If you are not sure about the length of the match, do not hardcode the amount of chars to be matched. {99} is not going to work with arbitrary strings.
Besides, you want to match the string in a mark-up text and you need to be sure you only match until the delimiting char. If it is a " char, then match until that character.
Also, dots in regex are special and you need to escape them to match literal dots.
Besides, findall is used to match all occurrences, you can use re.search to get the first one to free up some resources.
So, a fix could look like
def get_author(uri):
html = urllib.request.urlopen(uri)
author_image = re.search(r'yt3\.ggpht\.com/[^"]+', html.read().decode())
if author_image:
return f"https://{author_image.group()}"
return None # you will need to handle this condition in your later code
Here, author_image is the regex match data object, and if it matches, you need to prepend the match value (author_image.group()) with https:// and return the value, else, you need to return some default value to check later in the code (here, None).
I am learning how to develop Skills with Alexa. I followed a Lynda course to build the My Calculator skill, however ran into a problem where the numbers and results are not returned. I double checked my code, and tried it on Echoism.io, and same problem.
Per the attached, the numbers are recorded in the JSON input, however are not returned in the speechText or displayText?
What is the missing piece of code? Thanks.
Node.js code
Alexa console JSON Input
Welcome Roy! Couple things. In the future just put your code right here on the page so that it becomes searchable and we can see the exact characters you are using.
By looking at the images it looks like to me the following might be your use of template literals. Super common mistake.
So to use template literals you need use the back tick (`) instead of the single quote (')
It looks like it is that you currently have
speechText = 'The result of ${firstNumber} plus ${secondNumber} is ${result}';
and what you want is
speechText = `The result of ${firstNumber} plus ${secondNumber} is ${result}`;
Here is another good resources:
How to interpolate variables in strings in JavaScript, without concatenation?
Good day,
I missed something in telethondocumentation. All is clear with files, messages, document, but i cannot find, how to send emoji to other user. When I send emoji code like ;-) it sends it as raw message. If it is equals to send file, please help me to find list of emoji id to put into file variable. Official documentation provides functions below, it is not clear.
GetEmojiKeywordsDifferenceRequest
GetEmojiKeywordsLanguagesRequest
GetEmojiKeywordsRequest
GetEmojiURLRequest
Please hint me with it :)
Emoji are just strings, like any other in Python. The ";-)" replacement for "😉" in official clients is done on the client side, not the server.
You should be able to paste the emoji directly into your code, or if your editor does not support it, use a Python unicode escape:
client.send_message(chat, '😉')
client.send_message(chat, '\U0001F609')
If you prefer to use text replacements in your code, install the emoji package:
import emoji
client.send_message(chat, emoji.emojize(':wink:'))
(Please note I have not tried the emoji module myself, see their documentation for available replacements.)
I am having two cards in my card type, which I created based on the cloze deletion type, by copying it.
If the position of my card is 1, than as described in the documentation, {{cloze:MyDataField}} works, as I like. But if the position of the card is two, the cloze deletion extension seems to be disabled.
On my card on position two I only need to display the sentence, without the {{c1::word1}}, ...., {{cN::wordN}} tags but with the words, wrapped by those.
Is there maybe a way to create two cards using a close type in my card type's template? Or is there maybe a way to get the plain text of my fields so that I can parse it with JavaScript? Or can I maybe somehow just display the content of the field, ignoring {{c1 expressions, but displaying the content?
It is possible to write an Anki add-on to do this, if you are familiar with Python 2. Cards compiled with a Desktop Anki with this add-on installed will work on AnkiWeb without displaying gibberish. Here is an (untested!) add-on that should register a special fmod that allows you to write {{uncloze:fieldname}} to get the plain text sans {{cn:: and }} marks.
import re
from anki.hooks import addHook
from anki.template.template import Template
"""Adds support for the unclose Moustache tag.
Blame wizzwizz4 if this does / doesn't work.
Modified to work for <anki_version> by <your_name>."""
open_cloze_regex = re.compile(r"{{c\d+::")
def fmod_uncloze(txt, extra, context, tag, tag_name):
field_name = tag_name[8:] # Strip off "uncloze:"
field_content = Template("{{" + field_name + "}}", context).render()
return open_cloze_regex.sub("", field_content).replace("}}", "")
addHook("fmod_uncloze", fmod_uncloze)
Put this in a file in the Anki add-ons directory then restart Anki to install it.
Much of this code is from this answer.
I know about Data URIs in which base64 encoded data can be used inline such as images. Today I received an email actually an spam one in which there was an animated (gif) icon in its subject:
Here is the icon alone:
So the only thing did cross my mind was all about Data URIs and if Gmail allows some sort of emoticons to be inserted in subject. I saw the full detailed version of email and pointed to subject line at the below picture:
So GIF comes from =?UTF-8?B?876Urg==?= encoded string which is similar to Data URI scheme however I couldn't get the icon out of it. Here is element HTML source:
Long story short, there are lots of emoticons from https://mail.google.com/mail/e/XXX where XXX are hexadecimal numbers. They are documented nowhere or I couldn't find it. If that's about Data URI, so how is it possible to include them in Gmail's email subject? (I forwarded that email to a yahoo email account, seeing [?] instead of icon) and if it's not, then how that encoded string is parsed?
#Short description:
They are referred to internally as goomoji, and they appear to be a non-standard UTF-8 extension. When Gmail encounters one of these characters, it is replaced by the corresponding icon. I wasn't able to find any documentation on them, but I was able to reverse engineer the format.
#What are these icons?
Those icons are actually the icons that appear under the "Insert emoticons" panel.
While I don't see the 52E icon in the list, there are several others that follow the same convention.
B0C
4F4
Note that there are also some icons whose names are prefixed, such as gtalk.03C . I was not able to determine if or how these icons can be used in this manner.
#What is this Data URI thing?
It's not actually a Data URI, though it does share some similarities. It's actually a special syntax for encoding non-ASCII characters in email subjects, defined in RFC 2047. Basically, it works like this.
=?charset?encoding?data?=
So, in our example string, we have the following data.
=?UTF-8?B?876Urg==?=
charset = UTF-8
encoding = B (means base64)
data = 876Urg==
#So, how does it work?
We know that somehow, 876Urg== means the icon 52E, but how?
If we base64 decode 876Urg==, we get 0xf3be94ae. This looks like the following in binary:
11110011 10111110 10010100 10101110
These bits are consistent with a 4-byte UTF-8 encoded character.
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
So the relevant bits are the following.:
011 111110 010100 101110
Or when aligned:
00001111 11100101 00101110
In hexadecimal, these bytes are the following:
FE52E
As you can see, except for the FE prefix which is presumably to distinguished the goomoji icons from other UTF-8 characters, it matches the 52E in the icon URL. Some testing proves that this holds true for other icons.
#Sounds like a lot of work, is there a converter?:
This can of course be scripted. I created the following Python code for my testing. These functions can convert the base64 encoded string to and from the short hex string found in the URL. Note, this code is written for Python 3, and is not Python 2 compatible.
###Conversion functions:
import base64
def goomoji_decode(code):
#Base64 decode.
binary = base64.b64decode(code)
#UTF-8 decode.
decoded = binary.decode('utf8')
#Get the UTF-8 value.
value = ord(decoded)
#Hex encode, trim the 'FE' prefix, and uppercase.
return format(value, 'x')[2:].upper()
def goomoji_encode(code):
#Add the 'FE' prefix and decode.
value = int('FE' + code, 16)
#Convert to UTF-8 character.
encoded = chr(value)
#Encode UTF-8 to binary.
binary = bytearray(encoded, 'utf8')
#Base64 encode return end return a UTF-8 string.
return base64.b64encode(binary).decode('utf-8')
###Examples:
print(goomoji_decode('876Urg=='))
print(goomoji_encode('52E'))
###Output:
52E
876Urg==
And, of course, finding an icon's URL simply requires creating a new draft in Gmail, inserting the icon you want, and using your browser's DOM inspector.
If you use the correct hex code point (e.g. fe4f4 for 'pile of poo') and If it is correctly encoded within the subject line header, let it be base64 (see #AlexanderOMara) or quoted-printable (=?utf-8?Q?=F3=BE=93=B4?=), then Gmail will automatically parse and replace it with the corresponding emoji.
Here's a Gmail emoji list for copying and pasting into subject lines - or email bodies. Animated emojis, which will grab even more attention in the inbox, are placed on a yellow background:
Many thanks to Alexander O'Mara for such a well-researched answer about the goomoji-tagged HTML images!
I just wanted to add three things:
There are still many many emoji (and other Unicode sequences generating pictures) that spammers and other erstwhile marketers are starting to use in email subject lines and that gmail does not convert to HTML images. In some browsers these show up bold and colored, which is almost as bad as animation. Browsers could also choose to animate these, but I don't know if any do. These Unicode sequences get displayed by the browser as Unicode text, so the exact appearance (color or not, animated or not, ...) depends on what text rendering system the browser is using. The appearance of a given Unicode emoji also depends on any Unicode variation selectors and emoji modifiers that appear near it in the Unicode code point sequence. Unlike the image-based emoji spam, these sequences can be copied-and-pasted out of the browser and into other apps as Unicode text.
I hope the many marketers reading this StackOverflow question will just say no. It is a horrible idea to include these sequences in your email subject lines and it will immediately tarnish you and your brand as lowlife spammers. It is not worth the "attention" your email will get.
Of course the first question coming to everyone's mind is: "how do I get rid of these things?" Fortunately there is this open-source Greasemonkey/Tampermonkey/Violentmonkey userscript:
Gmail Subject Line Emoji Roach Motel
This userscript eliminates both HTML-image (thanks to awesome work of Alexander O'Mara) and pure-Unicode types.
For the latter type, the userscript includes a regular expression designed to capture the Unicode sequences likely to be abused by marketers. The regex looks like this in ES6 Javascript (the userscript translates this to widely-supported pre-ES6 regex using the amazing ES6 Regex Transpiler):
var re = /(\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}\uFE0F|[\u{2100}-\u{2BFF}\u{E000}-\u{F8FF}\u{1D000}-\u{1F5FF}\u{1F650}-\u{1FA6F}\u{F0000}-\u{FFFFF}\u{100000}-\u{10FFFF}])\s*/gu
// which includes the Unicode Emoji pattern from
// https://github.com/tc39/proposal-regexp-unicode-property-escapes
// plus also these blocks frequently used for spammy emojis
// (see https://en.wikipedia.org/wiki/Unicode_block ):
// U+2100..U+2BFF Arrows, Dingbats, Box Drawing, ...
// U+E000..U+F8FF Private Use Area (gmail generates them for some emoji)
// U+1D000..U+1F5FF Musical Symbols, Playing Cards (sigh), Pictographs, ...
// U+1F650..U+1FA6F Ornamental Dingbats, Transport and Map symbols, ...
// U+F0000..U+FFFFF Supplementary Private Use Area-A
// U+100000..U+10FFFF Supplementary Private Use Area-B
// plus any space AFTER the discovered emoji spam