Skip to content

Commit

Permalink
添加在机器人在线状态中显示服务器状态的功能
Browse files Browse the repository at this point in the history
Fixes #95
  • Loading branch information
Xujiayao committed Jul 4, 2024
1 parent 7b9d9f7 commit abd788a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/xujiayao/discord_mc_chat/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public static class Generic {
public String language = "en_us";

public String botToken = "";

public boolean showServerStatusInBotStatus = true;
public String botPlayingActivity = "Minecraft (%onlinePlayerCount%/%maxPlayerCount%)";
public String botListeningActivity = "";

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/xujiayao/discord_mc_chat/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.xujiayao.discord_mc_chat.minecraft.MinecraftEventListener;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Webhook;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.requests.GatewayIntent;
Expand Down Expand Up @@ -88,6 +89,7 @@ public void onInitializeServer() {
.setMemberCachePolicy(MemberCachePolicy.ALL)
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.MESSAGE_CONTENT)
.addEventListeners(new DiscordEventListener())
.setStatus(CONFIG.generic.showServerStatusInBotStatus ? OnlineStatus.DO_NOT_DISTURB : OnlineStatus.ONLINE)
.build();

JDA.awaitReady();
Expand Down Expand Up @@ -174,7 +176,7 @@ public void onInitializeServer() {

SERVER = server;

Utils.setBotActivity();
Utils.setBotPresence();

Utils.initCheckUpdateTimer();

Expand All @@ -191,6 +193,12 @@ public void onInitializeServer() {
}
});

ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
if (CONFIG.generic.showServerStatusInBotStatus) {
JDA.getPresence().setStatus(OnlineStatus.DO_NOT_DISTURB);
}
});

ServerLifecycleEvents.SERVER_STOPPED.register(server -> {
MSPT_MONITOR_TIMER.cancel();
CHANNEL_TOPIC_MONITOR_TIMER.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public static void init() {
});

MinecraftEvents.PLAYER_JOIN.register(player -> {
Utils.setBotActivity();
Utils.setBotPresence();

if (CONFIG.generic.announcePlayerJoinLeave) {
CHANNEL.sendMessage(Translations.translateMessage("message.joinServer")
Expand All @@ -278,7 +278,7 @@ public static void init() {
});

MinecraftEvents.PLAYER_QUIT.register(player -> {
Utils.setBotActivity();
Utils.setBotPresence();

if (CONFIG.generic.announcePlayerJoinLeave) {
CHANNEL.sendMessage(Translations.translateMessage("message.leftServer")
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/xujiayao/discord_mc_chat/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import com.xujiayao.discord_mc_chat.multi_server.MultiServer;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
Expand Down Expand Up @@ -273,7 +274,7 @@ public static String reload() {
ConfigManager.init(true);
Translations.init();

Utils.setBotActivity();
Utils.setBotPresence();

CHANNEL = JDA.getTextChannelById(CONFIG.generic.channelId);
if (CHANNEL == null) {
Expand Down Expand Up @@ -517,11 +518,18 @@ public static String getStatsCommandMessage(String type, String name, boolean ha
return message.toString();
}

public static void setBotActivity() {
public static void setBotPresence() {
if (SERVER == null) {
// Bot is registered before official server start
return;
}

if (CONFIG.generic.showServerStatusInBotStatus && SERVER.getPlayerCount() == 0) {
JDA.getPresence().setStatus(OnlineStatus.IDLE);
} else {
JDA.getPresence().setStatus(OnlineStatus.ONLINE);
}

if (!CONFIG.generic.botPlayingActivity.isEmpty()) {
JDA.getPresence().setActivity(Activity.playing(CONFIG.generic.botPlayingActivity
.replace("%onlinePlayerCount%", Integer.toString(SERVER.getPlayerCount()))
Expand Down

0 comments on commit abd788a

Please sign in to comment.