Check if a custom emoji is a valid emoji my bot can use

Hello, I'm trying to get my bot to check it the emoji code a user enters is a valid emoji that the bot can use in a button or not but I have no idea how to do that. Does someone have a solution?
11 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs3y ago
Tag suggestion for @ncls.: • For unicode emojis (twemoji): learn more • Custom emojis: /<?(a)?:?(\w{2,32}):(\d{17,19})>?/ learn more
Idris
Idris3y ago
check if it matches this regex
ncls.
ncls.OP3y ago
I think I implemented it wrong because I have no idea of RegEx. Could you take a look at this please?
function checkDiscordEmoji(emoji) {
const regex = new RegExp("<?(a)?:?(\w{2,32}):(\d{17,19})>?")
return regex.test(emoji)
}
function checkDiscordEmoji(emoji) {
const regex = new RegExp("<?(a)?:?(\w{2,32}):(\d{17,19})>?")
return regex.test(emoji)
}
Idris
Idris3y ago
I don't know if your code is wrong or not but I'd do
function checkDiscordEmoji(emoji) {
const id = emoji.match(/<?(a)?:?(\w{2,32}):(\d{17,19})>?/)[3];
if (id) return id
return "The provided emoji is invalid."
}
function checkDiscordEmoji(emoji) {
const id = emoji.match(/<?(a)?:?(\w{2,32}):(\d{17,19})>?/)[3];
if (id) return id
return "The provided emoji is invalid."
}
ncls.
ncls.OP3y ago
It works but it crashes when I put in a normal emoji or just text Says this:
const id = emoji.match(/<?(a)?:?(\w{2,32}):(\d{17,19})>?/)[3]
^
TypeError: Cannot read properties of null (reading '3')
const id = emoji.match(/<?(a)?:?(\w{2,32}):(\d{17,19})>?/)[3]
^
TypeError: Cannot read properties of null (reading '3')
Idris
Idris3y ago
this is what I do for my bot
ncls.
ncls.OP3y ago
That works way better but still crashes when you enter <noemoji It's fixable with a try catch but now I'm facing the issue that it thinks that "<😂" is a valid emoji
Idris
Idris3y ago
that regex is only for custom emojis not defaults one
ncls.
ncls.OP3y ago
Oh, I mixed things up. My function for checking normal emojis is just wrong because it just checks if a normal emoji is included. That's what passes the input. Not the custom emoji function. Thanks a lot for your help!
Idris
Idris3y ago
no prob
Want results from more Discord servers?
Add your server