问题
Making a userinfo command for my bot, but I seem to be running into some trouble with using custom emojis that will display differently depending on the user's presence. (Online, Away, DND, Offline). I'm curious as to how this could be accompolished. Anyone have any tips?
回答1:
I: checking for the presence
You can do this by simply checking User.presence.status, that can be either "online", "idle", "dnd" or "offline"
II: using custom emojis
To use a custom emoji you need to know its name or its id (if possible the id is better, you can get it by writing the emoji, adding a \ before it & sending the message)
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
回答2:
If you are trying to display different emojis depending on the user's presence you can try this code:
//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
}
Docs: https://discord.js.org/#/docs/main/stable/class/Presence
EDIT Sorry, I didn't consider using a switch/case. Sometimes using else/if is faster, but you can still use switch/case for readability.
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;
}
来源:https://stackoverflow.com/questions/51203742/using-a-custom-emoji-based-on-the-user-presence-status-in-a-messageembed