use of org.javacord.api.entity.channel.TextChannel in project Javacord by BtoBastian.
the class MessageBuilderBaseDelegateImpl method checkForAttachmentsAndExecuteRequest.
// //////////////////////////////////////////////////////////////////////////////
// Internal MessageBuilder utility methods
// //////////////////////////////////////////////////////////////////////////////
private CompletableFuture<Message> checkForAttachmentsAndExecuteRequest(TextChannel channel, ObjectNode body, RestRequest<Message> request, boolean clearAttachmentsIfAppropriate) {
if (attachments.isEmpty() && embeds.stream().noneMatch(EmbedBuilder::requiresAttachments)) {
if (clearAttachmentsIfAppropriate) {
body.set("attachments", JsonNodeFactory.instance.objectNode().arrayNode());
}
return executeRequestWithoutAttachments(channel, body, request);
}
CompletableFuture<Message> future = new CompletableFuture<>();
// We access files etc. so this should be async
channel.getApi().getThreadPool().getExecutorService().submit(() -> {
try {
List<FileContainer> tempAttachments = new ArrayList<>(attachments);
// Add the attachments required for the embeds
for (EmbedBuilder embed : embeds) {
tempAttachments.addAll(((EmbedBuilderDelegateImpl) embed.getDelegate()).getRequiredAttachments());
}
addMultipartBodyToRequest(request, body, tempAttachments, channel.getApi());
request.execute(result -> ((DiscordApiImpl) channel.getApi()).getOrCreateMessage(channel, result.getJsonBody())).whenComplete((newMessage, throwable) -> {
if (throwable != null) {
future.completeExceptionally(throwable);
} else {
future.complete(newMessage);
}
});
} catch (Throwable t) {
future.completeExceptionally(t);
}
});
return future;
}
use of org.javacord.api.entity.channel.TextChannel 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.api.entity.channel.TextChannel in project Javacord by BtoBastian.
the class MessageReactionRemoveHandler method handle.
@Override
public void handle(JsonNode packet) {
long messageId = packet.get("message_id").asLong();
long userId = packet.get("user_id").asLong();
Optional<Message> message = api.getCachedMessageById(messageId);
long channelId = packet.get("channel_id").asLong();
TextChannel channel;
if (packet.hasNonNull("guild_id")) {
channel = api.getTextChannelById(channelId).orElse(null);
} else {
// if private channel:
channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, userId, null);
}
if (channel == null) {
LoggerUtil.logMissingChannel(logger, channelId);
return;
}
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).removeReaction(emoji, userId == api.getYourself().getId()));
ReactionRemoveEvent event = new ReactionRemoveEventImpl(api, messageId, channel, emoji, userId);
Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
api.getEventDispatcher().dispatchReactionRemoveEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), messageId, optionalServer.orElse(null), channel, userId, event);
}
use of org.javacord.api.entity.channel.TextChannel 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.api.entity.channel.TextChannel in project Javacord by BtoBastian.
the class MessageDeleteBulkHandler method handle.
@Override
public void handle(JsonNode packet) {
long channelId = Long.parseLong(packet.get("channel_id").asText());
Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
if (optionalChannel.isPresent()) {
TextChannel channel = optionalChannel.get();
for (JsonNode messageIdJson : packet.get("ids")) {
long messageId = messageIdJson.asLong();
MessageDeleteEvent event = new MessageDeleteEventImpl(api, messageId, channel);
api.getCachedMessageById(messageId).ifPresent(((MessageCacheImpl) channel.getMessageCache())::removeMessage);
api.removeMessageFromCache(messageId);
Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
api.getEventDispatcher().dispatchMessageDeleteEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), messageId, optionalServer.orElse(null), channel, event);
}
} else {
LoggerUtil.logMissingChannel(logger, channelId);
}
}
Aggregations