diff --git a/pom.xml b/pom.xml index 3ce3630..3ab3b1b 100644 --- a/pom.xml +++ b/pom.xml @@ -81,5 +81,10 @@ 1.19.3-R0.1-SNAPSHOT provided + + net.dv8tion + JDA + 5.0.0-beta.11 + diff --git a/src/main/java/testplugin/plugin/Commands/setDiscordRelayChannelIdCommand.java b/src/main/java/testplugin/plugin/Commands/setDiscordRelayChannelIdCommand.java new file mode 100644 index 0000000..e5a49eb --- /dev/null +++ b/src/main/java/testplugin/plugin/Commands/setDiscordRelayChannelIdCommand.java @@ -0,0 +1,23 @@ +package testplugin.plugin.Commands; + +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import testplugin.plugin.Events.DiscordRelayChannelIdChangedEvent; + +public class setDiscordRelayChannelIdCommand implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (sender instanceof Player | sender instanceof Server && args.length == 1) { + String discordChannelId = args[0]; + DiscordRelayChannelIdChangedEvent event = new DiscordRelayChannelIdChangedEvent(discordChannelId); + Bukkit.getServer().getPluginManager().callEvent(event); + sender.sendMessage("Set discord relay channel to " + discordChannelId); + return true; + } + return false; + } +} \ No newline at end of file diff --git a/src/main/java/testplugin/plugin/Commands/setDiscordTokenCommand.java b/src/main/java/testplugin/plugin/Commands/setDiscordTokenCommand.java new file mode 100644 index 0000000..291a888 --- /dev/null +++ b/src/main/java/testplugin/plugin/Commands/setDiscordTokenCommand.java @@ -0,0 +1,23 @@ +package testplugin.plugin.Commands; + +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import testplugin.plugin.Events.DiscordTokenChangedEvent; + +public class setDiscordTokenCommand implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (sender instanceof Player | sender instanceof Server && args.length == 1) { + String discordToken = args[0]; + DiscordTokenChangedEvent event = new DiscordTokenChangedEvent(discordToken); + Bukkit.getServer().getPluginManager().callEvent(event); + sender.sendMessage("Set discord bot token to " + discordToken); + return true; + } + return false; + } +} \ No newline at end of file diff --git a/src/main/java/testplugin/plugin/setWebhookCommand.java b/src/main/java/testplugin/plugin/Commands/setWebhookCommand.java similarity index 91% rename from src/main/java/testplugin/plugin/setWebhookCommand.java rename to src/main/java/testplugin/plugin/Commands/setWebhookCommand.java index 6487e9c..58e5439 100644 --- a/src/main/java/testplugin/plugin/setWebhookCommand.java +++ b/src/main/java/testplugin/plugin/Commands/setWebhookCommand.java @@ -6,9 +6,7 @@ import org.bukkit.Server; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; public class setWebhookCommand implements CommandExecutor { // This method is called, when somebody uses our command diff --git a/src/main/java/testplugin/plugin/DiscordMessageEvent.java b/src/main/java/testplugin/plugin/DiscordMessageEvent.java new file mode 100644 index 0000000..e6bc880 --- /dev/null +++ b/src/main/java/testplugin/plugin/DiscordMessageEvent.java @@ -0,0 +1,29 @@ +package testplugin.plugin; + +import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.plugin.Plugin; + +import java.util.Objects; + +public class DiscordMessageEvent extends ListenerAdapter { + + private Plugin plugin; + private Config config; + + public DiscordMessageEvent(Plugin p,Config cfg) { + this.plugin = p; + this.config = cfg; + } + @Override + public void onMessageReceived(MessageReceivedEvent event) { + User author = event.getAuthor(); + String message = event.getMessage().getContentStripped(); + if (event.isFromGuild() && !author.isBot() && event.getGuildChannel().getId().equals(config.getValue("discordRelayChannel").toString())) { + plugin.getServer().broadcastMessage(ChatColor.DARK_BLUE + "[DISCORD] " + ChatColor.WHITE + author.getEffectiveName() + ": " + message); + } + } +} diff --git a/src/main/java/testplugin/plugin/Events/DiscordRelayChannelIdChangedEvent.java b/src/main/java/testplugin/plugin/Events/DiscordRelayChannelIdChangedEvent.java new file mode 100644 index 0000000..112ffd5 --- /dev/null +++ b/src/main/java/testplugin/plugin/Events/DiscordRelayChannelIdChangedEvent.java @@ -0,0 +1,25 @@ +package testplugin.plugin.Events; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public final class DiscordRelayChannelIdChangedEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + private String message; + + public DiscordRelayChannelIdChangedEvent(String msg) { + message = msg; + } + + public String getMessage() { + return message; + } + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/testplugin/plugin/Events/DiscordTokenChangedEvent.java b/src/main/java/testplugin/plugin/Events/DiscordTokenChangedEvent.java new file mode 100644 index 0000000..be7d335 --- /dev/null +++ b/src/main/java/testplugin/plugin/Events/DiscordTokenChangedEvent.java @@ -0,0 +1,25 @@ +package testplugin.plugin.Events; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public final class DiscordTokenChangedEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + private String message; + + public DiscordTokenChangedEvent(String msg) { + message = msg; + } + + public String getMessage() { + return message; + } + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/testplugin/plugin/WebhookChangedEvent.java b/src/main/java/testplugin/plugin/Events/WebhookChangedEvent.java similarity index 100% rename from src/main/java/testplugin/plugin/WebhookChangedEvent.java rename to src/main/java/testplugin/plugin/Events/WebhookChangedEvent.java diff --git a/src/main/java/testplugin/plugin/Events.java b/src/main/java/testplugin/plugin/LoggedEvents.java similarity index 81% rename from src/main/java/testplugin/plugin/Events.java rename to src/main/java/testplugin/plugin/LoggedEvents.java index ca8557a..06617f9 100644 --- a/src/main/java/testplugin/plugin/Events.java +++ b/src/main/java/testplugin/plugin/LoggedEvents.java @@ -99,4 +99,22 @@ public class Events implements Listener { config.setValue("webhookURL",this.webhookURL); this.plugin.saveConfig(); } + + @EventHandler(priority = EventPriority.LOW) + public void onDiscordTokenChanged(DiscordTokenChangedEvent event) { + String discordToken = event.getMessage(); + Config config = new Config(this.plugin); + logger.warning("discord token changed!"); + config.setValue("discordBotToken",discordToken); + this.plugin.saveConfig(); + } + + @EventHandler(priority = EventPriority.LOW) + public void onDiscordRelayChannelIdChanged(DiscordRelayChannelIdChangedEvent event) { + String discordRelayChannelId = event.getMessage(); + Config config = new Config(this.plugin); + logger.warning("discord relay channel id changed!"); + config.setValue("discordRelayChannel",discordRelayChannelId); + this.plugin.saveConfig(); + } } \ No newline at end of file diff --git a/src/main/java/testplugin/plugin/WebhookEventLogger.java b/src/main/java/testplugin/plugin/WebhookEventLogger.java index 026d5b0..196b1dd 100644 --- a/src/main/java/testplugin/plugin/WebhookEventLogger.java +++ b/src/main/java/testplugin/plugin/WebhookEventLogger.java @@ -1,7 +1,14 @@ package testplugin.plugin; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.requests.GatewayIntent; import org.bukkit.plugin.java.JavaPlugin; +import testplugin.plugin.Commands.setDiscordRelayChannelIdCommand; +import testplugin.plugin.Commands.setDiscordTokenCommand; +import testplugin.plugin.Commands.setWebhookCommand; +import javax.swing.*; import java.awt.*; import java.io.IOException; import java.util.Arrays; @@ -9,6 +16,9 @@ import java.util.Objects; public final class WebhookEventLogger extends JavaPlugin { private String webhookURL; + private String discordBotToken; + + private JDA jda; @Override public void onEnable() { @@ -19,9 +29,19 @@ public final class WebhookEventLogger extends JavaPlugin { this.webhookURL = config.getValue("webhookURL").toString(); - getServer().getPluginManager().registerEvents(new Events(getLogger(), this, webhookURL), this); + this.discordBotToken = config.getValue("discordBotToken").toString(); + if (discordBotToken.equals("TOKEN")) { + getLogger().warning("Token not set, please set it using \"/discordtoken\" and \"/discordrelaychannelid\" ingame do set the necessary information"); + } else { + getLogger().info("there is a token present"); + this.jda = JDABuilder.createDefault(discordBotToken).enableIntents(GatewayIntent.GUILD_MESSAGES,GatewayIntent.MESSAGE_CONTENT).addEventListeners(new DiscordMessageEvent(this, config)).build(); + } + + getServer().getPluginManager().registerEvents(new LoggedEvents(getLogger(), this, webhookURL), this); Objects.requireNonNull(getCommand("webhook")).setExecutor(new setWebhookCommand()); + Objects.requireNonNull(getCommand("discordtoken")).setExecutor(new setDiscordTokenCommand()); + Objects.requireNonNull(getCommand("discordrelaychannelid")).setExecutor(new setDiscordRelayChannelIdCommand()); DiscordWebhook Webhook = new DiscordWebhook(webhookURL); @@ -45,6 +65,7 @@ public final class WebhookEventLogger extends JavaPlugin { .setTitle("Server stopped.") .setColor(new Color(220, 75, 40)) ); + jda.shutdownNow(); try { Webhook.execute(); } catch (IOException e) { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9ef0c4a..0782fb1 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -14,9 +14,28 @@ commands: permission-message: You do not have permission to use this command. + usage: / [url] + discordtoken: + + permission: webhook.can_set + + description: Set the discord relay bot token used to relay discord messages + + permission-message: You do not have permission to use this command. + + usage: / [url] + discordrelaychannelid: + + permission: webhook.can_set + + description: Set the discord relay bot channel id used to relay discord messages + + permission-message: You do not have permission to use this command. + usage: / [url] permissions: webhook.can_set: - description: Allows to set webhook url - default: op \ No newline at end of file + description: Allows to set webhook/discord bot settings + default: op +