【发布时间】:2018-12-14 16:04:52
【问题描述】:
为我的机器人创建一个 userinfo 命令,但我似乎在使用自定义表情符号时遇到了一些麻烦,这些表情符号会根据用户的存在而不同地显示。 (在线、离开、免打扰、离线)。我很好奇这是如何实现的。有人有什么建议吗?
【问题讨论】:
标签: javascript node.js discord.js
为我的机器人创建一个 userinfo 命令,但我似乎在使用自定义表情符号时遇到了一些麻烦,这些表情符号会根据用户的存在而不同地显示。 (在线、离开、免打扰、离线)。我很好奇这是如何实现的。有人有什么建议吗?
【问题讨论】:
标签: javascript node.js discord.js
如果您尝试根据用户的存在显示不同的表情符号,您可以尝试以下代码:
//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;
}
【讨论】:
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
【讨论】: