use of org.javacord.core.entity.server.ServerImpl in project Javacord by BtoBastian.
the class GuildUpdateHandler method handle.
@Override
public void handle(JsonNode packet) {
if (packet.has("unavailable") && packet.get("unavailable").asBoolean()) {
return;
}
long id = packet.get("id").asLong();
api.getPossiblyUnreadyServerById(id).map(server -> (ServerImpl) server).ifPresent(server -> {
long oldApplicationId = server.getApplicationId().orElse(-1L);
long newApplicationId = packet.hasNonNull("application_id") ? packet.get("application_id").asLong() : -1L;
if (oldApplicationId != newApplicationId) {
server.setApplicationId(newApplicationId);
}
String newName = packet.get("name").asText();
String oldName = server.getName();
if (!Objects.deepEquals(oldName, newName)) {
server.setName(newName);
ServerChangeNameEvent event = new ServerChangeNameEventImpl(server, newName, oldName);
api.getEventDispatcher().dispatchServerChangeNameEvent(server, server, event);
}
String newIconHash = packet.get("icon").asText(null);
String oldIconHash = server.getIconHash();
if (!Objects.deepEquals(oldIconHash, newIconHash)) {
server.setIconHash(newIconHash);
ServerChangeIconEvent event = new ServerChangeIconEventImpl(server, newIconHash, oldIconHash);
api.getEventDispatcher().dispatchServerChangeIconEvent(server, server, event);
}
String newSplashHash = packet.get("splash").asText(null);
String oldSplashHash = server.getSplashHash();
if (!Objects.deepEquals(oldSplashHash, newSplashHash)) {
server.setSplashHash(newSplashHash);
ServerChangeSplashEvent event = new ServerChangeSplashEventImpl(server, newSplashHash, oldSplashHash);
api.getEventDispatcher().dispatchServerChangeSplashEvent(server, server, event);
}
VerificationLevel newVerificationLevel = VerificationLevel.fromId(packet.get("verification_level").asInt());
VerificationLevel oldVerificationLevel = server.getVerificationLevel();
if (newVerificationLevel != oldVerificationLevel) {
server.setVerificationLevel(newVerificationLevel);
ServerChangeVerificationLevelEvent event = new ServerChangeVerificationLevelEventImpl(server, newVerificationLevel, oldVerificationLevel);
api.getEventDispatcher().dispatchServerChangeVerificationLevelEvent(server, server, event);
}
Region newRegion = Region.getRegionByKey(packet.get("region").asText());
Region oldRegion = server.getRegion();
if (oldRegion != newRegion) {
server.setRegion(newRegion);
ServerChangeRegionEvent event = new ServerChangeRegionEventImpl(server, newRegion, oldRegion);
api.getEventDispatcher().dispatchServerChangeRegionEvent(server, server, event);
}
DefaultMessageNotificationLevel newDefaultMessageNotificationLevel = DefaultMessageNotificationLevel.fromId(packet.get("default_message_notifications").asInt());
DefaultMessageNotificationLevel oldDefaultMessageNotificationLevel = server.getDefaultMessageNotificationLevel();
if (newDefaultMessageNotificationLevel != oldDefaultMessageNotificationLevel) {
server.setDefaultMessageNotificationLevel(newDefaultMessageNotificationLevel);
ServerChangeDefaultMessageNotificationLevelEvent event = new ServerChangeDefaultMessageNotificationLevelEventImpl(server, newDefaultMessageNotificationLevel, oldDefaultMessageNotificationLevel);
api.getEventDispatcher().dispatchServerChangeDefaultMessageNotificationLevelEvent(server, server, event);
}
long newOwnerId = packet.get("owner_id").asLong();
long oldOwnerId = server.getOwnerId();
if (newOwnerId != oldOwnerId) {
server.setOwnerId(newOwnerId);
ServerChangeOwnerEvent event = new ServerChangeOwnerEventImpl(server, newOwnerId, oldOwnerId);
api.getEventDispatcher().dispatchServerChangeOwnerEvent(server, server, event);
}
if (packet.has("system_channel_id")) {
ServerTextChannel newSystemChannel = packet.get("system_channel_id").isNull() ? null : server.getTextChannelById(packet.get("system_channel_id").asLong()).orElse(null);
ServerTextChannel oldSystemChannel = server.getSystemChannel().orElse(null);
if (oldSystemChannel != newSystemChannel) {
server.setSystemChannelId(newSystemChannel == null ? -1 : newSystemChannel.getId());
ServerChangeSystemChannelEvent event = new ServerChangeSystemChannelEventImpl(server, newSystemChannel, oldSystemChannel);
api.getEventDispatcher().dispatchServerChangeSystemChannelEvent(server, server, event);
}
}
if (packet.has("afk_channel_id")) {
ServerVoiceChannel newAfkChannel = packet.get("afk_channel_id").isNull() ? null : server.getVoiceChannelById(packet.get("afk_channel_id").asLong()).orElse(null);
ServerVoiceChannel oldAfkChannel = server.getAfkChannel().orElse(null);
if (oldAfkChannel != newAfkChannel) {
server.setAfkChannelId(newAfkChannel == null ? -1 : newAfkChannel.getId());
ServerChangeAfkChannelEvent event = new ServerChangeAfkChannelEventImpl(server, newAfkChannel, oldAfkChannel);
api.getEventDispatcher().dispatchServerChangeAfkChannelEvent(server, server, event);
}
}
int newAfkTimeout = packet.get("afk_timeout").asInt();
int oldAfkTimeout = server.getAfkTimeoutInSeconds();
if (oldAfkTimeout != newAfkTimeout) {
server.setAfkTimeout(newAfkTimeout);
ServerChangeAfkTimeoutEvent event = new ServerChangeAfkTimeoutEventImpl(server, newAfkTimeout, oldAfkTimeout);
api.getEventDispatcher().dispatchServerChangeAfkTimeoutEvent(server, server, event);
}
ExplicitContentFilterLevel newExplicitContentFilterLevel = ExplicitContentFilterLevel.fromId(packet.get("explicit_content_filter").asInt());
ExplicitContentFilterLevel oldExplicitContentFilterLevel = server.getExplicitContentFilterLevel();
if (oldExplicitContentFilterLevel != newExplicitContentFilterLevel) {
server.setExplicitContentFilterLevel(newExplicitContentFilterLevel);
ServerChangeExplicitContentFilterLevelEvent event = new ServerChangeExplicitContentFilterLevelEventImpl(server, newExplicitContentFilterLevel, oldExplicitContentFilterLevel);
api.getEventDispatcher().dispatchServerChangeExplicitContentFilterLevelEvent(server, server, event);
}
MultiFactorAuthenticationLevel newMultiFactorAuthenticationLevel = MultiFactorAuthenticationLevel.fromId(packet.get("mfa_level").asInt());
MultiFactorAuthenticationLevel oldMultiFactorAuthenticationLevel = server.getMultiFactorAuthenticationLevel();
if (oldMultiFactorAuthenticationLevel != newMultiFactorAuthenticationLevel) {
server.setMultiFactorAuthenticationLevel(newMultiFactorAuthenticationLevel);
ServerChangeMultiFactorAuthenticationLevelEvent event = new ServerChangeMultiFactorAuthenticationLevelEventImpl(server, newMultiFactorAuthenticationLevel, oldMultiFactorAuthenticationLevel);
api.getEventDispatcher().dispatchServerChangeMultiFactorAuthenticationLevelEvent(server, server, event);
}
if (packet.has("rules_channel_id")) {
ServerTextChannel newRulesChannel = packet.get("rules_channel_id").isNull() ? null : server.getTextChannelById(packet.get("rules_channel_id").asLong()).orElse(null);
ServerTextChannel oldRulesChannel = server.getRulesChannel().orElse(null);
if (oldRulesChannel != newRulesChannel) {
server.setRulesChannelId(newRulesChannel == null ? -1 : newRulesChannel.getId());
ServerChangeRulesChannelEvent event = new ServerChangeRulesChannelEventImpl(server, newRulesChannel, oldRulesChannel);
api.getEventDispatcher().dispatchServerChangeRulesChannelEvent(server, server, event);
}
}
if (packet.has("public_updates_channel_id")) {
ServerTextChannel newModeratorsOnlyChannel = packet.get("public_updates_channel_id").isNull() ? null : server.getTextChannelById(packet.get("public_updates_channel_id").asLong()).orElse(null);
ServerTextChannel oldModeratorsOnlyChannel = server.getModeratorsOnlyChannel().orElse(null);
if (oldModeratorsOnlyChannel != newModeratorsOnlyChannel) {
server.setModeratorsOnlyChannelId(newModeratorsOnlyChannel == null ? -1 : newModeratorsOnlyChannel.getId());
ServerChangeModeratorsOnlyChannelEvent event = new ServerChangeModeratorsOnlyChannelEventImpl(server, newModeratorsOnlyChannel, oldModeratorsOnlyChannel);
api.getEventDispatcher().dispatchServerChangeModeratorsOnlyChannelEvent(server, server, event);
}
}
BoostLevel oldBoostLevel = server.getBoostLevel();
BoostLevel newBoostLevel = BoostLevel.fromId(packet.get("premium_tier").asInt());
if (oldBoostLevel != newBoostLevel) {
server.setBoostLevel(newBoostLevel);
ServerChangeBoostLevelEvent event = new ServerChangeBoostLevelEventImpl(server, newBoostLevel, oldBoostLevel);
api.getEventDispatcher().dispatchServerChangeBoostLevelEvent(server, server, event);
}
NsfwLevel oldNsfwLevel = server.getNsfwLevel();
NsfwLevel newNsfwLevel = NsfwLevel.fromId(packet.get("nsfw_level").asInt());
if (oldNsfwLevel != newNsfwLevel) {
server.setNsfwLevel(newNsfwLevel);
ServerChangeNsfwLevelEvent event = new ServerChangeNsfwLevelEventImpl(server, newNsfwLevel, oldNsfwLevel);
api.getEventDispatcher().dispatchServerChangeNsfwLevelEvent(server, server, event);
}
Locale newPreferredLocale = new Locale.Builder().setLanguageTag(packet.get("preferred_locale").asText()).build();
Locale oldPreferredLocale = server.getPreferredLocale();
if (!oldPreferredLocale.equals(newPreferredLocale)) {
server.setPreferredLocale(newPreferredLocale);
ServerChangePreferredLocaleEvent event = new ServerChangePreferredLocaleEventImpl(server, newPreferredLocale, oldPreferredLocale);
api.getEventDispatcher().dispatchServerChangePreferredLocaleEvent(server, server, event);
}
int oldBoostCount = server.getBoostCount();
int newBoostCount = packet.has("premium_subscription_count") ? packet.get("premium_subscription_count").asInt() : 0;
if (oldBoostCount != newBoostCount) {
server.setServerBoostCount(newBoostCount);
ServerChangeBoostCountEvent event = new ServerChangeBoostCountEventImpl(server, newBoostCount, oldBoostCount);
api.getEventDispatcher().dispatchServerChangeBoostCountEvent(server, server, event);
}
String oldDescription = server.getDescription().isPresent() ? server.getDescription().get() : null;
String newDescription = packet.hasNonNull("description") ? packet.get("description").asText() : null;
if (!Objects.deepEquals(oldDescription, newDescription)) {
server.setDescription(newDescription);
ServerChangeDescriptionEvent event = new ServerChangeDescriptionEventImpl(server, newDescription, oldDescription);
api.getEventDispatcher().dispatchServerChangeDescriptionEvent(server, server, event);
}
String newDiscoverySplashHash = packet.get("discovery_splash").asText(null);
String oldDiscoverySplashHash = server.getDiscoverySplashHash();
if (!Objects.deepEquals(oldDiscoverySplashHash, newDiscoverySplashHash)) {
server.setDiscoverySplashHash(newDiscoverySplashHash);
ServerChangeDiscoverySplashEvent event = new ServerChangeDiscoverySplashEventImpl(server, newDiscoverySplashHash, oldDiscoverySplashHash);
api.getEventDispatcher().dispatchServerChangeDiscoverySplashEvent(server, server, event);
}
String oldVanityCode = server.getVanityUrlCode().map(VanityUrlCode::getCode).orElse(null);
String newVanityCode = packet.hasNonNull("vanity_url_code") ? packet.get("vanity_url_code").asText() : null;
if (!Objects.deepEquals(oldVanityCode, newVanityCode)) {
server.setVanityUrlCode(new VanityUrlCodeImpl(packet.get("vanity_url_code").asText()));
ServerChangeVanityUrlCodeEvent event = new ServerChangeVanityUrlCodeEventImpl(server, newVanityCode, oldVanityCode);
api.getEventDispatcher().dispatchServerChangeVanityUrlCodeEvent(server, server, event);
}
Collection<ServerFeature> oldServerFeature = server.getFeatures();
Collection<ServerFeature> newServerFeature = new ArrayList<>();
if (packet.has("features")) {
packet.get("features").forEach(jsonNode -> {
try {
newServerFeature.add(ServerFeature.valueOf(jsonNode.asText()));
} catch (Exception ignored) {
logger.debug("Encountered server with unknown feature {}. Please update to the latest " + "Javacord version or create an issue on the Javacord GitHub page if you are already " + "on the latest version.", jsonNode.asText());
}
});
}
if (!(oldServerFeature.containsAll(newServerFeature) && newServerFeature.containsAll(oldServerFeature))) {
server.setServerFeatures(newServerFeature);
ServerChangeServerFeaturesEvent event = new ServerChangeServerFeaturesEventImpl(server, newServerFeature, oldServerFeature);
api.getEventDispatcher().dispatchServerChangeServerFeaturesEvent(server, server, event);
}
if (packet.has("system_channel_flags")) {
server.setSystemChannelFlag(packet.get("system_channel_flags").asInt());
}
});
}
use of org.javacord.core.entity.server.ServerImpl in project Javacord by BtoBastian.
the class MessageReactionAddHandler method handle.
@Override
public void handle(JsonNode packet) {
long channelId = packet.get("channel_id").asLong();
long messageId = packet.get("message_id").asLong();
long userId = packet.get("user_id").asLong();
String serverId = packet.hasNonNull("guild_id") ? packet.get("guild_id").asText() : null;
TextChannel channel;
if (serverId == null) {
// if private channel:
channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, userId, null);
} else {
channel = api.getTextChannelById(channelId).orElse(null);
}
if (channel == null) {
LoggerUtil.logMissingChannel(logger, channelId);
return;
}
Optional<Server> server = api.getServerById(serverId);
Member member = null;
if (packet.hasNonNull("member") && server.isPresent()) {
member = new MemberImpl(api, (ServerImpl) server.get(), packet.get("member"), null);
}
Optional<Message> message = api.getCachedMessageById(messageId);
Emoji emoji;
JsonNode emojiJson = packet.get("emoji");
if (!emojiJson.has("id") || emojiJson.get("id").isNull()) {
emoji = UnicodeEmojiImpl.fromString(emojiJson.get("name").asText());
} else {
emoji = api.getKnownCustomEmojiOrCreateCustomEmoji(emojiJson);
}
message.ifPresent(msg -> ((MessageImpl) msg).addReaction(emoji, userId == api.getYourself().getId()));
ReactionAddEvent event = new ReactionAddEventImpl(api, messageId, channel, emoji, userId, member);
api.getEventDispatcher().dispatchReactionAddEvent(server.map(DispatchQueueSelector.class::cast).orElse(api), messageId, server.orElse(null), channel, userId, event);
}
use of org.javacord.core.entity.server.ServerImpl in project Javacord by BtoBastian.
the class InteractionCreateHandler method handle.
@Override
public void handle(JsonNode packet) {
TextChannel channel = null;
if (packet.hasNonNull("channel_id")) {
long channelId = packet.get("channel_id").asLong();
// Check if this interaction comes from a guild or a DM
if (packet.hasNonNull("guild_id")) {
channel = api.getTextChannelById(channelId).orElse(null);
} else {
UserImpl user = new UserImpl(api, packet.get("user"), (MemberImpl) null, null);
channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, user.getId(), user);
}
}
int typeId = packet.get("type").asInt();
final InteractionType interactionType = InteractionType.fromValue(typeId);
ComponentType componentType = null;
InteractionImpl interaction;
switch(interactionType) {
case APPLICATION_COMMAND:
int applicationCommandTypeId = packet.get("data").get("type").asInt();
ApplicationCommandType applicationCommandType = ApplicationCommandType.fromValue(applicationCommandTypeId);
switch(applicationCommandType) {
case SLASH:
interaction = new SlashCommandInteractionImpl(api, channel, packet);
break;
case USER:
interaction = new UserContextMenuInteractionImpl(api, channel, packet);
break;
case MESSAGE:
interaction = new MessageContextMenuInteractionImpl(api, channel, packet);
break;
default:
logger.info("Got application command interaction of unknown type <{}>. " + "Please contact the developer!", applicationCommandTypeId);
return;
}
break;
case MESSAGE_COMPONENT:
int componentTypeId = packet.get("data").get("component_type").asInt();
componentType = ComponentType.fromId(componentTypeId);
switch(componentType) {
case BUTTON:
interaction = new ButtonInteractionImpl(api, channel, packet);
break;
case ACTION_ROW:
logger.warn("Received a message component interaction of type ACTION_ROW. This should not" + " be possible.");
return;
case SELECT_MENU:
interaction = new SelectMenuInteractionImpl(api, channel, packet);
break;
default:
logger.warn("Received message component interaction of unknown type <{}>. " + "Please contact the developer!", componentTypeId);
return;
}
break;
case APPLICATION_COMMAND_AUTOCOMPLETE:
interaction = new AutocompleteInteractionImpl(api, channel, packet);
break;
default:
logger.warn("Received interaction of unknown type <{}>. " + "Please contact the developer!", typeId);
return;
}
InteractionCreateEvent event = new InteractionCreateEventImpl(interaction);
ServerImpl server = (ServerImpl) interaction.getServer().orElse(null);
api.getEventDispatcher().dispatchInteractionCreateEvent(server == null ? api : server, server, interaction.getChannel().orElse(null), interaction.getUser(), event);
switch(interactionType) {
case APPLICATION_COMMAND:
int applicationCommandTypeId = packet.get("data").get("type").asInt();
ApplicationCommandType applicationCommandType = ApplicationCommandType.fromValue(applicationCommandTypeId);
switch(applicationCommandType) {
case SLASH:
SlashCommandCreateEvent slashCommandCreateEvent = new SlashCommandCreateEventImpl(interaction);
api.getEventDispatcher().dispatchSlashCommandCreateEvent(server == null ? api : server, server, interaction.getChannel().orElse(null), interaction.getUser(), slashCommandCreateEvent);
break;
case USER:
UserContextMenuCommandEvent userContextMenuCommandEvent = new UserContextMenuCommandEventImpl(interaction);
api.getEventDispatcher().dispatchUserContextMenuCommandEvent(server, server, interaction.getChannel().orElse(null), interaction.getUser(), userContextMenuCommandEvent);
break;
case MESSAGE:
MessageContextMenuCommandEvent messageContextMenuCommandEvent = new MessageContextMenuCommandEventImpl(interaction);
api.getEventDispatcher().dispatchMessageContextMenuCommandEvent(server, interaction.asMessageContextMenuInteraction().orElseThrow(AssertionError::new).getTarget().getId(), server, interaction.getChannel().orElse(null), interaction.getUser(), messageContextMenuCommandEvent);
break;
default:
logger.info("Got application command interaction of unknown type <{}>. " + "Please contact the developer!", applicationCommandTypeId);
return;
}
break;
case MESSAGE_COMPONENT:
MessageComponentCreateEvent messageComponentCreateEvent = new MessageComponentCreateEventImpl(interaction);
long messageId = messageComponentCreateEvent.getMessageComponentInteraction().getMessage().getId();
api.getEventDispatcher().dispatchMessageComponentCreateEvent(server == null ? api : server, messageId, server, interaction.getChannel().orElse(null), interaction.getUser(), messageComponentCreateEvent);
switch(componentType) {
case BUTTON:
ButtonClickEvent buttonClickEvent = new ButtonClickEventImpl(interaction);
api.getEventDispatcher().dispatchButtonClickEvent(server == null ? api : server, messageId, server, interaction.getChannel().orElse(null), interaction.getUser(), buttonClickEvent);
break;
case SELECT_MENU:
SelectMenuChooseEvent selectMenuChooseEvent = new SelectMenuChooseEventImpl(interaction);
api.getEventDispatcher().dispatchSelectMenuChooseEvent(server == null ? api : server, messageId, server, interaction.getChannel().orElse(null), interaction.getUser(), selectMenuChooseEvent);
break;
default:
break;
}
break;
case APPLICATION_COMMAND_AUTOCOMPLETE:
AutocompleteCreateEvent autocompleteCreateEvent = new AutocompleteCreateEventImpl(interaction);
api.getEventDispatcher().dispatchAutocompleteCreateEvent(server == null ? api : server, server, interaction.getChannel().orElse(null), interaction.getUser(), autocompleteCreateEvent);
break;
default:
break;
}
}
use of org.javacord.core.entity.server.ServerImpl in project Javacord by BtoBastian.
the class TypingStartHandler method handle.
@Override
public void handle(JsonNode packet) {
long userId = packet.get("user_id").asLong();
long channelId = packet.get("channel_id").asLong();
TextChannel channel = api.getTextChannelById(channelId).orElse(null);
ServerImpl server = null;
if (packet.hasNonNull("guild_id")) {
long serverId = packet.get("guild_id").asLong();
server = (ServerImpl) api.getPossiblyUnreadyServerById(serverId).orElseThrow(AssertionError::new);
}
MemberImpl member = null;
if (packet.hasNonNull("member") && server != null) {
member = new MemberImpl(api, server, packet.get("member"), null);
}
if (channel != null) {
UserStartTypingEvent event = new UserStartTypingEventImpl(channel, userId, member);
api.getEventDispatcher().dispatchUserStartTypingEvent(server != null ? server : api, server, channel, userId, event);
}
}
use of org.javacord.core.entity.server.ServerImpl in project Javacord by BtoBastian.
the class ThreadUpdateHandler method handleThread.
private void handleThread(final JsonNode jsonChannel) {
final long channelId = jsonChannel.get("id").asLong();
final long serverId = jsonChannel.get("guild_id").asLong();
final ServerImpl server = api.getServerById(serverId).map(ServerImpl.class::cast).orElse(null);
if (server == null) {
logger.warn("Unable to find server with id {}", serverId);
return;
}
final ServerThreadChannelImpl thread = (ServerThreadChannelImpl) server.getOrCreateServerThreadChannel(jsonChannel);
// Handling whether the name has changed
final String oldName = thread.getName();
final String newName = jsonChannel.get("name").asText();
if (!Objects.deepEquals(oldName, newName)) {
thread.setName(newName);
final ServerChannelChangeNameEvent event = new ServerChannelChangeNameEventImpl(thread, newName, oldName);
if (server.isReady()) {
api.getEventDispatcher().dispatchServerChannelChangeNameEvent((DispatchQueueSelector) thread.getServer(), thread.getServer(), thread, event);
}
}
}
Aggregations