add fiscord webhook
This commit is contained in:
parent
0fea0f4537
commit
5ece2192a2
8 changed files with 757 additions and 2 deletions
|
|
@ -0,0 +1,244 @@
|
|||
package com.loki.lochat.integrations;
|
||||
|
||||
import com.loki.lochat.util.FoliaUtil;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Интеграция с Discord через вебхуки
|
||||
*/
|
||||
public class DiscordIntegration {
|
||||
private final JavaPlugin plugin;
|
||||
private YamlConfiguration config;
|
||||
private DiscordWebhook webhook;
|
||||
private boolean enabled = false;
|
||||
|
||||
// Паттерны для очистки сообщений
|
||||
private static final Pattern MENTION_PATTERN = Pattern.compile("@(everyone|here)", Pattern.CASE_INSENSITIVE);
|
||||
private static final Pattern COLOR_CODE_PATTERN = Pattern.compile("§[0-9a-fk-or]", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
public DiscordIntegration(JavaPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
loadConfig();
|
||||
initWebhook();
|
||||
}
|
||||
|
||||
/**
|
||||
* Загрузить конфигурацию
|
||||
*/
|
||||
private void loadConfig() {
|
||||
File configFile = new File(plugin.getDataFolder(), "config/discord.yml");
|
||||
if (!configFile.exists()) {
|
||||
plugin.saveResource("config/discord.yml", false);
|
||||
}
|
||||
|
||||
config = YamlConfiguration.loadConfiguration(configFile);
|
||||
enabled = config.getBoolean("enabled", false);
|
||||
|
||||
if (!enabled) {
|
||||
plugin.getLogger().info("Discord integration disabled in config");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Инициализировать вебхук
|
||||
*/
|
||||
private void initWebhook() {
|
||||
if (!enabled) return;
|
||||
|
||||
String webhookUrl = config.getString("webhook.url", "");
|
||||
String username = config.getString("webhook.username", "Minecraft Server");
|
||||
String avatarUrl = config.getString("webhook.avatar_url", "https://mc-heads.net/avatar/minecraft/64");
|
||||
int timeout = config.getInt("performance.timeout", 10);
|
||||
int retryAttempts = config.getInt("performance.retry_attempts", 3);
|
||||
long retryDelay = config.getLong("performance.retry_delay", 1000);
|
||||
|
||||
webhook = new DiscordWebhook(plugin, webhookUrl, username, avatarUrl, timeout, retryAttempts, retryDelay);
|
||||
|
||||
if (!webhook.isValid()) {
|
||||
plugin.getLogger().severe("Invalid Discord webhook URL! Please check config/discord.yml");
|
||||
enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getLogger().info("Discord integration initialized successfully");
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправить сообщение чата в Discord
|
||||
*/
|
||||
public void sendChatMessage(Player player, String message, boolean isGlobal) {
|
||||
if (!enabled || !config.getBoolean("chat.enabled", true)) return;
|
||||
|
||||
// Проверяем фильтр глобального чата
|
||||
if (config.getBoolean("chat.global_only", true) && !isGlobal) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Фильтрация сообщения
|
||||
String cleanMessage = sanitizeMessage(message);
|
||||
if (cleanMessage.length() < config.getInt("filter.min_message_length", 1)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Обрезаем если слишком длинное
|
||||
int maxLength = config.getInt("filter.max_message_length", 2000);
|
||||
if (cleanMessage.length() > maxLength) {
|
||||
cleanMessage = cleanMessage.substring(0, maxLength - 3) + "...";
|
||||
}
|
||||
|
||||
String playerName = player.getName();
|
||||
String format = config.getString("chat.format", "**{player}**: {message}");
|
||||
String formattedMessage = format
|
||||
.replace("{player}", playerName)
|
||||
.replace("{message}", cleanMessage);
|
||||
|
||||
// Отправляем асинхронно
|
||||
if (config.getBoolean("performance.async", true)) {
|
||||
FoliaUtil.runAsync(plugin, () -> sendChatMessageInternal(formattedMessage, playerName, isGlobal));
|
||||
} else {
|
||||
sendChatMessageInternal(formattedMessage, playerName, isGlobal);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Внутренний метод отправки сообщения чата
|
||||
*/
|
||||
private void sendChatMessageInternal(String message, String playerName, boolean isGlobal) {
|
||||
if (config.getBoolean("chat.use_embed", true)) {
|
||||
String title = isGlobal ? "🌍 Глобальный чат" : "📍 Локальный чат";
|
||||
String color = config.getString("chat.embed_color", "5865F2");
|
||||
String thumbnail = "https://mc-heads.net/avatar/" + playerName + "/64";
|
||||
|
||||
webhook.sendEmbed(title, message, color, thumbnail);
|
||||
} else {
|
||||
webhook.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправить событие входа игрока
|
||||
*/
|
||||
public void sendPlayerJoin(Player player) {
|
||||
if (!enabled || !config.getBoolean("events.enabled", true) ||
|
||||
!config.getBoolean("events.join.enabled", true)) return;
|
||||
|
||||
String playerName = player.getName();
|
||||
String title = config.getString("events.join.title", "🟢 Игрок зашел на сервер");
|
||||
String description = config.getString("events.join.description", "**{player}** присоединился к серверу")
|
||||
.replace("{player}", playerName);
|
||||
String color = config.getString("events.join.color", "00FF00");
|
||||
String thumbnail = config.getString("events.join.thumbnail", "https://mc-heads.net/avatar/{player}/64")
|
||||
.replace("{player}", playerName);
|
||||
|
||||
if (config.getBoolean("performance.async", true)) {
|
||||
FoliaUtil.runAsync(plugin, () -> webhook.sendEmbed(title, description, color, thumbnail));
|
||||
} else {
|
||||
webhook.sendEmbed(title, description, color, thumbnail);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправить событие выхода игрока
|
||||
*/
|
||||
public void sendPlayerQuit(Player player) {
|
||||
if (!enabled || !config.getBoolean("events.enabled", true) ||
|
||||
!config.getBoolean("events.quit.enabled", true)) return;
|
||||
|
||||
String playerName = player.getName();
|
||||
String title = config.getString("events.quit.title", "🔴 Игрок покинул сервер");
|
||||
String description = config.getString("events.quit.description", "**{player}** покинул сервер")
|
||||
.replace("{player}", playerName);
|
||||
String color = config.getString("events.quit.color", "FF0000");
|
||||
String thumbnail = config.getString("events.quit.thumbnail", "https://mc-heads.net/avatar/{player}/64")
|
||||
.replace("{player}", playerName);
|
||||
|
||||
if (config.getBoolean("performance.async", true)) {
|
||||
FoliaUtil.runAsync(plugin, () -> webhook.sendEmbed(title, description, color, thumbnail));
|
||||
} else {
|
||||
webhook.sendEmbed(title, description, color, thumbnail);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправить событие смерти игрока
|
||||
*/
|
||||
public void sendPlayerDeath(Player player, String deathMessage) {
|
||||
if (!enabled || !config.getBoolean("events.enabled", true) ||
|
||||
!config.getBoolean("events.death.enabled", true)) return;
|
||||
|
||||
String playerName = player.getName();
|
||||
String title = config.getString("events.death.title", "💀 Смерть игрока");
|
||||
String description = config.getString("events.death.description", "**{player}** {death_message}")
|
||||
.replace("{player}", playerName)
|
||||
.replace("{death_message}", sanitizeMessage(deathMessage));
|
||||
String color = config.getString("events.death.color", "800080");
|
||||
String thumbnail = config.getString("events.death.thumbnail", "https://mc-heads.net/avatar/{player}/64")
|
||||
.replace("{player}", playerName);
|
||||
|
||||
if (config.getBoolean("performance.async", true)) {
|
||||
FoliaUtil.runAsync(plugin, () -> webhook.sendEmbed(title, description, color, thumbnail));
|
||||
} else {
|
||||
webhook.sendEmbed(title, description, color, thumbnail);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Очистить сообщение от нежелательных элементов
|
||||
*/
|
||||
private String sanitizeMessage(String message) {
|
||||
if (message == null) return "";
|
||||
|
||||
// Удаляем цветовые коды Minecraft
|
||||
message = COLOR_CODE_PATTERN.matcher(message).replaceAll("");
|
||||
|
||||
// Заменяем опасные упоминания
|
||||
if (config.getBoolean("filter.sanitize_mentions", true)) {
|
||||
message = MENTION_PATTERN.matcher(message).replaceAll("@\u200B$1"); // Добавляем невидимый символ
|
||||
}
|
||||
|
||||
// Экранируем Discord markdown
|
||||
message = message.replace("*", "\\*")
|
||||
.replace("_", "\\_")
|
||||
.replace("`", "\\`")
|
||||
.replace("~", "\\~");
|
||||
|
||||
return message.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Перезагрузить конфигурацию
|
||||
*/
|
||||
public void reload() {
|
||||
loadConfig();
|
||||
initWebhook();
|
||||
plugin.getLogger().info("Discord integration reloaded");
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправить тестовое сообщение
|
||||
*/
|
||||
public void sendTestMessage(String message, String senderName) {
|
||||
if (!enabled) return;
|
||||
|
||||
String title = "🧪 Тестовое сообщение";
|
||||
String description = "**Отправитель:** " + senderName + "\n**Сообщение:** " + message;
|
||||
String color = "FFD700"; // золотой цвет
|
||||
String thumbnail = "https://mc-heads.net/avatar/" + senderName + "/64";
|
||||
|
||||
webhook.sendEmbed(title, description, color, thumbnail);
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверить включена ли интеграция
|
||||
*/
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
}
|
||||
186
src/main/java/com/loki/lochat/integrations/DiscordWebhook.java
Normal file
186
src/main/java/com/loki/lochat/integrations/DiscordWebhook.java
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
package com.loki.lochat.integrations;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.loki.lochat.util.FoliaUtil;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* Класс для отправки сообщений в Discord через вебхуки
|
||||
*/
|
||||
public class DiscordWebhook {
|
||||
private final JavaPlugin plugin;
|
||||
private final String webhookUrl;
|
||||
private final String username;
|
||||
private final String avatarUrl;
|
||||
private final int timeout;
|
||||
private final int retryAttempts;
|
||||
private final long retryDelay;
|
||||
|
||||
public DiscordWebhook(JavaPlugin plugin, String webhookUrl, String username, String avatarUrl,
|
||||
int timeout, int retryAttempts, long retryDelay) {
|
||||
this.plugin = plugin;
|
||||
this.webhookUrl = webhookUrl;
|
||||
this.username = username;
|
||||
this.avatarUrl = avatarUrl;
|
||||
this.timeout = timeout * 1000; // конвертируем в миллисекунды
|
||||
this.retryAttempts = retryAttempts;
|
||||
this.retryDelay = retryDelay;
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправить простое текстовое сообщение
|
||||
*/
|
||||
public CompletableFuture<Boolean> sendMessage(String content) {
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("content", content);
|
||||
json.addProperty("username", username);
|
||||
json.addProperty("avatar_url", avatarUrl);
|
||||
|
||||
return sendWebhook(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправить embed сообщение
|
||||
*/
|
||||
public CompletableFuture<Boolean> sendEmbed(String title, String description, String color, String thumbnailUrl) {
|
||||
JsonObject embed = new JsonObject();
|
||||
if (title != null) embed.addProperty("title", title);
|
||||
if (description != null) embed.addProperty("description", description);
|
||||
if (color != null) embed.addProperty("color", Integer.parseInt(color, 16));
|
||||
|
||||
if (thumbnailUrl != null) {
|
||||
JsonObject thumbnail = new JsonObject();
|
||||
thumbnail.addProperty("url", thumbnailUrl);
|
||||
embed.add("thumbnail", thumbnail);
|
||||
}
|
||||
|
||||
// Добавляем timestamp
|
||||
embed.addProperty("timestamp", java.time.Instant.now().toString());
|
||||
|
||||
JsonArray embeds = new JsonArray();
|
||||
embeds.add(embed);
|
||||
|
||||
JsonObject json = new JsonObject();
|
||||
json.add("embeds", embeds);
|
||||
json.addProperty("username", username);
|
||||
json.addProperty("avatar_url", avatarUrl);
|
||||
|
||||
return sendWebhook(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправить embed с кастомными полями
|
||||
*/
|
||||
public CompletableFuture<Boolean> sendEmbedWithFields(String title, String description, String color,
|
||||
String thumbnailUrl, JsonArray fields) {
|
||||
JsonObject embed = new JsonObject();
|
||||
if (title != null) embed.addProperty("title", title);
|
||||
if (description != null) embed.addProperty("description", description);
|
||||
if (color != null) embed.addProperty("color", Integer.parseInt(color, 16));
|
||||
if (fields != null) embed.add("fields", fields);
|
||||
|
||||
if (thumbnailUrl != null) {
|
||||
JsonObject thumbnail = new JsonObject();
|
||||
thumbnail.addProperty("url", thumbnailUrl);
|
||||
embed.add("thumbnail", thumbnail);
|
||||
}
|
||||
|
||||
embed.addProperty("timestamp", java.time.Instant.now().toString());
|
||||
|
||||
JsonArray embeds = new JsonArray();
|
||||
embeds.add(embed);
|
||||
|
||||
JsonObject json = new JsonObject();
|
||||
json.add("embeds", embeds);
|
||||
json.addProperty("username", username);
|
||||
json.addProperty("avatar_url", avatarUrl);
|
||||
|
||||
return sendWebhook(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправить JSON в вебхук с повторными попытками
|
||||
*/
|
||||
private CompletableFuture<Boolean> sendWebhook(JsonObject json) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
for (int attempt = 1; attempt <= retryAttempts; attempt++) {
|
||||
try {
|
||||
boolean success = sendWebhookSync(json.toString());
|
||||
if (success) {
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("Discord webhook attempt " + attempt + " failed: " + e.getMessage());
|
||||
}
|
||||
|
||||
// Ждем перед следующей попыткой (кроме последней)
|
||||
if (attempt < retryAttempts) {
|
||||
try {
|
||||
Thread.sleep(retryDelay);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
plugin.getLogger().severe("Failed to send Discord webhook after " + retryAttempts + " attempts");
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Синхронная отправка в вебхук
|
||||
*/
|
||||
private boolean sendWebhookSync(String jsonPayload) throws IOException {
|
||||
URL url = new URL(webhookUrl);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
try {
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setRequestProperty("User-Agent", "LoChat-Discord-Integration/1.0");
|
||||
connection.setDoOutput(true);
|
||||
connection.setConnectTimeout(timeout);
|
||||
connection.setReadTimeout(timeout);
|
||||
|
||||
// Отправляем JSON
|
||||
try (OutputStream os = connection.getOutputStream()) {
|
||||
byte[] input = jsonPayload.getBytes(StandardCharsets.UTF_8);
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
|
||||
if (responseCode == 204) {
|
||||
return true; // Успешная отправка
|
||||
} else if (responseCode == 429) {
|
||||
plugin.getLogger().warning("Discord webhook rate limited (429)");
|
||||
return false;
|
||||
} else {
|
||||
plugin.getLogger().warning("Discord webhook returned code: " + responseCode);
|
||||
return false;
|
||||
}
|
||||
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверить валидность вебхука
|
||||
*/
|
||||
public boolean isValid() {
|
||||
return webhookUrl != null &&
|
||||
webhookUrl.startsWith("https://discord.com/api/webhooks/") &&
|
||||
webhookUrl.length() > 50; // Минимальная длина валидного URL
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue