How to validate if text is emoji? 🤔

My research has not turned up anything. I want users to be able to type in any emoji that is a valid emoji that could be used as a reaction via Discord's emoji picker. (I'm having the bot add reactions to a message.) I want to validate the character that the user enters is a valid emoji from the Discord emoji picker. (Standard, or custom server emoji. Whatever.) How do I go about validating this? Some regex rule? Some built-in method?
29 Replies
d.js toolkit
d.js toolkit13mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
d.js docs
d.js docs13mo ago
- For unicode emojis (twemoji): learn more - Custom emojis: /<?(a)?:?(\w{2,32}):(\d{17,19})>?/ learn more
2Flow2
2Flow2OP13mo ago
I looked at both of those docs before posting this question, but I wasn't quite sure how to use them.
ShompiFlen
ShompiFlen13mo ago
there is also an utility for parsing emojis that you might like
d.js docs
d.js docs13mo ago
method parseEmoji Parses emoji info out of a string. The string must be one of: (more...)
2Flow2
2Flow2OP13mo ago
That looks almost exactly like what I want! How do I import it? (I'm getting parseEmoji is not defined if I try to use it without any new library import statements.) I'm seeing that it is in Util.js, correct? I'm kind of a noob, and I don't know how to import the functions from that file 😅
ShompiFlen
ShompiFlen13mo ago
you import it directly from discord.js
2Flow2
2Flow2OP13mo ago
So something like
const { parseEmoji, SlashCommandBuilder } = require('discord.js');
const { parseEmoji, SlashCommandBuilder } = require('discord.js');
should work? I've checked node_modules/discord.js/src/util/Util.js and I see the function defined in there in my environment. But this above statement doesn't work for some reason...
ShompiFlen
ShompiFlen13mo ago
what version of discord.js is this
2Flow2
2Flow2OP13mo ago
I'm still receiving parseEmoji is not defined on the line that I call it. Lemme check to make sure.
ShompiFlen
ShompiFlen13mo ago
make sure you save the code and restart the process
2Flow2
2Flow2OP13mo ago
Oh my gosh I am so sorry, I just realized my console wasn't clearing and I didn't realize it, that was the issue 🤦‍♀️🤦‍♀️ I'm so sorry to waste your time with that last question.
ShompiFlen
ShompiFlen13mo ago
no need to apologize
2Flow2
2Flow2OP13mo ago
Although how odd! Now that the statement is running, the function is returning an APIEmoji but the name field is the entire message that it was sent to parse... I assumed it would have just taken the emoji out of the message and only returned that emoji?
ShompiFlen
ShompiFlen13mo ago
uhm let me check
2Flow2
2Flow2OP13mo ago
No description
2Flow2
2Flow2OP13mo ago
No description
2Flow2
2Flow2OP13mo ago
No description
2Flow2
2Flow2OP13mo ago
Ohh, maybe this function is just to give you info about an emoji if you feed it a straight one (like, single character emoji with nothing else) in the code?
ShompiFlen
ShompiFlen13mo ago
I see
2Flow2
2Flow2OP13mo ago
Although even when I pass nothing but an emoji it doesn't seem to provide me any data other than "animated: false" so maybe that theory is wrong.
ShompiFlen
ShompiFlen13mo ago
yea the same happens on my tests. If you use an unicode emoji this function will parse the entire content, not sure why. It works fine with custom emojis unicode emojis will only have a name Hm... kind of weird not gonna lie, i dont think this function is supposed to work like this
2Flow2
2Flow2OP13mo ago
Oh hey you're right. My tests are showing the same. Bummer it only grabs the first emoji from the string if there are multiple, but that part is probably intentional. Should this wacky behavior be reported in the Issues on the GitHub?
ShompiFlen
ShompiFlen13mo ago
well looking at the code i actually think it is intended to work like this at least for unicode i guess you are better off using what qjuh suggested since this one will not help you with unicode emojis
2Flow2
2Flow2OP13mo ago
How should I implement what qjuh suggested? I couldn't quite figure it out...
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
2Flow2
2Flow2OP13mo ago
Wow, that worked like a charm, thanks! I'm honestly quite surprised this isn't something built into the default libraries that Discord.js comes with..
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
2Flow2
2Flow2OP13mo ago
Right? Ah well, thanks for the help!
Want results from more Discord servers?
Add your server