Port to JDA and bug fixes

This commit is contained in:
Jonas_Jones 2023-11-14 20:14:45 +01:00
parent 428e49d259
commit 3faec0166c
5 changed files with 125 additions and 82 deletions

View file

@ -1,6 +1,9 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
plugins { plugins {
id 'fabric-loom' version '1.4-SNAPSHOT' id 'fabric-loom' version '1.4-SNAPSHOT'
id 'maven-publish' id 'maven-publish'
id 'com.github.johnrengelman.shadow' version '8.1.1'
} }
version = project.mod_version version = project.mod_version
@ -46,9 +49,28 @@ dependencies {
// These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time.
// modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}"
implementation 'org.javacord:javacord:3.8.0' implementation('net.dv8tion:JDA:5.0.0-beta.17') {
exclude module: 'opus-java'
}
shadow 'net.dv8tion:JDA:5.0.0-beta.17'
} }
tasks.withType(ShadowJar).configureEach {
it.minimize()
it.configurations = [project.configurations.shadow]
it.enableRelocation = true
it.relocationPrefix = "dev.jonasjones.yadacl.vendor"
}
remapJar {
from('LICENSE') // Include license
inputFile.set shadowJar.archiveFile
dependsOn shadowJar
}
processResources { processResources {
inputs.property "version", project.version inputs.property "version", project.version

View file

@ -15,17 +15,17 @@ public class YetAnotherDiscordChatLink implements ModInitializer {
public static final String MOD_ID = "yadcl"; public static final String MOD_ID = "yadcl";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static DiscordBot discordBot;
public static final long startTime = System.currentTimeMillis();
@Override @Override
public void onInitialize() { public void onInitialize() {
// Register the config // Register the config
ModConfigs.registerConfigs(); ModConfigs.registerConfigs();
// Set the token and channel id
DiscordBot.setToken(ModConfigs.TOKEN);
DiscordBot.setTargetChannelId(ModConfigs.CHANNEL_ID);
// Start the bot // Start the bot
DiscordBot.startBot(); discordBot = new DiscordBot(ModConfigs.TOKEN, ModConfigs.CHANNEL_ID);
discordBot.startBot();
// send starting message // send starting message
sendToDiscord("Server is starting up."); sendToDiscord("Server is starting up.");
} }

View file

@ -17,7 +17,7 @@ public class ModConfigs {
//public static Boolean PLAYER_COMMAND_MSG; //public static Boolean PLAYER_COMMAND_MSG;
public static Boolean SERVER_START_STOP_MSG; public static Boolean SERVER_START_STOP_MSG;
//public static String BOT_STATUS; public static String BOT_STATUS;
public static void registerConfigs() { public static void registerConfigs() {
configs = new ModConfigProvider(); configs = new ModConfigProvider();
@ -37,7 +37,7 @@ public class ModConfigs {
configs.addKeyValuePair(new Pair<>("bot.player_chat_msgs", true), "Should the bot send a message when a player sends a chat message"); configs.addKeyValuePair(new Pair<>("bot.player_chat_msgs", true), "Should the bot send a message when a player sends a chat message");
//configs.addKeyValuePair(new Pair<>("bot.player_command_msgs", true), "Should the bot send a message when a player sends a command"); //configs.addKeyValuePair(new Pair<>("bot.player_command_msgs", true), "Should the bot send a message when a player sends a command");
configs.addKeyValuePair(new Pair<>("bot.server_start_stop_msgs", true), "Should the bot send a message when the server starts or stops"); configs.addKeyValuePair(new Pair<>("bot.server_start_stop_msgs", true), "Should the bot send a message when the server starts or stops");
//configs.addKeyValuePair(new Pair<>("bot.status", "PlayerCount"), "What status should the bot display [None, PlayerCount, IP, Uptime]"); configs.addKeyValuePair(new Pair<>("bot.status", "PlayerCount"), "What status should the bot display [None, PlayerCount, IP, Uptime]");
} }
private static void assignConfigs() { private static void assignConfigs() {
@ -49,7 +49,7 @@ public class ModConfigs {
PLAYER_CHAT_MSG = CONFIG.getOrDefault("bot.player_chat_msgs", true); PLAYER_CHAT_MSG = CONFIG.getOrDefault("bot.player_chat_msgs", true);
//PLAYER_COMMAND_MSG = CONFIG.getOrDefault("bot.player_command_msgs", true); //PLAYER_COMMAND_MSG = CONFIG.getOrDefault("bot.player_command_msgs", true);
SERVER_START_STOP_MSG = CONFIG.getOrDefault("bot.server_start_stop_msgs", true); SERVER_START_STOP_MSG = CONFIG.getOrDefault("bot.server_start_stop_msgs", true);
//BOT_STATUS = CONFIG.getOrDefault("bot.status", "PlayerCount"); BOT_STATUS = CONFIG.getOrDefault("bot.status", "PlayerCount");
YetAnotherDiscordChatLink.LOGGER.info("All " + configs.getConfigsList().size() + " Configs have been set properly"); YetAnotherDiscordChatLink.LOGGER.info("All " + configs.getConfigsList().size() + " Configs have been set properly");
} }

View file

@ -1,54 +1,55 @@
package dev.jonasjones.yadcl.dcbot; package dev.jonasjones.yadcl.dcbot;
import dev.jonasjones.yadcl.config.ModConfigs; import dev.jonasjones.yadcl.config.ModConfigs;
import lombok.Setter; import lombok.Getter;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text;
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.entity.channel.TextChannel;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static dev.jonasjones.yadcl.YetAnotherDiscordChatLink.LOGGER; import static dev.jonasjones.yadcl.YetAnotherDiscordChatLink.LOGGER;
import static dev.jonasjones.yadcl.YetAnotherDiscordChatLink.startTime;
public class DiscordBot { public class DiscordBot extends ListenerAdapter {
@Setter
private static String token; private static String token;
@Setter @Getter
private static String targetChannelId; private static String targetChannelId;
private static DiscordApi api; private static JDA jda;
@Getter
private static MinecraftServer minecraftServer; private static MinecraftServer minecraftServer;
@Getter
private static Boolean isBotRunning = false; private static Boolean isBotRunning = false;
public DiscordBot(String token, String targetChannelId) {
this.token = token;
this.targetChannelId = targetChannelId;
// Register Minecraft Server events
registerEvents();
}
public static void startBot() { public static void startBot() {
if (isBotRunning) { if (isBotRunning) {
return; return;
} }
try { try {
registerEvents(); // Create the bot
api = new DiscordApiBuilder().setToken(token).setAllIntents().login().join(); jda = JDABuilder.createDefault(token)
api.addMessageCreateListener(event -> { .addEventListeners(new MessageListener())
// Check if the message is from the specific channel by comparing channel IDs .enableIntents(GatewayIntent.MESSAGE_CONTENT)
if (String.valueOf(event.getChannel().getId()).equals(targetChannelId)) { .build();
//check if message author is the bot
if (event.getMessageAuthor().isBotUser()) {
return;
}
String discordMessage = "[" + event.getMessageAuthor().getDisplayName() + "] " + event.getMessageContent();
// Broadcast the message to Minecraft chat
// You can implement a method to broadcast messages to Minecraft players
minecraftServer.getPlayerManager().broadcast(Text.of(discordMessage), false);
}
});
// Set the bot status // Set the bot status
/*if (ModConfigs.BOT_STATUS.equals("Uptime")) { if (ModConfigs.BOT_STATUS.equals("Uptime") || ModConfigs.BOT_STATUS.equals("PlayerCount")) {
new Thread(() -> { new Thread(() -> {
while (isBotRunning) { while (isBotRunning) {
botStatus(ModConfigs.BOT_STATUS); setBotStatus(ModConfigs.BOT_STATUS);
try { try {
Thread.sleep(60000); Thread.sleep(60000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -57,8 +58,8 @@ public class DiscordBot {
} }
}).start(); }).start();
} else { } else {
botStatus(ModConfigs.BOT_STATUS); setBotStatus(ModConfigs.BOT_STATUS);
}*/ }
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Failed to start Discord bot. Check the provided discord token in the config file."); LOGGER.error("Failed to start Discord bot. Check the provided discord token in the config file.");
return; return;
@ -70,7 +71,7 @@ public class DiscordBot {
if (!isBotRunning) { if (!isBotRunning) {
return; return;
} }
api.disconnect(); jda.shutdown();
isBotRunning = false; isBotRunning = false;
} }
@ -80,12 +81,12 @@ public class DiscordBot {
} }
// Get the text channel by its ID // Get the text channel by its ID
try { try {
TextChannel channel = api.getTextChannelById(targetChannelId).orElse(null); TextChannel channel = jda.getTextChannelById(targetChannelId);
// Check if the channel exists and send the message // Check if the channel exists and send the message
if (channel != null) { if (channel != null) {
channel.sendMessage(message); channel.sendMessage(message).queue();
} else { } else {
throw new Exception("Discord Channel does not exist!"); //throw new Exception("Discord Channel does not exist!");
} }
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Discord Channel does not exist!"); LOGGER.error("Discord Channel does not exist!");
@ -95,7 +96,7 @@ public class DiscordBot {
private static void registerEvents() { private static void registerEvents() {
ServerTickEvents.START_SERVER_TICK.register(server -> { ServerTickEvents.START_SERVER_TICK.register(server -> {
// This code is executed on every server tick // This code is executed on every server tick
minecraftServer = server; DiscordBot.minecraftServer = server;
}); });
ServerLifecycleEvents.SERVER_STARTED.register(server -> { ServerLifecycleEvents.SERVER_STARTED.register(server -> {
@ -104,32 +105,23 @@ public class DiscordBot {
ServerLifecycleEvents.SERVER_STOPPED.register(server -> { ServerLifecycleEvents.SERVER_STOPPED.register(server -> {
sendToDiscord("Server is stopped!"); sendToDiscord("Server is stopped!");
//wait for 2 seconds to make sure the message is sent
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopBot(); stopBot();
}); });
} }
/*public static void botStatus(String status) { public static void setBotStatus(String statusType) {
if (!isBotRunning) { if (!isBotRunning || DiscordBot.minecraftServer == null) {
return; return;
} }
switch (status) { switch (statusType) {
case "None" -> api.updateActivity(""); case "PlayerCount" -> jda.getPresence().setActivity(Activity.playing("with " + DiscordBot.minecraftServer.getCurrentPlayerCount() + "/"+ DiscordBot.minecraftServer.getMaxPlayerCount() + " Players"));
case "PlayerCount" -> case "Uptime" -> jda.getPresence().setActivity(Activity.playing("for " + calculateUptime()));
api.updateActivity("Player Count: " + minecraftServer.getCurrentPlayerCount() + "/" + minecraftServer.getMaxPlayerCount());
case "IP" -> api.updateActivity("IP: " + minecraftServer.getServerIp());
case "Uptime" -> api.updateActivity("Uptime: " + calculateUptime());
default -> api.updateActivity(null);
} }
} }
private static String calculateUptime() { private static String calculateUptime() {
long secs = System.currentTimeMillis() - minecraftServer.getTicks() / 20; try {
long secs = (System.currentTimeMillis() - startTime) / 1000;
long days = TimeUnit.SECONDS.toDays(secs); long days = TimeUnit.SECONDS.toDays(secs);
secs -= TimeUnit.DAYS.toMillis(days); secs -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.SECONDS.toHours(secs); long hours = TimeUnit.SECONDS.toHours(secs);
@ -149,12 +141,19 @@ public class DiscordBot {
} }
if (minutes > 0 || hours > 0 || days > 0) { if (minutes > 0 || hours > 0 || days > 0) {
if (minutes == 1) { if (minutes == 1) {
duration.append(minutes).append(" Minute, "); duration.append(minutes).append(" Minute");
} else { } else {
duration.append(minutes).append(" Minutes, "); duration.append(minutes).append(" Minutes");
} }
} }
if (duration.isEmpty()) {
return "less than a minute";
}
return duration.toString(); return duration.toString();
}*/ } catch (Exception e) {
return "-";
}
}
} }

View file

@ -0,0 +1,22 @@
package dev.jonasjones.yadcl.dcbot;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.minecraft.text.Text;
import static dev.jonasjones.yadcl.YetAnotherDiscordChatLink.discordBot;
public class MessageListener extends ListenerAdapter {
@Override
public void onMessageReceived(MessageReceivedEvent event) {
if (!DiscordBot.getIsBotRunning()) return; // Ignore messages if the bot is not running
if (event.getAuthor().isBot()) return; // Ignore messages from other bots
// Check if the message was sent in the trigger channel
if (event.getChannel().getId().equals(DiscordBot.getTargetChannelId())) {
// Send the message to the server
DiscordBot.getMinecraftServer().getPlayerManager().broadcast(Text.of("[" +event.getAuthor().getName() + "]: " + event.getMessage().getContentRaw()), false);
}
}
}