use of org.javacord.api.entity.user.User in project Javacord by BtoBastian.
the class GuildMemberRemoveHandler method handle.
@Override
public void handle(JsonNode packet) {
api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(server -> (ServerImpl) server).ifPresent(server -> {
User user = new UserImpl(api, packet.get("user"), (MemberImpl) null, server);
server.removeMember(user.getId());
server.decrementMemberCount();
ServerMemberLeaveEvent event = new ServerMemberLeaveEventImpl(server, user);
api.getEventDispatcher().dispatchServerMemberLeaveEvent(server, server, user, event);
});
}
use of org.javacord.api.entity.user.User in project Javacord by BtoBastian.
the class DiscordApi method makeMentionsReadable.
/**
* Gets the readable content of the string, which replaces all mentions etc. with the actual name.
* The replacement happens as following:
* <ul>
* <li><b>User mentions</b>:
* <code>@nickname</code> if the user has a nickname, <code>@name</code> if the user has no nickname, unchanged if
* the user is not in the cache.
* <li><b>Role mentions</b>:
* <code>@name</code> if the role exists in the server, otherwise <code>#deleted-role</code>
* <li><b>Channel mentions</b>:
* <code>#name</code> if the text channel exists in the server, otherwise <code>#deleted-channel</code>
* <li><b>Custom emoji</b>:
* <code>:name:</code>. If the emoji is known, the real name is used, otherwise the name from the mention tag.
* </ul>
*
* @param content The string to strip the mentions away.
* @param server The server to get the display name of users from.
*
* @return The readable content of the string.
*/
default String makeMentionsReadable(String content, Server server) {
Matcher userMention = DiscordRegexPattern.USER_MENTION.matcher(content);
while (userMention.find()) {
String userId = userMention.group("id");
Optional<User> userOptional = getCachedUserById(userId);
if (userOptional.isPresent()) {
User user = userOptional.get();
String userName = server == null ? user.getName() : server.getDisplayName(user);
content = userMention.replaceFirst(Matcher.quoteReplacement("@" + userName));
userMention.reset(content);
}
}
Matcher roleMention = DiscordRegexPattern.ROLE_MENTION.matcher(content);
while (roleMention.find()) {
String roleName = getRoleById(roleMention.group("id")).map(Role::getName).orElse("deleted-role");
content = roleMention.replaceFirst(Matcher.quoteReplacement("@" + roleName));
roleMention.reset(content);
}
Matcher channelMention = DiscordRegexPattern.CHANNEL_MENTION.matcher(content);
while (channelMention.find()) {
String channelId = channelMention.group("id");
String channelName = getServerChannelById(channelId).map(ServerChannel::getName).orElse("deleted-channel");
content = channelMention.replaceFirst("#" + channelName);
channelMention.reset(content);
}
Matcher customEmoji = DiscordRegexPattern.CUSTOM_EMOJI.matcher(content);
while (customEmoji.find()) {
String emojiId = customEmoji.group("id");
String name = getCustomEmojiById(emojiId).map(CustomEmoji::getName).orElseGet(() -> customEmoji.group("name"));
content = customEmoji.replaceFirst(":" + name + ":");
customEmoji.reset(content);
}
return ESCAPED_CHARACTER.matcher(content).replaceAll("${char}");
}
use of org.javacord.api.entity.user.User in project Javacord by BtoBastian.
the class DiscordWebSocketAdapter method sendVoiceStateUpdate.
/**
* Sends the voice state update packet.
*
* @param server The server to send the voice state update for. Can be {@code null} if {@code channel} is given.
* @param channel The channel to connect to or {@code null} to disconnect from voice.
* @param selfMuted Whether to self-mute on the given server. If {@code null}, current state remains unchanged.
* @param selfDeafened Whether to self-deafen on the given server. If {@code null}, current state remains unchanged.
*/
public void sendVoiceStateUpdate(Server server, ServerVoiceChannel channel, Boolean selfMuted, Boolean selfDeafened) {
ObjectNode updateVoiceStatePacket = JsonNodeFactory.instance.objectNode().put("op", GatewayOpcode.VOICE_STATE_UPDATE.getCode());
if (server == null) {
if (channel == null) {
throw new IllegalArgumentException("Either server or channel must be given");
}
server = channel.getServer();
}
User yourself = api.getYourself();
updateVoiceStatePacket.putObject("d").put("guild_id", server.getIdAsString()).put("channel_id", (channel == null) ? null : channel.getIdAsString()).put("self_mute", (selfMuted == null) ? server.isSelfMuted(yourself) : selfMuted).put("self_deaf", (selfDeafened == null) ? server.isSelfDeafened(yourself) : selfDeafened);
logger.debug("Sending VOICE_STATE_UPDATE packet for {} on {}", channel, server);
sendTextFrame(updateVoiceStatePacket.toString());
}
use of org.javacord.api.entity.user.User in project Javacord by BtoBastian.
the class ChannelUpdateHandler method dispatchServerChannelChangeOverwrittenPermissionsEvent.
/**
* Dispatches a ServerChannelChangeOverwrittenPermissionsEvent.
*
* @param channel The channel of the event.
* @param newPermissions The new overwritten permissions.
* @param oldPermissions The old overwritten permissions.
* @param entityId The id of the entity.
* @param entity The entity of the event.
*/
private void dispatchServerChannelChangeOverwrittenPermissionsEvent(ServerChannel channel, Permissions newPermissions, Permissions oldPermissions, long entityId, DiscordEntity entity) {
if (newPermissions.equals(oldPermissions)) {
// any of its values. We don't need to dispatch an event for this.
return;
}
ServerChannelChangeOverwrittenPermissionsEvent event = new ServerChannelChangeOverwrittenPermissionsEventImpl(channel, newPermissions, oldPermissions, entityId, entity);
api.getEventDispatcher().dispatchServerChannelChangeOverwrittenPermissionsEvent((DispatchQueueSelector) channel.getServer(), (entity instanceof Role) ? (Role) entity : null, channel.getServer(), channel, (entity instanceof User) ? (User) entity : null, event);
}
use of org.javacord.api.entity.user.User in project Javacord by BtoBastian.
the class GuildBanRemoveHandler method handle.
@Override
public void handle(JsonNode packet) {
api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(server -> (ServerImpl) server).ifPresent(server -> {
User user = new UserImpl(api, packet.get("user"), (MemberImpl) null, server);
ServerMemberUnbanEvent event = new ServerMemberUnbanEventImpl(server, user);
api.getEventDispatcher().dispatchServerMemberUnbanEvent(server, server, user, event);
});
}
Aggregations