/
GuildRoleUpdateHandler.java
144 lines (133 loc) · 5.39 KB
/
GuildRoleUpdateHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.internal.handle;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.RoleIcon;
import net.dv8tion.jda.api.events.role.update.*;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.entities.GuildImpl;
import net.dv8tion.jda.internal.entities.RoleImpl;
import java.util.Objects;
public class GuildRoleUpdateHandler extends SocketHandler
{
public GuildRoleUpdateHandler(JDAImpl api)
{
super(api);
}
@Override
protected Long handleInternally(DataObject content)
{
final long guildId = content.getLong("guild_id");
if (getJDA().getGuildSetupController().isLocked(guildId))
return guildId;
DataObject rolejson = content.getObject("role");
GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
if (guild == null)
{
getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
EventCache.LOG.debug("Received a Role Update for a Guild that is not yet cached: {}", content);
return null;
}
final long roleId = rolejson.getLong("id");
RoleImpl role = (RoleImpl) guild.getRolesView().get(roleId);
if (role == null)
{
getJDA().getEventCache().cache(EventCache.Type.ROLE, roleId, responseNumber, allContent, this::handle);
EventCache.LOG.debug("Received a Role Update for Role that is not yet cached: {}", content);
return null;
}
String name = rolejson.getString("name");
int color = rolejson.getInt("color");
if (color == 0)
color = Role.DEFAULT_COLOR_RAW;
int position = rolejson.getInt("position");
long permissions = rolejson.getLong("permissions");
boolean hoisted = rolejson.getBoolean("hoist");
boolean mentionable = rolejson.getBoolean("mentionable");
String iconId = rolejson.getString("icon", null);
String emoji = rolejson.getString("unicode_emoji", null);
rolejson.optObject("tags").ifPresent(role::setTags);
if (!Objects.equals(name, role.getName()))
{
String oldName = role.getName();
role.setName(name);
getJDA().handleEvent(
new RoleUpdateNameEvent(
getJDA(), responseNumber,
role, oldName));
}
if (color != role.getColorRaw())
{
int oldColor = role.getColorRaw();
role.setColor(color);
getJDA().handleEvent(
new RoleUpdateColorEvent(
getJDA(), responseNumber,
role, oldColor));
}
if (!Objects.equals(position, role.getPositionRaw()))
{
int oldPosition = role.getPosition();
int oldPositionRaw = role.getPositionRaw();
role.setRawPosition(position);
getJDA().handleEvent(
new RoleUpdatePositionEvent(
getJDA(), responseNumber,
role, oldPosition, oldPositionRaw));
}
if (!Objects.equals(permissions, role.getPermissionsRaw()))
{
long oldPermissionsRaw = role.getPermissionsRaw();
role.setRawPermissions(permissions);
getJDA().handleEvent(
new RoleUpdatePermissionsEvent(
getJDA(), responseNumber,
role, oldPermissionsRaw));
}
if (hoisted != role.isHoisted())
{
boolean wasHoisted = role.isHoisted();
role.setHoisted(hoisted);
getJDA().handleEvent(
new RoleUpdateHoistedEvent(
getJDA(), responseNumber,
role, wasHoisted));
}
if (mentionable != role.isMentionable())
{
boolean wasMentionable = role.isMentionable();
role.setMentionable(mentionable);
getJDA().handleEvent(
new RoleUpdateMentionableEvent(
getJDA(), responseNumber,
role, wasMentionable));
}
RoleIcon oldIcon = role.getIcon();
RoleIcon newIcon = iconId == null && emoji == null
? null
: new RoleIcon(iconId, emoji, roleId);
if (!Objects.equals(oldIcon, newIcon))
{
role.setIcon(newIcon);
getJDA().handleEvent(
new RoleUpdateIconEvent(
getJDA(), responseNumber,
role, oldIcon));
}
return null;
}
}