public WebHookDto getDtoForView(long guildId, WebHook webHook) { WebHookDto hookDto = mapper.getWebHookDto(webHook); if (discordService.isConnected()) { JDA jda = discordService.getJda(); Guild guild = jda.getGuildById(guildId); if (guild != null && guild.getSelfMember().hasPermission(Permission.MANAGE_WEBHOOKS)) { hookDto.setAvailable(true); Webhook webhook = webHookService.getWebHook(guild, webHook); if (webhook != null) { hookDto.setChannelId(webhook.getChannel().getIdLong()); } else { hookDto.setEnabled(false); } } } return hookDto; }
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(); }
public boolean delete(long guildId, WebHook webHook) { if (discordService.isConnected()) { JDA jda = discordService.getJda(); Guild guild = jda.getGuildById(guildId); if (guild != null && guild.getSelfMember().hasPermission(Permission.MANAGE_WEBHOOKS)) { Webhook webhook = getWebHook(guild, webHook); if (webhook != null) { webhook.delete().queue(); return true; } } } return false; }
@Override protected void handleResponse(Response response, Request<Webhook> request) { if (!response.isOk()) { request.onFailure(response); return; } JSONObject json = response.getObject(); Webhook webhook = api.getEntityBuilder().createWebhook(json); request.onSuccess(webhook); }
@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(); } } } } }
@ParametersAreNonnullByDefault public List<Webhook> load(Guild guild) { return guild.getWebhooks().complete(); }
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.getName() + " #1"); Webhook webhook2 = createWebhook(channel.getGuild(), channel, "DiscordSRV #" + channel.getName() + " #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; } }
public static Webhook createWebhook(Guild guild, TextChannel channel, String name) { try { Webhook createdWebhook = guild.getController().createWebhook(channel, name).complete(); DiscordSRV.debug("Created webhook " + createdWebhook.getName() + " to deliver messages to text channel #" + channel.getName()); return createdWebhook; } catch (Exception e) { DiscordSRV.error("Failed to create webhook " + name + " for message delivery: " + e.getMessage()); return null; } }
/** * Creates new {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients} and adds them * to this cluster. * <br>The {@link net.dv8tion.jda.webhook.WebhookClientBuilder WebhookClientBuilders} * will be supplied with the default settings of this cluster. * * @param webhooks * Webhooks to target (duplicates will not be filtered) * * @throws java.lang.IllegalArgumentException * If the provided array or any of the contained * webhooks is {@code null} * * @return The current WebhookCluster for chaining convenience * * @see #buildWebhooks(Webhook...) * @see #newBuilder(Webhook) */ public WebhookCluster buildWebhooks(Webhook... webhooks) { Checks.notNull(webhooks, "Webhooks"); for (Webhook webhook : webhooks) { Checks.notNull(webhook, "Webhook"); buildWebhook(webhook.getIdLong(), webhook.getToken()); } return this; }
/** * Creates new {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients} and adds them * to this cluster. * <br>The {@link net.dv8tion.jda.webhook.WebhookClientBuilder WebhookClientBuilders} * will be supplied with the default settings of this cluster. * * @param webhooks * Webhooks to target (duplicates will not be filtered) * * @throws java.lang.IllegalArgumentException * If the provided collection or any of the contained * webhooks is {@code null} * * @return The current WebhookCluster for chaining convenience * * @see #buildWebhooks(Webhook...) * @see #newBuilder(Webhook) */ public WebhookCluster buildWebhooks(Collection<Webhook> webhooks) { Checks.notNull(webhooks, "Webhooks"); for (Webhook webhook : webhooks) { Checks.notNull(webhook, "Webhook"); buildWebhook(webhook.getIdLong(), webhook.getToken()); } return this; }
/** * Creates a new {@link net.dv8tion.jda.webhook.WebhookClientBuilder WebhookClientBuilder} * with the defined default settings of this cluster. * * @param webhook * The target webhook * * @throws java.lang.IllegalArgumentException * If the webhook is {@code null} * * @return The WebhookClientBuilder with default settings * * @see net.dv8tion.jda.webhook.WebhookClientBuilder#WebhookClientBuilder(Webhook) new WebhookClientBuilder(Webhook) */ public WebhookClientBuilder newBuilder(Webhook webhook) { Checks.notNull(webhook, "Webhook"); return newBuilder(webhook.getIdLong(), webhook.getToken()); }
/** * Creates a new WebhookClientBuilder with the provided id and token * * @param webhook * The target {@link net.dv8tion.jda.core.entities.Webhook Webhook} * * @throws java.lang.NullPointerException * If the provided {@link net.dv8tion.jda.core.entities.Webhook Webhook} is {@code null} */ public WebhookClientBuilder(@Nonnull Webhook webhook) { this(webhook.getIdLong(), webhook.getToken()); }
/** * Creates a new WebhookManager instance * * @param webhook * The target {@link net.dv8tion.jda.core.entities.Webhook Webhook} to modify */ public WebhookManager(Webhook webhook) { this.manager = new WebhookManagerUpdatable(webhook); }
/** * The target {@link net.dv8tion.jda.core.entities.Webhook Webhook} * that will be modified by this manager * * @return The target {@link net.dv8tion.jda.core.entities.Webhook Webhook} */ public Webhook getWebhook() { return manager.getWebhook(); }
Webhook getWebHook(Guild guild, WebHook webHook);