mirror of
https://github.com/JonasunderscoreJones/YetAnotherDiscordChatLink.git
synced 2025-10-23 08:39:18 +02:00
Port to JDA
This commit is contained in:
parent
da3e6ad947
commit
a74e5bd4b0
6 changed files with 120 additions and 67 deletions
24
build.gradle
24
build.gradle
|
@ -1,8 +1,11 @@
|
||||||
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'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
||||||
|
|
||||||
version = project.mod_version
|
version = project.mod_version
|
||||||
group = project.maven_group
|
group = project.maven_group
|
||||||
|
|
||||||
|
@ -16,6 +19,7 @@ repositories {
|
||||||
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
|
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
|
||||||
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
|
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
|
||||||
// for more information about repositories.
|
// for more information about repositories.
|
||||||
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
loom {
|
loom {
|
||||||
|
@ -46,7 +50,25 @@ 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.dev.impl.vendor"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
remapJar {
|
||||||
|
from('LICENSE') // Include license
|
||||||
|
|
||||||
|
inputFile.set shadowJar.archiveFile
|
||||||
|
dependsOn shadowJar
|
||||||
}
|
}
|
||||||
|
|
||||||
processResources {
|
processResources {
|
||||||
|
|
|
@ -9,7 +9,7 @@ yarn_mappings=1.20.2+build.1
|
||||||
loader_version=0.14.22
|
loader_version=0.14.22
|
||||||
|
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version=0.0.0
|
mod_version=1.1.0
|
||||||
maven_group=dev.jonasjones
|
maven_group=dev.jonasjones
|
||||||
archives_base_name=yet-another-discord-chat-link
|
archives_base_name=yet-another-discord-chat-link
|
||||||
|
|
||||||
|
|
|
@ -15,17 +15,16 @@ 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;
|
||||||
|
|
||||||
@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.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,51 +1,59 @@
|
||||||
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.entities.channel.middleman.MessageChannel;
|
||||||
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
|
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 net.minecraft.text.Text;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static dev.jonasjones.yadcl.YetAnotherDiscordChatLink.LOGGER;
|
import static dev.jonasjones.yadcl.YetAnotherDiscordChatLink.LOGGER;
|
||||||
|
|
||||||
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();
|
|
||||||
api = new DiscordApiBuilder().setToken(token).setAllIntents().login().join();
|
// Create the bot
|
||||||
api.addMessageCreateListener(event -> {
|
jda = JDABuilder.createDefault(token)
|
||||||
// Check if the message is from the specific channel by comparing channel IDs
|
.addEventListeners(new MessageListener())
|
||||||
if (String.valueOf(event.getChannel().getId()).equals(targetChannelId)) {
|
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
|
||||||
//check if message author is the bot
|
.build();
|
||||||
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")) {
|
||||||
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) {
|
||||||
|
@ -54,7 +62,7 @@ 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.");
|
||||||
|
@ -67,7 +75,7 @@ public class DiscordBot {
|
||||||
if (!isBotRunning) {
|
if (!isBotRunning) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
api.disconnect();
|
jda.shutdown();
|
||||||
isBotRunning = false;
|
isBotRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,12 +85,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!");
|
||||||
|
@ -105,47 +113,49 @@ public class DiscordBot {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void botStatus(String status) {
|
public static void setBotStatus(String statusType) {
|
||||||
if (!isBotRunning) {
|
if (!isBotRunning) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (status) {
|
switch (statusType.toLowerCase()) {
|
||||||
case "None" -> api.updateActivity("");
|
case "player count" -> jda.getPresence().setActivity(Activity.playing("Players: 100"));
|
||||||
case "PlayerCount" ->
|
case "ip" -> jda.getPresence().setActivity(Activity.listening("Server IP: example.com"));
|
||||||
api.updateActivity("Player Count: " + minecraftServer.getCurrentPlayerCount() + "/" + minecraftServer.getMaxPlayerCount());
|
case "uptime" -> jda.getPresence().setActivity(Activity.watching("Uptime: " + calculateUptime()));
|
||||||
case "IP" -> api.updateActivity("IP: " + minecraftServer.getServerIp());
|
default -> System.out.println("Invalid status type!");
|
||||||
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 days = TimeUnit.SECONDS.toDays(secs);
|
long secs = System.currentTimeMillis() - minecraftServer.getTicks() / 20;
|
||||||
secs -= TimeUnit.DAYS.toMillis(days);
|
long days = TimeUnit.SECONDS.toDays(secs);
|
||||||
long hours = TimeUnit.SECONDS.toHours(secs);
|
secs -= TimeUnit.DAYS.toMillis(days);
|
||||||
secs -= TimeUnit.HOURS.toMillis(hours);
|
long hours = TimeUnit.SECONDS.toHours(secs);
|
||||||
long minutes = TimeUnit.SECONDS.toMinutes(secs);
|
secs -= TimeUnit.HOURS.toMillis(hours);
|
||||||
|
long minutes = TimeUnit.SECONDS.toMinutes(secs);
|
||||||
|
|
||||||
StringBuilder duration = new StringBuilder();
|
StringBuilder duration = new StringBuilder();
|
||||||
if (days > 0) {
|
if (days > 0) {
|
||||||
duration.append(days).append(" Days, ");
|
duration.append(days).append(" Days, ");
|
||||||
}
|
|
||||||
if (hours > 0 || days > 0) {
|
|
||||||
if (hours == 1) {
|
|
||||||
duration.append(hours).append(" Hour, ");
|
|
||||||
} else {
|
|
||||||
duration.append(hours).append(" Hours, ");
|
|
||||||
}
|
}
|
||||||
}
|
if (hours > 0 || days > 0) {
|
||||||
if (minutes > 0 || hours > 0 || days > 0) {
|
if (hours == 1) {
|
||||||
if (minutes == 1) {
|
duration.append(hours).append(" Hour, ");
|
||||||
duration.append(minutes).append(" Minute, ");
|
} else {
|
||||||
} else {
|
duration.append(hours).append(" Hours, ");
|
||||||
duration.append(minutes).append(" Minutes, ");
|
}
|
||||||
|
}
|
||||||
|
if (minutes > 0 || hours > 0 || days > 0) {
|
||||||
|
if (minutes == 1) {
|
||||||
|
duration.append(minutes).append(" Minute, ");
|
||||||
|
} else {
|
||||||
|
duration.append(minutes).append(" Minutes, ");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return duration.toString();
|
return duration.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "-";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,7 +10,7 @@ import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
|
||||||
import static dev.jonasjones.yadcl.dcbot.DiscordBot.botStatus;
|
import static dev.jonasjones.yadcl.dcbot.DiscordBot.setBotStatus;
|
||||||
import static dev.jonasjones.yadcl.dcbot.DiscordBot.sendToDiscord;
|
import static dev.jonasjones.yadcl.dcbot.DiscordBot.sendToDiscord;
|
||||||
|
|
||||||
@Mixin(PlayerManager.class)
|
@Mixin(PlayerManager.class)
|
||||||
|
@ -20,7 +20,7 @@ public class PlayerManagerMixin {
|
||||||
if (ModConfigs.PLAYER_JOIN_LEAVE_MSG) {
|
if (ModConfigs.PLAYER_JOIN_LEAVE_MSG) {
|
||||||
sendToDiscord(player.getDisplayName().getString() + " joined the game");
|
sendToDiscord(player.getDisplayName().getString() + " joined the game");
|
||||||
if (ModConfigs.BOT_STATUS.equals("PlayerCount")) {
|
if (ModConfigs.BOT_STATUS.equals("PlayerCount")) {
|
||||||
botStatus(ModConfigs.BOT_STATUS);
|
setBotStatus(ModConfigs.BOT_STATUS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ public class PlayerManagerMixin {
|
||||||
if (ModConfigs.PLAYER_JOIN_LEAVE_MSG) {
|
if (ModConfigs.PLAYER_JOIN_LEAVE_MSG) {
|
||||||
sendToDiscord(player.getDisplayName().getString() + " left the game");
|
sendToDiscord(player.getDisplayName().getString() + " left the game");
|
||||||
if (ModConfigs.BOT_STATUS.equals("PlayerCount")) {
|
if (ModConfigs.BOT_STATUS.equals("PlayerCount")) {
|
||||||
botStatus(ModConfigs.BOT_STATUS);
|
setBotStatus(ModConfigs.BOT_STATUS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue