implement chat relay (Discord to minecraft)

aa
This commit is contained in:
dowoge 2023-07-01 04:19:59 -04:00
parent 2f823a9916
commit e697b1a0f5
No known key found for this signature in database
GPG Key ID: A868F67EA0425DD8
11 changed files with 191 additions and 5 deletions

View File

@ -81,5 +81,10 @@
<version>1.19.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.0-beta.11</version>
</dependency>
</dependencies>
</project>

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

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

View File

@ -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) {

View File

@ -14,9 +14,28 @@ commands:
permission-message: You do not have permission to use this command.
usage: /<command> [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: /<command> [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: /<command> [url]
permissions:
webhook.can_set:
description: Allows to set webhook url
default: op
description: Allows to set webhook/discord bot settings
default: op