-
-
Notifications
You must be signed in to change notification settings - Fork 734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added handling for relationship presence updates #494
Changes from all commits
1a0c156
90da256
1def5e6
3356d7e
b87c29b
4138cf2
5c16d83
3ad9b4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
*/ | ||
package net.dv8tion.jda.core.handle; | ||
|
||
import net.dv8tion.jda.client.JDAClient; | ||
import net.dv8tion.jda.client.entities.impl.FriendImpl; | ||
import net.dv8tion.jda.core.AccountType; | ||
import net.dv8tion.jda.core.OnlineStatus; | ||
import net.dv8tion.jda.core.entities.Game; | ||
import net.dv8tion.jda.core.entities.impl.*; | ||
|
@@ -37,12 +40,21 @@ public PresenceUpdateHandler(JDAImpl api) | |
@Override | ||
protected Long handleInternally(JSONObject content) | ||
{ | ||
//Do a pre-check to see if this is for a Guild, and if it is, if the guild is currently locked. | ||
if (content.has("guild_id")) | ||
GuildImpl guild = null; | ||
//Do a pre-check to see if this is for a Guild, and if it is, if the guild is currently locked or not cached. | ||
if (!content.isNull("guild_id")) | ||
{ | ||
final long guildId = content.getLong("guild_id"); | ||
if (api.getGuildLock().isLocked(guildId)) | ||
return guildId; | ||
guild = (GuildImpl) api.getGuildById(guildId); | ||
if (guild == null) | ||
{ | ||
api.getEventCache().cache(EventCache.Type.GUILD, guildId, () -> handle(responseNumber, allContent)); | ||
EventCache.LOG.debug("Received a PRESENCE_UPDATE for a guild that is not yet cached! " + | ||
"GuildId: " + guildId + " UserId: " + content.getJSONObject("user").get("id")); | ||
return null; | ||
} | ||
} | ||
|
||
JSONObject jsonUser = content.getJSONObject("user"); | ||
|
@@ -74,7 +86,7 @@ protected Long handleInternally(JSONObject content) | |
user, oldUsername, oldDiscriminator)); | ||
} | ||
String oldAvatar = user.getAvatarId(); | ||
if (!(avatarId == null && oldAvatar == null) && !Objects.equals(avatarId, oldAvatar)) | ||
if (!Objects.equals(avatarId, oldAvatar)) | ||
{ | ||
String oldAvatarId = user.getAvatarId(); | ||
user.setAvatarId(avatarId); | ||
|
@@ -90,31 +102,29 @@ protected Long handleInternally(JSONObject content) | |
String gameName = null; | ||
String gameUrl = null; | ||
Game.GameType type = null; | ||
if ( !content.isNull("game") && !content.getJSONObject("game").isNull("name") ) | ||
final JSONObject game = content.optJSONObject("game"); | ||
if (game != null && !game.isNull("name")) | ||
{ | ||
gameName = content.getJSONObject("game").get("name").toString(); | ||
gameUrl = ( content.getJSONObject("game").isNull("url") ? null : content.getJSONObject("game").get("url").toString() ); | ||
gameName = game.get("name").toString(); | ||
gameUrl = game.isNull("url") ? null : game.get("url").toString(); | ||
try | ||
{ | ||
type = content.getJSONObject("game").isNull("type") | ||
type = game.isNull("type") | ||
? Game.GameType.DEFAULT | ||
: Game.GameType.fromKey(Integer.parseInt(content.getJSONObject("game").get("type").toString())); | ||
: Game.GameType.fromKey(Integer.parseInt(game.get("type").toString())); | ||
} | ||
catch (NumberFormatException ex) | ||
{ | ||
type = Game.GameType.DEFAULT; | ||
} | ||
} | ||
Game nextGame = (gameName == null | ||
? null | ||
: api.getEntityBuilder().createGame(gameName, gameUrl, type)); | ||
Game nextGame = gameName == null ? null : api.getEntityBuilder().createGame(gameName, gameUrl, type); | ||
OnlineStatus status = OnlineStatus.fromKey(content.getString("status")); | ||
|
||
//If we are in a Guild, then we will use Member. | ||
// If we aren't we'll be dealing with the Relation system. | ||
if (content.has("guild_id")) | ||
if (guild != null) | ||
{ | ||
GuildImpl guild = (GuildImpl) api.getGuildById(content.getLong("guild_id")); | ||
MemberImpl member = (MemberImpl) guild.getMember(user); | ||
|
||
//If the Member is null, then User isn't in the Guild. | ||
|
@@ -144,7 +154,7 @@ protected Long handleInternally(JSONObject content) | |
api, responseNumber, | ||
user, guild, oldStatus)); | ||
} | ||
if(member.getGame() == null ? nextGame != null : !member.getGame().equals(nextGame)) | ||
if (!Objects.equals(member.getGame(), nextGame)) | ||
{ | ||
Game oldGame = member.getGame(); | ||
member.setGame(nextGame); | ||
|
@@ -158,7 +168,32 @@ protected Long handleInternally(JSONObject content) | |
else | ||
{ | ||
//In this case, this PRESENCE_UPDATE is for a Relation. | ||
if (api.getAccountType() != AccountType.CLIENT) | ||
return null; | ||
JDAClient client = api.asClient(); | ||
FriendImpl friend = (FriendImpl) client.getFriendById(userId); | ||
|
||
if (friend != null) | ||
{ | ||
if (!friend.getOnlineStatus().equals(status)) | ||
{ | ||
OnlineStatus oldStatus = friend.getOnlineStatus(); | ||
friend.setOnlineStatus(status); | ||
api.getEventManager().handle( | ||
new UserOnlineStatusUpdateEvent( | ||
api, responseNumber, | ||
user, null, oldStatus)); | ||
} | ||
if (!Objects.equals(friend.getGame(), nextGame)) | ||
{ | ||
Game oldGame = friend.getGame(); | ||
friend.setGame(nextGame); | ||
api.getEventManager().handle( | ||
new UserGameUpdateEvent( | ||
api, responseNumber, | ||
user, null, oldGame)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be firing FriendXEvents, not UserXEvents I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or, at a minimum, take a Friend object to these User events so that you can call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is already supported in message-rw changes: https://github.com/DV8FromTheWorld/JDA/pull/453/files#diff-b7dbbd6a832a00b7f5e6173633fefff6 |
||
} | ||
} | ||
} | ||
} | ||
else | ||
|
@@ -173,19 +208,10 @@ protected Long handleInternally(JSONObject content) | |
|
||
//If the OnlineStatus is OFFLINE, ignore the event and return. | ||
OnlineStatus status = OnlineStatus.fromKey(content.getString("status")); | ||
if (status == OnlineStatus.OFFLINE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We lost the ability to return in an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate? Not sure what you mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind. I see what has changed here. The fast escape logic is no longer needed. |
||
return null; | ||
|
||
//If this was for a Guild, cache it in the Guild for later use in GUILD_MEMBER_ADD | ||
if (content.has("guild_id")) | ||
{ | ||
GuildImpl guild = (GuildImpl) api.getGuildById(content.getLong("guild_id")); | ||
if (status != OnlineStatus.OFFLINE && guild != null) | ||
guild.getCachedPresenceMap().put(userId, content); | ||
} | ||
else | ||
{ | ||
//cache in relationship stuff | ||
} | ||
} | ||
return null; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't
game.getInt("type")
be the same here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the past we've encountered numerous issues with game objects due to them not being checked by the API, I didn't touch that logic at all here.