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();
}
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;
}
}
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;
}
}
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();
}
}
}
}
}
Aggregations