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
- 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- For unicode emojis (twemoji): learn more
- Custom emojis:
/<?(a)?:?(\w{2,32}):(\d{17,19})>?/
learn moreI looked at both of those docs before posting this question, but I wasn't quite sure how to use them.
there is also an utility for parsing emojis that you might like
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 😅you import it directly from discord.js
So something like
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...what version of discord.js is this
I'm still receiving
parseEmoji is not defined
on the line that I call it.
Lemme check to make sure.make sure you save the code and restart the process
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.
no need to apologize
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?uhm
let me check
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?
I see
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.
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
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?
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
How should I implement what qjuh suggested? I couldn't quite figure it out...
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
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•14mo ago
Message Not Public
Sign In & Join Server To View
Right?
Ah well, thanks for the help!