mirror of
https://github.com/JonasunderscoreJones/YetAnotherDiscordChatLink.git
synced 2025-10-23 00:39:17 +02:00
Fixed bot status issues
This commit is contained in:
parent
a464a229cc
commit
600046e786
2 changed files with 16 additions and 17 deletions
|
@ -16,6 +16,7 @@ public class YetAnotherDiscordChatLink implements ModInitializer {
|
||||||
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
||||||
|
|
||||||
public static DiscordBot discordBot;
|
public static DiscordBot discordBot;
|
||||||
|
public static final long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
|
|
|
@ -6,19 +6,16 @@ import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.JDABuilder;
|
import net.dv8tion.jda.api.JDABuilder;
|
||||||
import net.dv8tion.jda.api.entities.Activity;
|
import net.dv8tion.jda.api.entities.Activity;
|
||||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
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.hooks.ListenerAdapter;
|
||||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
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.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;
|
||||||
|
import static dev.jonasjones.yadcl.YetAnotherDiscordChatLink.startTime;
|
||||||
|
|
||||||
public class DiscordBot extends ListenerAdapter {
|
public class DiscordBot extends ListenerAdapter {
|
||||||
private static String token;
|
private static String token;
|
||||||
|
@ -43,14 +40,13 @@ public class DiscordBot extends ListenerAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// Create the bot
|
// Create the bot
|
||||||
jda = JDABuilder.createDefault(token)
|
jda = JDABuilder.createDefault(token)
|
||||||
.addEventListeners(new MessageListener())
|
.addEventListeners(new MessageListener())
|
||||||
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
|
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
|
||||||
.build();
|
.build();
|
||||||
// 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) {
|
||||||
setBotStatus(ModConfigs.BOT_STATUS);
|
setBotStatus(ModConfigs.BOT_STATUS);
|
||||||
|
@ -100,7 +96,7 @@ public class DiscordBot extends ListenerAdapter {
|
||||||
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 -> {
|
||||||
|
@ -114,20 +110,18 @@ public class DiscordBot extends ListenerAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setBotStatus(String statusType) {
|
public static void setBotStatus(String statusType) {
|
||||||
if (!isBotRunning) {
|
if (!isBotRunning || DiscordBot.minecraftServer == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (statusType.toLowerCase()) {
|
switch (statusType) {
|
||||||
case "player count" -> jda.getPresence().setActivity(Activity.playing("Players: 100"));
|
case "PlayerCount" -> jda.getPresence().setActivity(Activity.playing("with " + DiscordBot.minecraftServer.getCurrentPlayerCount() + "/"+ DiscordBot.minecraftServer.getMaxPlayerCount() + " Players"));
|
||||||
case "ip" -> jda.getPresence().setActivity(Activity.listening("Server IP: example.com"));
|
case "Uptime" -> jda.getPresence().setActivity(Activity.playing("for " + calculateUptime()));
|
||||||
case "uptime" -> jda.getPresence().setActivity(Activity.watching("Uptime: " + calculateUptime()));
|
|
||||||
default -> System.out.println("Invalid status type!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String calculateUptime() {
|
private static String calculateUptime() {
|
||||||
try {
|
try {
|
||||||
long secs = System.currentTimeMillis() - minecraftServer.getTicks() / 20;
|
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);
|
||||||
|
@ -147,15 +141,19 @@ public class DiscordBot extends ListenerAdapter {
|
||||||
}
|
}
|
||||||
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) {
|
} catch (Exception e) {
|
||||||
return "-";
|
return "-";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue