Search in sources :

Example 1 with MessageAction

use of net.dv8tion.jda.api.requests.restaction.MessageAction in project DiscordSRV by Scarsz.

the class DiscordUtil method sendMessageBlocking.

/**
 * Send the given message to the given channel, blocking the thread's execution until it's successfully sent then returning it
 * @param channel The channel to send the message to
 * @param message The message to send to the channel
 * @param allowMassPing Whether or not to allow mass pings like @everyone
 * @return The sent message
 */
public static Message sendMessageBlocking(TextChannel channel, Message message, boolean allowMassPing) {
    if (getJda() == null) {
        DiscordSRV.debug("Tried sending a message when JDA was null");
        return null;
    }
    if (channel == null) {
        DiscordSRV.debug("Tried sending a message to a null channel");
        return null;
    }
    if (message == null || StringUtils.isBlank(message.getContentRaw())) {
        DiscordSRV.debug("Tried sending a null or blank message");
        return null;
    }
    Message sentMessage;
    try {
        MessageAction action = channel.sendMessage(message);
        if (allowMassPing)
            action = action.allowedMentions(EnumSet.allOf(Message.MentionType.class));
        sentMessage = action.complete();
    } catch (PermissionException e) {
        if (e.getPermission() != Permission.UNKNOWN) {
            DiscordSRV.warning("Could not send message in channel " + channel + " because the bot does not have the \"" + e.getPermission().getName() + "\" permission");
        } else {
            DiscordSRV.warning("Could not send message in channel " + channel + " because \"" + e.getMessage() + "\"");
        }
        return null;
    }
    DiscordSRV.api.callEvent(new DiscordGuildMessageSentEvent(getJda(), sentMessage));
    return sentMessage;
}
Also used : PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) DiscordGuildMessageSentEvent(github.scarsz.discordsrv.api.events.DiscordGuildMessageSentEvent) MessageAction(net.dv8tion.jda.api.requests.restaction.MessageAction)

Example 2 with MessageAction

use of net.dv8tion.jda.api.requests.restaction.MessageAction in project DiscordSRV by Scarsz.

the class DiscordUtil method queueMessage.

/**
 * Send the given message to the given channel, optionally doing something with the message via the given consumer
 * @param channel The channel to send the message to
 * @param message The message to send to the channel
 * @param consumer The consumer to handle the message
 */
public static void queueMessage(TextChannel channel, Message message, Consumer<Message> consumer, boolean allowMassPing) {
    if (channel == null) {
        DiscordSRV.debug("Tried sending a message to a null channel");
        return;
    }
    try {
        MessageAction action = channel.sendMessage(message);
        if (allowMassPing)
            action = action.allowedMentions(EnumSet.allOf(Message.MentionType.class));
        action.queue(sentMessage -> {
            DiscordSRV.api.callEvent(new DiscordGuildMessageSentEvent(getJda(), sentMessage));
            if (consumer != null)
                consumer.accept(sentMessage);
        }, throwable -> DiscordSRV.error("Failed to send message to channel " + channel + ": " + throwable.getMessage()));
    } catch (PermissionException e) {
        if (e.getPermission() != Permission.UNKNOWN) {
            DiscordSRV.warning("Could not send message in channel " + channel + " because the bot does not have the \"" + e.getPermission().getName() + "\" permission");
        } else {
            DiscordSRV.warning("Could not send message in channel " + channel + " because \"" + e.getMessage() + "\"");
        }
    } catch (IllegalStateException e) {
        DiscordSRV.error("Could not send message to channel " + channel + ": " + e.getMessage());
    }
}
Also used : PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) DiscordGuildMessageSentEvent(github.scarsz.discordsrv.api.events.DiscordGuildMessageSentEvent) MessageAction(net.dv8tion.jda.api.requests.restaction.MessageAction)

Aggregations

DiscordGuildMessageSentEvent (github.scarsz.discordsrv.api.events.DiscordGuildMessageSentEvent)2 PermissionException (net.dv8tion.jda.api.exceptions.PermissionException)2 MessageAction (net.dv8tion.jda.api.requests.restaction.MessageAction)2