Skip to content

Commit

Permalink
Merge pull request #169 from SpaceServerUniverse/dev/m1sk9-improve-di…
Browse files Browse the repository at this point in the history
…scord

feat: `!playerlist` の追加
  • Loading branch information
meyason authored Oct 25, 2024
2 parents 514b536 + c84569b commit b208108
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -39,17 +40,21 @@ public void onEnable(UniverseCoreV2 core) {
try {
jda.awaitReady();
} catch (InterruptedException e) {
throw new DiscordJDANotReadyException("JDA is not ready!");
throw new DiscordJDANotReadyException("JDA の初期化に失敗しました。詳しくは JDA のスタックトレースを確認してください");
}

Guild guild = jda.getGuildById(config.getDiscordGuildId());
if (guild == null) {
throw new DiscordGuildNotFoundException("Guild not found with ID: " + config.getDiscordGuildId());
throw new DiscordGuildNotFoundException("指定されたギルドIDは無効です: " + config.getDiscordGuildId());
}

TextChannel channel = guild.getTextChannelById(config.getDiscordChannelId());
if (channel == null) {
throw new DiscordChannelNotFoundException("Channel not found with ID: " + config.getDiscordChannelId());
throw new DiscordChannelNotFoundException("指定されたチャンネルIDは無効です: " + config.getDiscordChannelId());
}

if (channel.getType() != ChannelType.TEXT) {
throw new DiscordChannelNotFoundException("指定されたチャンネルIDはテキストチャンネルではありません: " + config.getDiscordChannelId());
}

new EventManager(core, channel);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.bukkit.entity.Player;
import space.yurisi.universecorev2.utils.Message;

import java.util.stream.Collectors;

public class UniverseDiscordMessage {

/** Minecraft へのメッセージ */
Expand All @@ -26,7 +28,7 @@ public static void sendMessageToMinecraft(Member discordMember, String discordMe
name_format = discordMember.getUser().getName() + " (" + discordMember.getNickname() + ")";
}

Bukkit.broadcast(Component.text("§a[Discord#" + name_format + "] " + "§f" + discordMessage));
Bukkit.broadcast(Component.text("§a[Discord] §r<" + name_format + "> " + discordMessage));
}


Expand All @@ -36,7 +38,7 @@ public static void sendMessageToDiscord(Player player, TextChannel discordChanne
Location location = player.getLocation();
String world_name = location.getWorld().getName();

discordChannel.sendMessage("[" + player.getName() + " | " + world_name + "]" + " " + message).queue();
discordChannel.sendMessage("[" + player.getName() + "@" + world_name + "]" + " " + message).queue();
}

public static void sendEventMessageToDiscord(TextChannel discordChannel, String message, int color) {
Expand All @@ -51,7 +53,7 @@ public static void sendJoinMessageToDiscord(Player player, TextChannel discordCh
EmbedBuilder embed = new EmbedBuilder();
embed.setTitle("入室");
embed.setDescription(player.getName() + " がログインしました");
embed.setThumbnail("https://mc-heads.net/avatar/" + player.getUniqueId() + "/100/nohelm.png");
embed.setThumbnail("https://mc-heads.net/head/" + player.getUniqueId());
embed.setColor(0x2AFF5C);

discordChannel.sendMessageEmbeds(embed.build()).queue();
Expand All @@ -61,10 +63,27 @@ public static void sendQuitMessageToDiscord(Player player, TextChannel discordCh
EmbedBuilder embed = new EmbedBuilder();
embed.setTitle("退出");
embed.setDescription(player.getName() + " がログアウトしました");
embed.setThumbnail("https://mc-heads.net/avatar/" + player.getUniqueId() + "/100/nohelm.png");
embed.setThumbnail("https://mc-heads.net/head/" + player.getUniqueId());
embed.setColor(0x687EFF);

discordChannel.sendMessageEmbeds(embed.build()).queue();
}

public static void sendStatusMessageToMinecraft(TextChannel discordChannel) {
String players = Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.collect(Collectors.joining(", "));
int online_players = Bukkit.getOnlinePlayers().size();
int max_players = Bukkit.getMaxPlayers();

EmbedBuilder embed = new EmbedBuilder();
embed.setTitle("サーバー情報");
embed.setDescription("```\n" + players + "\n```");
embed.setFooter("現在のプレイヤー数: " + online_players + " / " + max_players);
embed.setColor(0xFFA74B);

discordChannel.sendMessageEmbeds(embed.build()).queue();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public void onMessageReceived(MessageReceivedEvent event) {
return;
}

if (content.equals("!playerlist")) {
// すでに環境変数で指定されたチャンネルであることは確認済みなので, 暗黙的キャストでテキストチャンネル *として* 扱う
UniverseDiscordMessage.sendStatusMessageToMinecraft(event.getChannel().asTextChannel());
return;
}

UniverseDiscordMessage.sendMessageToMinecraft(member, content);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import space.yurisi.universecorev2.subplugins.universediscord.UniverseDiscordChannel;
import space.yurisi.universecorev2.subplugins.universediscord.UniverseDiscordMessage;
import space.yurisi.universecorev2.utils.Message;

Expand All @@ -28,13 +27,11 @@ public PlayerEvent(TextChannel discordChannel) {
@EventHandler(priority = EventPriority.HIGHEST)
public void onJoin(PlayerJoinEvent event) {
UniverseDiscordMessage.sendJoinMessageToDiscord(event.getPlayer(), discordChannel);
UniverseDiscordChannel.updateDiscordChannelTopic(discordChannel, event.getPlayer().getServer().getOnlinePlayers().size());
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onQuit(PlayerQuitEvent event) {
UniverseDiscordMessage.sendQuitMessageToDiscord(event.getPlayer(), discordChannel);
UniverseDiscordChannel.updateDiscordChannelTopic(discordChannel, event.getPlayer().getServer().getOnlinePlayers().size());
}

@EventHandler(priority = EventPriority.HIGHEST)
Expand Down

0 comments on commit b208108

Please sign in to comment.