forked from Minecraft/Hush
95 lines
3.5 KiB
Java
95 lines
3.5 KiB
Java
package com.zerohighdef.hush.listeners;
|
|
|
|
import com.zerohighdef.hush.Hush;
|
|
import com.zerohighdef.hush.WatchCategory;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.EventPriority;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
|
import xyz.etztech.Javacord;
|
|
import xyz.etztech.Webhook;
|
|
import xyz.etztech.embed.Author;
|
|
import xyz.etztech.embed.Embed;
|
|
import xyz.etztech.embed.Field;
|
|
|
|
import java.time.OffsetDateTime;
|
|
import java.util.regex.Matcher;
|
|
|
|
public class HushAsyncChatListener implements Listener {
|
|
private final Hush plugin;
|
|
|
|
public HushAsyncChatListener(Hush plugin) {
|
|
this.plugin = plugin;
|
|
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
|
public void onChat(AsyncPlayerChatEvent event) {
|
|
String chatMessage = event.getMessage();
|
|
Player sender = event.getPlayer();
|
|
|
|
for (String permission : plugin.getWatchLists().keySet()) {
|
|
if (sender.hasPermission("hush." + permission)) {
|
|
for (WatchCategory category : plugin.getWatchLists().get(permission)) {
|
|
boolean is_match = checkMessage(category, permission, sender, chatMessage);
|
|
|
|
// If the message matched, and the category requires the event to be canceled
|
|
if (is_match && category.getCancelMsg()) {
|
|
// cancel event
|
|
event.setCancelled(true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void runCommand(String command) {
|
|
Bukkit.getScheduler().runTask(plugin, () -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command));
|
|
}
|
|
|
|
private boolean checkMessage(WatchCategory category, String watchList, Player player, String chatMessage) {
|
|
Matcher match = category.checkPatterns(chatMessage);
|
|
|
|
if (match == null) {
|
|
// message was not a match
|
|
return false;
|
|
}
|
|
|
|
String playerName = player.getName();
|
|
String webhookURL = plugin.getConfig().getString("webhook");
|
|
|
|
for (String command: category.getCommands()) {
|
|
runCommand(command.replace("{player}", playerName));
|
|
}
|
|
|
|
plugin.log(String.format("%s matched filter in %s.%s", playerName, watchList, category.getCategoryName()));
|
|
|
|
if (webhookURL != null && !plugin.isInCoolDown(player)) {
|
|
plugin.addCoolDown(player);
|
|
chatMessage = match.replaceAll("**$0**");
|
|
String message = Javacord.escapeFormat(playerName) + " said: " + chatMessage;
|
|
Embed embed = new Embed()
|
|
.color(0xC70039)
|
|
.description(message)
|
|
.addField(new Field("Watchlist", String.format("`%s`", watchList)))
|
|
.addField(new Field("Category", String.format("`%s`", category.getCategoryName())))
|
|
.author(new Author(playerName, "", String.format("https://minotar.net/helm/%s/100.png", playerName), ""))
|
|
.timestamp(OffsetDateTime.now());
|
|
|
|
Webhook webhook = new Webhook("@here", embed);
|
|
|
|
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> {
|
|
try {
|
|
Javacord.sendWebhook(webhookURL, webhook);
|
|
} catch (Exception e) {
|
|
this.plugin.log("Webhook failed to send.");
|
|
}
|
|
});
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|