我正在制作一个不和谐的机器人,我希望该机器人发送用户发送的表情符号的图像或gif,但我似乎无法弄清楚如何检测表情符号是否为动画。< / p>
var emoji1 = args[0].split(':')
var emojiID = emoji1[2].split('>')
const emoji = bot.emojis.get(emojiID);
if (emoji.animated == true) {
message.channel.send({
file: "https://cdn.discordapp.com/emojis/" + emojiID[0] + ".gif"
});
} else {
message.channel.send({
file: "https://cdn.discordapp.com/emojis/" + emojiID[0] + ".png"
});
}
但是当我运行它时我得到一个错误
Cannot read property 'animated' of undefined
答案 0 :(得分:0)
您应该尝试做
const emoji = client.emojis.find(emoji => emoji.name === `${emoji1}`);```
答案 1 :(得分:0)
我建议为此使用Regex
。 Here是一个在线编辑器,可以帮助您构建正则表达式
var emojiId = args.join(" ").match(/(?<=<a?:.*:)\d*(?=>)/);
const emoji = bot.emojis.get(emojiID[0]);
message.channel.send({
file: "https://cdn.discordapp.com/emojis/" + emojiID[0] + emoji.animated
? ".gif"
: ".png"
});
答案 2 :(得分:0)
const emoji = args[0];
if(!emoji) return message.reply('Please define emoji to search for.');
const emojiID = emoji.replace(/[^0-9]+/g, ''); //Using RegEx remove everything that isn't a number.
const fetch = bot.emojis.get(emojiID); //Grab the emoji
if(fetch.animated == true){ //If it's aniamted.
message.channel.send({file: `https://cdn.discordapp.com/emojis/${emojiID}.gif`});
}else if(fetch.animated == false){ //If it isn't
message.channel.send({file: `https://cdn.discordapp.com/emojis/${emojiID}.png`});
}else{ //If emoji isn't found
return message.reply('Emoji not found. :(')
}
这是我使用的: 对于替换功能:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 对于嵌套的if语句: https://www.w3schools.com/js/js_if_else.asp
享受<3