Search in sources :

Example 1 with Webhook

use of net.dv8tion.jda.core.entities.Webhook in project DiscordSRV by Scarsz.

the class WebhookUtil method deliverMessage.

public static void deliverMessage(TextChannel channel, Player player, String message) {
    if (channel == null)
        return;
    Webhook targetWebhook = getWebhookToUseForChannel(channel, player.getUniqueId().toString());
    if (targetWebhook == null)
        return;
    new Thread(() -> {
        try {
            HttpResponse<String> response = Unirest.post(targetWebhook.getUrl()).field("content", message).field("username", DiscordUtil.strip(player.getDisplayName())).field("avatar_url", "https://minotar.net/helm/" + player.getName() + "/100.png").asString();
            DiscordSRV.debug("Received API response for webhook message delivery: " + response.getStatus());
        } catch (Exception e) {
            DiscordSRV.error("Failed to deliver webhook message to Discord: " + e.getMessage());
            DiscordSRV.debug(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }
    }).start();
}
Also used : HttpResponse(com.mashape.unirest.http.HttpResponse) Webhook(net.dv8tion.jda.core.entities.Webhook)

Example 2 with Webhook

use of net.dv8tion.jda.core.entities.Webhook in project DiscordSRV by Scarsz.

the class WebhookUtil method getWebhookToUseForChannel.

public static Webhook getWebhookToUseForChannel(TextChannel channel, String targetName) {
    synchronized (lastUsedWebhooks) {
        List<Webhook> webhooks = new ArrayList<>();
        channel.getGuild().getWebhooks().complete().stream().filter(webhook -> webhook.getName().startsWith("DiscordSRV #" + channel.getName() + " #")).forEach(webhooks::add);
        if (webhooks.size() != 2) {
            webhooks.forEach(webhook -> webhook.delete().reason("Purging orphaned webhook").queue());
            webhooks.clear();
            if (!channel.getGuild().getMember(channel.getJDA().getSelfUser()).hasPermission(Permission.MANAGE_WEBHOOKS)) {
                DiscordSRV.error("Can't create a webhook to deliver chat message, bot is missing permission \"Manage Webhooks\"");
                return null;
            }
            // create webhooks to use
            Webhook webhook1 = createWebhook(channel.getGuild(), channel, "DiscordSRV " + channel.getId() + " #1");
            Webhook webhook2 = createWebhook(channel.getGuild(), channel, "DiscordSRV " + channel.getId() + " #2");
            if (webhook1 == null || webhook2 == null)
                return null;
            webhooks.add(webhook1);
            webhooks.add(webhook2);
        }
        LastWebhookInfo info = lastUsedWebhooks.getOrDefault(channel, null);
        Webhook target;
        if (info == null) {
            target = webhooks.get(0);
            lastUsedWebhooks.put(channel, new LastWebhookInfo(target.getId(), targetName));
            return target;
        }
        target = info.targetName.equals(targetName) ? webhooks.get(0).getId().equals(info.webhook) ? webhooks.get(0) : webhooks.get(1) : webhooks.get(0).getId().equals(info.webhook) ? webhooks.get(0) : webhooks.get(1);
        lastUsedWebhooks.put(channel, new LastWebhookInfo(target.getId(), targetName));
        return target;
    }
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) HashMap(java.util.HashMap) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Webhook(net.dv8tion.jda.core.entities.Webhook) Guild(net.dv8tion.jda.core.entities.Guild) Unirest(com.mashape.unirest.http.Unirest) List(java.util.List) Permission(net.dv8tion.jda.core.Permission) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) HttpResponse(com.mashape.unirest.http.HttpResponse) Map(java.util.Map) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) ArrayList(java.util.ArrayList) Webhook(net.dv8tion.jda.core.entities.Webhook)

Example 3 with Webhook

use of net.dv8tion.jda.core.entities.Webhook in project DiscordSRV by Scarsz.

the class WebhookUtil method createWebhook.

public static Webhook createWebhook(Guild guild, TextChannel channel, String name) {
    try {
        Webhook webhook = channel.createWebhook(name).complete();
        DiscordSRV.debug("Created webhook " + webhook.getName() + " to deliver messages to text channel #" + channel.getName());
        return webhook;
    } catch (Exception e) {
        DiscordSRV.error("Failed to create webhook " + name + " for message delivery: " + e.getMessage());
        return null;
    }
}
Also used : Webhook(net.dv8tion.jda.core.entities.Webhook)

Example 4 with Webhook

use of net.dv8tion.jda.core.entities.Webhook in project Rubicon by Rubicon-Bot.

the class PortalListener method onGuildMessageReceived.

@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
    if (!e.getAuthor().isBot()) {
        if (e.getChannel().getId().equals(RubiconBot.getMySQL().getPortalValue(e.getGuild(), "channelid"))) {
            String status = RubiconBot.getMySQL().getGuildValue(e.getGuild(), "portal");
            if (status.contains("open")) {
                TextChannel otherChannel = e.getJDA().getTextChannelById(RubiconBot.getMySQL().getPortalValue(e.getJDA().getGuildById(RubiconBot.getMySQL().getPortalValue(e.getGuild(), "partnerid")), "channelid"));
                try {
                    Webhook webhook = null;
                    for (Webhook hook : otherChannel.getWebhooks().complete()) {
                        if (hook.getName().equals("rubicon-portal-hook")) {
                            webhook = hook;
                            break;
                        }
                    }
                    if (webhook == null) {
                        webhook = otherChannel.createWebhook("rubicon-portal-hook").complete();
                    }
                    WebhookClientBuilder clientBuilder = webhook.newClient();
                    WebhookClient client = clientBuilder.build();
                    WebhookMessageBuilder builder = new WebhookMessageBuilder();
                    builder.setContent(e.getMessage().getContentDisplay().replace("@here", "@ here").replace("@everyone", "@ everyone"));
                    builder.setAvatarUrl(e.getAuthor().getAvatarUrl());
                    builder.setUsername(e.getAuthor().getName());
                    WebhookMessage message = builder.build();
                    client.send(message);
                    client.close();
                /*EmbedBuilder builder = new EmbedBuilder();
                        builder.setAuthor(e.getAuthor().getName(), null, e.getAuthor().getEffectiveAvatarUrl());
                        builder.setDescription(e.getMessage().getContent());
                        otherChannel.sendMessage(builder.build()).queue();*/
                } catch (NullPointerException fuck) {
                    fuck.printStackTrace();
                }
            }
        }
    }
}
Also used : WebhookMessage(net.dv8tion.jda.webhook.WebhookMessage) WebhookClientBuilder(net.dv8tion.jda.webhook.WebhookClientBuilder) TextChannel(net.dv8tion.jda.core.entities.TextChannel) WebhookClient(net.dv8tion.jda.webhook.WebhookClient) WebhookMessageBuilder(net.dv8tion.jda.webhook.WebhookMessageBuilder) Webhook(net.dv8tion.jda.core.entities.Webhook)

Aggregations

Webhook (net.dv8tion.jda.core.entities.Webhook)4 HttpResponse (com.mashape.unirest.http.HttpResponse)2 TextChannel (net.dv8tion.jda.core.entities.TextChannel)2 Unirest (com.mashape.unirest.http.Unirest)1 DiscordSRV (github.scarsz.discordsrv.DiscordSRV)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Permission (net.dv8tion.jda.core.Permission)1 Guild (net.dv8tion.jda.core.entities.Guild)1 WebhookClient (net.dv8tion.jda.webhook.WebhookClient)1 WebhookClientBuilder (net.dv8tion.jda.webhook.WebhookClientBuilder)1 WebhookMessage (net.dv8tion.jda.webhook.WebhookMessage)1 WebhookMessageBuilder (net.dv8tion.jda.webhook.WebhookMessageBuilder)1 ExceptionUtils (org.apache.commons.lang3.exception.ExceptionUtils)1 Player (org.bukkit.entity.Player)1