Search in sources :

Example 1 with Message

use of github.scarsz.discordsrv.dependencies.jda.api.entities.Message in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class OutboundToDiscordEvents method onAdvancementSend.

@Subscribe(priority = ListenerPriority.HIGHEST)
public void onAdvancementSend(AchievementMessagePostProcessEvent event) {
    if (event.isCancelled()) {
        return;
    }
    Debug.debug("Triggered onAdvancementSend");
    Message message = event.getDiscordMessage();
    if (!message.getContentRaw().contains("<ICA=")) {
        return;
    }
    String text = message.getContentRaw();
    Set<Integer> matches = new LinkedHashSet<>();
    synchronized (RESEND_WITH_ATTACHMENT) {
        for (int key : RESEND_WITH_ATTACHMENT.keySet()) {
            if (text.contains("<ICA=" + key + ">")) {
                matches.add(key);
            }
        }
    }
    event.setCancelled(true);
    DiscordMessageContent content = new DiscordMessageContent(message);
    for (int key : matches) {
        AttachmentData data = RESEND_WITH_ATTACHMENT.remove(key);
        if (data != null) {
            content.addAttachment(data.getName(), data.getData());
        }
    }
    TextChannel destinationChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(event.getChannel());
    Debug.debug("onAdvancementSend sending message to discord");
    if (event.isUsingWebhooks()) {
        String webHookUrl = WebhookUtil.getWebhookUrlToUseForChannel(destinationChannel);
        WebhookClient client = WebhookClient.withUrl(webHookUrl);
        if (client == null) {
            throw new NullPointerException("Unable to get the Webhook client URL for the TextChannel " + destinationChannel.getName());
        }
        client.send(content.toWebhookMessageBuilder().setUsername(event.getWebhookName()).setAvatarUrl(event.getWebhookAvatarUrl()).build());
        client.close();
    } else {
        content.toJDAMessageRestAction(destinationChannel).queue();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TextChannel(github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel) WebhookClient(club.minnced.discord.webhook.WebhookClient) Message(github.scarsz.discordsrv.dependencies.jda.api.entities.Message) DiscordMessageContent(com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent) AttachmentData(com.loohp.interactivechatdiscordsrvaddon.objectholders.AttachmentData) Subscribe(github.scarsz.discordsrv.api.Subscribe)

Example 2 with Message

use of github.scarsz.discordsrv.dependencies.jda.api.entities.Message in project Foundation by kangarko.

the class DiscordListener method flashMessage.

/**
 * Send a message to the given channel for four seconds
 *
 * @param channel
 * @param message
 */
protected final void flashMessage(TextChannel channel, String message) {
    final String finalMessage = Common.stripColors(message);
    Common.runAsync(() -> {
        final Message sentMessage = channel.sendMessage(finalMessage).complete();
        Common.runLaterAsync(4 * 20, () -> {
            try {
                channel.deleteMessageById(sentMessage.getIdLong()).complete();
            } catch (final github.scarsz.discordsrv.dependencies.jda.api.exceptions.ErrorResponseException ex) {
                // Silence if deleted already
                if (!ex.getMessage().contains("Unknown Message"))
                    ex.printStackTrace();
            }
        });
    });
}
Also used : ErrorResponseException(github.scarsz.discordsrv.dependencies.jda.api.exceptions.ErrorResponseException) Message(github.scarsz.discordsrv.dependencies.jda.api.entities.Message)

Example 3 with Message

use of github.scarsz.discordsrv.dependencies.jda.api.entities.Message in project Foundation by kangarko.

the class DiscordListener method editMessageById.

/**
 * Edit the given message by ID
 *
 * @param channel
 * @param messageId
 * @param newMessage
 */
protected final void editMessageById(TextChannel channel, long messageId, String newMessage) {
    Common.runAsync(() -> {
        try {
            final Message message = channel.retrieveMessageById(messageId).complete();
            if (message != null) {
                // Remove old message
                channel.deleteMessageById(messageId).complete();
                // Send a new one
                final Message newSentMessage = channel.sendMessage(message.getAuthor().getName() + ": " + newMessage.replace("*", "\\*").replace("_", "\\_").replace("@", "\\@")).complete();
                this.editedMessages.put(messageId, newSentMessage.getIdLong());
            }
        } catch (final Throwable t) {
            if (!t.toString().contains("Unknown Message"))
                t.printStackTrace();
        }
    });
}
Also used : Message(github.scarsz.discordsrv.dependencies.jda.api.entities.Message)

Example 4 with Message

use of github.scarsz.discordsrv.dependencies.jda.api.entities.Message in project Foundation by kangarko.

the class DiscordListener method removeAndWarn.

/**
 * Removes the given message and sends a warning message from the bot
 * shown for the given duration in seconds and then remove it.
 *
 * @param message
 * @param warningMessage
 * @param warningDurationSeconds how long to show the warning message
 */
protected final void removeAndWarn(Message message, String warningMessage, int warningDurationSeconds) {
    message.delete().complete();
    final MessageChannel channel = message.getChannel();
    final Message channelWarningMessage = channel.sendMessage(warningMessage).complete();
    channel.deleteMessageById(channelWarningMessage.getIdLong()).completeAfter(warningDurationSeconds, TimeUnit.SECONDS);
}
Also used : MessageChannel(github.scarsz.discordsrv.dependencies.jda.api.entities.MessageChannel) Message(github.scarsz.discordsrv.dependencies.jda.api.entities.Message)

Example 5 with Message

use of github.scarsz.discordsrv.dependencies.jda.api.entities.Message in project Foundation by kangarko.

the class DiscordSender method sendMessage.

@Override
public void sendMessage(String message) {
    final String finalMessage = Common.stripColors(message);
    Common.runAsync(() -> {
        final Message sentMessage = channel.sendMessage(finalMessage).complete();
        try {
            // Automatically remove after a short while
            channel.deleteMessageById(sentMessage.getIdLong()).completeAfter(4, TimeUnit.SECONDS);
        } catch (final Throwable t) {
            // Ignore already deleted messages
            if (!t.toString().contains("Unknown Message"))
                t.printStackTrace();
        }
    });
}
Also used : Message(github.scarsz.discordsrv.dependencies.jda.api.entities.Message)

Aggregations

Message (github.scarsz.discordsrv.dependencies.jda.api.entities.Message)9 TextChannel (github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel)5 Subscribe (github.scarsz.discordsrv.api.Subscribe)4 ArrayList (java.util.ArrayList)3 Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)2 ClickEvent (com.loohp.interactivechat.libs.net.kyori.adventure.text.event.ClickEvent)2 HoverEvent (com.loohp.interactivechat.libs.net.kyori.adventure.text.event.HoverEvent)2 LegacyComponentSerializer (com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer)2 ICPlayer (com.loohp.interactivechat.objectholders.ICPlayer)2 ComponentReplacing (com.loohp.interactivechat.utils.ComponentReplacing)2 CustomStringUtils (com.loohp.interactivechat.utils.CustomStringUtils)2 InteractiveChatDiscordSrvAddon (com.loohp.interactivechatdiscordsrvaddon.InteractiveChatDiscordSrvAddon)2 DiscordSRV (github.scarsz.discordsrv.DiscordSRV)2 Guild (github.scarsz.discordsrv.dependencies.jda.api.entities.Guild)2 MessageEmbed (github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed)2 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2