【问题标题】:How to test if an emoji is animated in discord.js如何在 discord.js 中测试表情符号是否为动画
【发布时间】:2020-01-11 11:50:00
【问题描述】:

我正在制作一个不和谐的机器人,我希望该机器人发送用户发送的表情符号的图像或 gif,但我似乎无法弄清楚如何检测表情符号是否为动画。

        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

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    你应该尝试做

    const emoji = client.emojis.find(emoji => emoji.name === `${emoji1}`);```
    

    【讨论】:

      【解决方案2】:

      我建议改用RegexHere 是一个在线编辑器,可以帮助你构建正则表达式

          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"
          });
      

      【讨论】:

        【解决方案3】:
          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

        享受

        【讨论】:

          猜你喜欢
          • 2020-05-05
          • 1970-01-01
          • 2020-07-09
          • 2020-09-10
          • 2019-02-04
          • 2014-08-30
          • 2016-12-30
          • 2016-07-24
          • 1970-01-01
          相关资源
          最近更新 更多