Skip to content

Commit

Permalink
Add Discord logging for some events
Browse files Browse the repository at this point in the history
  • Loading branch information
renbinden committed Nov 27, 2023
1 parent a78cd3f commit 21e329b
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.arvandor.whisperingthreshold.cooldown.WtNotificationCooldownRepository;
import net.arvandor.whisperingthreshold.cooldown.WtNotificationCooldownService;
import net.arvandor.whisperingthreshold.discord.WtDiscordService;
import net.arvandor.whisperingthreshold.listener.PlayerMoveListener;
import net.arvandor.whisperingthreshold.listener.*;
import net.arvandor.whisperingthreshold.mixpanel.WtMixpanelService;
import net.arvandor.whisperingthreshold.notification.WtNotificationOverrideRepository;
import net.arvandor.whisperingthreshold.notification.WtNotificationService;
Expand Down Expand Up @@ -88,6 +88,19 @@ public void onEnable() {
mixpanelService),
this);

getServer()
.getPluginManager()
.registerEvents(new GameModeChangeListener(discordService), this);
getServer()
.getPluginManager()
.registerEvents(new InventoryCreativeListener(discordService), this);
getServer()
.getPluginManager()
.registerEvents(new PlayerDropItemListener(discordService), this);
getServer()
.getPluginManager()
.registerEvents(new PlayerPickupItemListener(discordService), this);

getCommand("wt")
.setExecutor(
new WtCommand(this, worldEditService, areaService, notificationService));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package net.arvandor.whisperingthreshold.discord;

import net.dv8tion.jda.api.EmbedBuilder;

import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class EmbedBuilders {

private EmbedBuilders() {}

public static void addItemAttributes(EmbedBuilder embedBuilder, String slot, ItemStack item) {
embedBuilder.addField("Item details", slot, false);
if (item != null) {
embedBuilder.addField("Item", item.getType().toString(), false);
embedBuilder.addField("Amount", Integer.toString(item.getAmount()), false);
ItemMeta meta = item.getItemMeta();
if (meta != null) {
if (meta.hasDisplayName()) {
embedBuilder.addField("Display name", meta.getDisplayName(), false);
}
if (meta.hasLore()) {
embedBuilder.addField("Lore", String.join("\n", meta.getLore()), false);
}
Map<String, Object> serializedMeta = meta.serialize();
if (serializedMeta.containsKey("PublicBukkitValues")) {
Map<String, Object> serializedPersistentDataContainer =
(Map<String, Object>) serializedMeta.get("PublicBukkitValues");
serializedPersistentDataContainer.forEach(
(key, value) -> {
embedBuilder.addField(
"Persistent data container: " + key,
toString(value),
false);
});
}
}
}
}

private static String toString(Object value) {
if (value instanceof Map) {
return ((Map<String, Object>) value)
.entrySet().stream()
.map(entry -> entry.getKey() + ": " + toString(entry.getValue()))
.collect(Collectors.joining(", "));
} else if (value instanceof List) {
return ((List<Object>) value)
.stream().map(EmbedBuilders::toString).collect(Collectors.joining(", "));
} else if (value instanceof Object[]) {
return Arrays.stream((Object[]) value)
.map(EmbedBuilders::toString)
.collect(Collectors.joining(", "));
} else {
return value.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

Expand All @@ -16,11 +17,13 @@ public final class WtDiscordService {
private final JDA jda;
private Guild guild;
private TextChannel channel;
private TextChannel actionLogChannel;

public WtDiscordService(WhisperingThreshold plugin) {
this.plugin = plugin;
long guildId = plugin.getConfig().getLong("discord.server-id");
long channelId = plugin.getConfig().getLong("discord.channel-id");
long actionLogChannelId = plugin.getConfig().getLong("action-log.channel-id");
JDABuilder builder =
JDABuilder.createDefault(plugin.getConfig().getString("discord.token"));
builder.disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE);
Expand All @@ -36,6 +39,8 @@ public WtDiscordService(WhisperingThreshold plugin) {
this.guild = jda.getGuildById(guildId);
if (guild != null) {
this.channel = guild.getTextChannelById(channelId);
this.actionLogChannel =
guild.getTextChannelById(actionLogChannelId);
}
} catch (InterruptedException exception) {
plugin.getLogger()
Expand All @@ -54,6 +59,18 @@ public void sendMessage(String message) {
}
}

public void sendActionLogMessage(String message) {
if (actionLogChannel != null) {
actionLogChannel.sendMessage(message).queue();
}
}

public void sendActionLogMessage(MessageEmbed message) {
if (actionLogChannel != null) {
actionLogChannel.sendMessageEmbeds(message).queue();
}
}

public void disconnect() {
jda.shutdown();
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.arvandor.whisperingthreshold.listener;

import net.arvandor.whisperingthreshold.discord.WtDiscordService;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerGameModeChangeEvent;

import java.awt.Color;

public class GameModeChangeListener implements Listener {

private final WtDiscordService discordService;

public GameModeChangeListener(WtDiscordService discordService) {
this.discordService = discordService;
}

@EventHandler
public void onGameModeChange(PlayerGameModeChangeEvent event) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Game mode change");
embedBuilder.setColor(Color.MAGENTA);
embedBuilder.addField("Player", event.getPlayer().getName(), false);
embedBuilder.addField("Old game mode", event.getPlayer().getGameMode().toString(), false);
embedBuilder.addField("New game mode", event.getNewGameMode().toString(), false);
MessageEmbed embed = embedBuilder.build();

discordService.sendActionLogMessage(embed);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.arvandor.whisperingthreshold.listener;

import net.arvandor.whisperingthreshold.discord.EmbedBuilders;
import net.arvandor.whisperingthreshold.discord.WtDiscordService;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCreativeEvent;

import java.awt.Color;
import java.util.stream.Collectors;

public class InventoryCreativeListener implements Listener {

private final WtDiscordService discordService;

public InventoryCreativeListener(WtDiscordService discordService) {
this.discordService = discordService;
}

@EventHandler
public void onInventoryCreative(InventoryCreativeEvent event) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Creative inventory action");
embedBuilder.setColor(Color.YELLOW);
embedBuilder.addField("Player", event.getWhoClicked().getName(), false);
embedBuilder.addField(
"Clicked slot", event.getSlot() + " (" + event.getSlotType() + ")", false);
embedBuilder.addBlankField(false);
EmbedBuilders.addItemAttributes(embedBuilder, "Cursor item", event.getCursor());
embedBuilder.addBlankField(false);
EmbedBuilders.addItemAttributes(embedBuilder, "Clicked item", event.getCurrentItem());
embedBuilder.addBlankField(false);
embedBuilder.addField("World", event.getWhoClicked().getWorld().getName(), false);
embedBuilder.addField(
"X", String.valueOf(event.getWhoClicked().getLocation().getBlockX()), true);
embedBuilder.addField(
"Y", String.valueOf(event.getWhoClicked().getLocation().getBlockY()), true);
embedBuilder.addField(
"Z", String.valueOf(event.getWhoClicked().getLocation().getBlockZ()), true);
embedBuilder.addField(
"Nearby players (within 32 blocks)",
event.getWhoClicked().getNearbyEntities(32, 32, 32).stream()
.filter(entity -> entity instanceof Player)
.map(Entity::getName)
.collect(Collectors.joining(", ")),
false);
MessageEmbed embed = embedBuilder.build();

discordService.sendActionLogMessage(embed);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.arvandor.whisperingthreshold.listener;

import static net.arvandor.whisperingthreshold.discord.EmbedBuilders.addItemAttributes;

import net.arvandor.whisperingthreshold.discord.WtDiscordService;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerDropItemEvent;

import java.awt.Color;

public class PlayerDropItemListener implements Listener {

private final WtDiscordService discordService;

public PlayerDropItemListener(WtDiscordService discordService) {
this.discordService = discordService;
}

@EventHandler
public void onPlayerDropItem(PlayerDropItemEvent event) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Item dropped");
embedBuilder.setColor(Color.BLUE);
embedBuilder.addField("Player", event.getPlayer().getName(), false);
addItemAttributes(embedBuilder, "Dropped item", event.getItemDrop().getItemStack());
MessageEmbed embed = embedBuilder.build();

discordService.sendActionLogMessage(embed);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.arvandor.whisperingthreshold.listener;

import static net.arvandor.whisperingthreshold.discord.EmbedBuilders.addItemAttributes;

import net.arvandor.whisperingthreshold.discord.WtDiscordService;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;

import java.awt.Color;

public class PlayerPickupItemListener implements Listener {

private final WtDiscordService discordService;

public PlayerPickupItemListener(WtDiscordService discordService) {
this.discordService = discordService;
}

@EventHandler
public void onPlayerDropItem(EntityPickupItemEvent event) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Item picked up");
embedBuilder.setColor(Color.CYAN);
embedBuilder.addField("Player", event.getEntity().getName(), false);
addItemAttributes(embedBuilder, "Acquired item", event.getItem().getItemStack());
MessageEmbed embed = embedBuilder.build();

discordService.sendActionLogMessage(embed);
}
}
4 changes: 3 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ cooldown:
messages:
enter: '&2{0} has entered the area {1}'
mixpanel:
token: ''
token: ''
action-log:
channel-id: 1178426232531058748

0 comments on commit 21e329b

Please sign in to comment.