【问题标题】:Using a custom emoji based on the user presence status in a MessageEmbed在 MessageEmbed 中使用基于用户存在状态的自定义表情符号
【发布时间】:2018-12-14 16:04:52
【问题描述】:

为我的机器人创建一个 userinfo 命令,但我似乎在使用自定义表情符号时遇到了一些麻烦,这些表情符号会根据用户的存在而不同地显示。 (在线、离开、免打扰、离线)。我很好奇这是如何实现的。有人有什么建议吗?

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    如果您尝试根据用户的存在显示不同的表情符号,您可以尝试以下代码:

    //user.presence.status returns the current status of a user
    if (message.author.presence.status == 'online') {
        message.channel.send(':green_heart:');//Online
    }else if (message.author.presence.status == 'idle') {
        message.channel.send(':yellow_heart:');//Away
    }else if (message.author.presence.status == 'dnd') {
        message.channel.send(':heart:');//Do not disturb
    }else{//Skipped the conditional because the only remaining status is offline
        message.channel.send(':black_heart:');//Offline
    }
    

    文档:https://discord.js.org/#/docs/main/stable/class/Presence

    编辑 抱歉,我没有考虑使用开关/外壳。有时使用 else/if 会更快,但您仍然可以使用 switch/case 来提高可读性。

    switch (message.author.presence.status) {
        case 'online':
            message.channel.send(':green_heart:');
            break;
        case 'idle':
            message.channel.send(':yellow_heart:');
            break;
        case 'dnd':
            message.channel.send(':heart:');
            break;
        case 'offline':
            message.channel.send(':black_heart:');
            break;
    }
    

    【讨论】:

      【解决方案2】:

      I:检查是否存在

      你可以通过简单地检查User.presence.status来做到这一点,可以是"online""idle""dnd""offline"

      II:使用自定义表情符号

      要使用custom emoji,您需要知道其名称或 ID(如果可能,ID 更好,您可以通过编写表情符号、在其前添加 \ 并发送消息来获取)

      let emoji = guild.emojis.get(your_emoji_id_as_a_string); //if you have the id OR
      let emoji = guild.emojis.find("name", your_name_as_a_string) //if you have the name (but if you change this in the server it won't work)
      //create your embed like a normal one and when you have to use an emoji, just type it like a variable
      let str = `The user is ${emoji}`; //== "The user is " + emoji
      

      【讨论】:

        猜你喜欢
        • 2021-11-23
        • 1970-01-01
        • 2020-02-28
        • 1970-01-01
        • 2019-04-25
        • 2020-11-17
        • 2021-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多