在 MessageEmbed 中使用基于用户在线状态的自定义表情符号

Using a custom emoji based on the user presence status in a MessageEmbed

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

如果你想根据用户的存在来显示不同的表情符号,你可以试试这个代码:

//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

编辑 抱歉,我没有考虑使用 switch/case。有时使用 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;
}

I: 检查是否存在

您只需勾选 User.presence.status 即可,它可以是 "online""idle""dnd""offline"

二:使用自定义表情符号

要使用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