From d042d0570f478f4365a1b6d2e6015f83c20096d1 Mon Sep 17 00:00:00 2001 From: Tomachi <8929706+book000@users.noreply.github.com> Date: Fri, 29 Jul 2022 15:38:08 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20JDA=20v5.0.0-alpha.17=E3=81=AB=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C=20(#293)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: JDA v5.0.0-alpha.17に対応 * fix: 対応が完全ではなかったので修正 * fix: emote -> emojiに変更 --- pom.xml | 2 +- .../java/com/jaoafa/javajaotan2/Main.java | 12 +- .../javajaotan2/command/Cmd_GameRole.java | 53 ++++----- .../javajaotan2/command/Cmd_Search.java | 14 ++- .../javajaotan2/command/Cmd_SearchImg.java | 14 ++- .../javajaotan2/command/Cmd_ToRandJa.java | 5 +- .../javajaotan2/event/Event_BugReaction.java | 40 +++---- .../event/Event_CheckNitroEmoji.java | 28 ++--- .../javajaotan2/event/Event_GameRole.java | 14 ++- .../javajaotan2/event/Event_Greeting.java | 12 +- .../javajaotan2/event/Event_MeetingVote.java | 31 ++--- .../javajaotan2/event/Event_NeedSupport.java | 5 +- .../javajaotan2/event/Event_PinReaction.java | 12 +- .../event/Event_VCSpeechLogLink.java | 6 +- .../javajaotan2/event/Event_WatchEmojis.java | 109 +++++++++++++----- .../com/jaoafa/javajaotan2/lib/GameRole.java | 10 +- .../javajaotan2/lib/JavajaotanCommand.java | 4 +- .../javajaotan2/lib/JavajaotanLibrary.java | 11 +- .../jaoafa/javajaotan2/lib/SubAccount.java | 11 +- .../com/jaoafa/javajaotan2/lib/Translate.java | 16 +-- .../jaoafa/javajaotan2/lib/WatchEmojis.java | 12 +- .../tasks/Task_SyncOtherServerPerm.java | 18 +-- 22 files changed, 252 insertions(+), 187 deletions(-) diff --git a/pom.xml b/pom.xml index 8d76de69..473105ac 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,7 @@ net.dv8tion JDA - 5.0.0-alpha.9 + 5.0.0-alpha.17 club.minnced diff --git a/src/main/java/com/jaoafa/javajaotan2/Main.java b/src/main/java/com/jaoafa/javajaotan2/Main.java index 2bc41776..3a942792 100644 --- a/src/main/java/com/jaoafa/javajaotan2/Main.java +++ b/src/main/java/com/jaoafa/javajaotan2/Main.java @@ -62,7 +62,7 @@ public class Main { static boolean isGuildDevelopMode = false; static long developUserId; static long developGuildId; - static Logger logger = LoggerFactory.getLogger("Javajaotan2"); + static final Logger logger = LoggerFactory.getLogger("Javajaotan2"); static JavajaotanConfig config; static JDA jda; static JSONArray commands; @@ -110,7 +110,7 @@ public static void main(String[] args) { try { JDABuilder jdabuilder = JDABuilder.createDefault(config.getToken()) .enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_PRESENCES, - GatewayIntent.GUILD_MESSAGE_TYPING, GatewayIntent.DIRECT_MESSAGE_TYPING, GatewayIntent.GUILD_EMOJIS) + GatewayIntent.GUILD_MESSAGE_TYPING, GatewayIntent.DIRECT_MESSAGE_TYPING, GatewayIntent.GUILD_EMOJIS_AND_STICKERS) .setAutoReconnect(true) .setBulkDeleteSplittingEnabled(false) .setContextEnabled(false); @@ -316,13 +316,12 @@ static void registerCommand(JDA jda) { commands.put(details); getLogger().info(String.format("%s: コマンドの登録に成功しました。", commandName)); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { + } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | + InvocationTargetException e) { getLogger().warn(String.format("%s: コマンドの登録に失敗しました。", commandName)); e.printStackTrace(); } } - - System.out.println(manager.getCommands()); } catch (InterruptedException | IOException | ClassNotFoundException e) { e.printStackTrace(); } @@ -351,7 +350,8 @@ static void registerEvent(JDABuilder jdaBuilder) { jdaBuilder.addEventListeners(instance); getLogger().info(String.format("%s: イベントの登録に成功しました。", eventName)); } - } catch (ClassNotFoundException | IOException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { + } catch (ClassNotFoundException | IOException | NoSuchMethodException | InstantiationException | + IllegalAccessException | InvocationTargetException e) { getLogger().error("イベントの登録に失敗しました。"); e.printStackTrace(); } diff --git a/src/main/java/com/jaoafa/javajaotan2/command/Cmd_GameRole.java b/src/main/java/com/jaoafa/javajaotan2/command/Cmd_GameRole.java index a53cfc4b..f36ad74a 100644 --- a/src/main/java/com/jaoafa/javajaotan2/command/Cmd_GameRole.java +++ b/src/main/java/com/jaoafa/javajaotan2/command/Cmd_GameRole.java @@ -21,6 +21,7 @@ import com.jaoafa.javajaotan2.lib.*; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji; import org.jetbrains.annotations.NotNull; import org.json.JSONArray; import org.json.JSONException; @@ -37,11 +38,11 @@ import java.util.stream.Collectors; public class Cmd_GameRole implements CommandPremise { - Path path = GameRole.getPath(); - Path pathMessages = GameRole.getPathMessages(); - Pattern emojiPattern = GameRole.getEmojiPattern(); - long SERVER_ID = GameRole.getServerId(); - long GAME_ROLE_BORDER_ID = GameRole.getGameRoleBorderId(); + final Path path = GameRole.getPath(); + final Path pathMessages = GameRole.getPathMessages(); + final Pattern emojiPattern = GameRole.getEmojiPattern(); + final long SERVER_ID = GameRole.getServerId(); + final long GAME_ROLE_BORDER_ID = GameRole.getGameRoleBorderId(); @Override public JavajaotanCommand.Detail details() { @@ -301,12 +302,12 @@ private void createGameRoleMessage(@NotNull Guild guild, @NotNull MessageChannel sb.append("所持しているゲームの絵文字をこのメッセージにリアクションすると、ゲームロールが付与されます。\n"); sb.append("ロールの剥奪(解除)は運営側では対応しません。`/gamerole take `(GAMENAMEはゲーム名)で解除できます。\n\n"); List usedEmojis = GameRole.getUsedGameEmojis(); - List emoteIds = guild.getEmotes() + List emojiIds = guild.getEmojis() .stream() - .map(Emote::getId) + .map(RichCustomEmoji::getId) .filter(s -> !usedEmojis.contains(s)) - .collect(Collectors.toList()); - List emotes = new ArrayList<>(); + .toList(); + List emojis = new ArrayList<>(); for (String gameId : games) { Role role = guild.getRoleById(gameId); @@ -315,10 +316,10 @@ private void createGameRoleMessage(@NotNull Guild guild, @NotNull MessageChannel } String gameName = role.getName(); String gameEmoji = GameRole.getGameEmoji(gameId); - while (gameEmoji == null || sb.toString().contains(gameEmoji) || jda.getEmoteById(gameEmoji) == null) { - gameEmoji = emoteIds.get(new Random().nextInt(emoteIds.size())); + while (gameEmoji == null || sb.toString().contains(gameEmoji) || jda.getEmojiById(gameEmoji) == null) { + gameEmoji = emojiIds.get(new Random().nextInt(emojiIds.size())); } - Emote emoji = jda.getEmoteById(gameEmoji); + RichCustomEmoji emoji = jda.getEmojiById(gameEmoji); if (emoji == null) { continue; } @@ -327,12 +328,12 @@ private void createGameRoleMessage(@NotNull Guild guild, @NotNull MessageChannel .append(" ") .append(gameName) .append("\n"); - emotes.add(emoji); + emojis.add(emoji); } Message postMessage = channel.sendMessage(sb.toString()).complete(); - for (Emote emote : emotes) { - postMessage.addReaction(emote).queue(); + for (RichCustomEmoji emoji : emojis) { + postMessage.addReaction(emoji).queue(); } addMessage(postMessage); } @@ -371,7 +372,7 @@ private void changeGameRoleEmoji(@NotNull Guild guild, @NotNull MessageChannel c if (matcher.matches()) { emojiId = matcher.group(1); } - Emote emoji = jda.getEmoteById(emojiId); + RichCustomEmoji emoji = jda.getEmojiById(emojiId); if (emoji == null) { message.reply("指定された絵文字が見つかりませんでした。").queue(); return; @@ -498,12 +499,12 @@ private void updateMessages() { sb.append("所持しているゲームの絵文字をこのメッセージにリアクションすると、ゲームロールが付与されます。\n"); sb.append("ロールの剥奪(解除)は運営側では対応しません。`/gamerole take `(GAMENAMEはゲーム名)で解除できます。\n\n"); List usedEmojis = GameRole.getUsedGameEmojis(); - List emoteIds = guild.getEmotes() + List emojiIds = guild.getEmojis() .stream() - .map(Emote::getId) + .map(RichCustomEmoji::getId) .filter(s -> !usedEmojis.contains(s)) - .collect(Collectors.toList()); - List emotes = new ArrayList<>(); + .toList(); + List emojis = new ArrayList<>(); for (String gameId : gameRoles) { Role role = guild.getRoleById(gameId); @@ -512,10 +513,10 @@ private void updateMessages() { } String gameName = role.getName(); String gameEmoji = GameRole.getGameEmoji(gameId); - while (gameEmoji == null || sb.toString().contains(gameEmoji) || jda.getEmoteById(gameEmoji) == null) { - gameEmoji = emoteIds.get(new Random().nextInt(emoteIds.size())); + while (gameEmoji == null || sb.toString().contains(gameEmoji) || jda.getEmojiById(gameEmoji) == null) { + gameEmoji = emojiIds.get(new Random().nextInt(emojiIds.size())); } - Emote emoji = jda.getEmoteById(gameEmoji); + RichCustomEmoji emoji = jda.getEmojiById(gameEmoji); if (emoji == null) { continue; } @@ -524,7 +525,7 @@ private void updateMessages() { .append(" ") .append(gameName) .append("\n"); - emotes.add(emoji); + emojis.add(emoji); } JSONArray messages = games.getJSONArray("messages"); for (int i = 0; i < messages.length(); i++) { @@ -538,8 +539,8 @@ private void updateMessages() { String content = sb.toString(); Message message = channel.editMessageById(messageId, content).complete(); message.clearReactions().complete(); - for (Emote emote : emotes) { - message.addReaction(emote).queue(); + for (RichCustomEmoji emoji : emojis) { + message.addReaction(emoji).queue(); } } } diff --git a/src/main/java/com/jaoafa/javajaotan2/command/Cmd_Search.java b/src/main/java/com/jaoafa/javajaotan2/command/Cmd_Search.java index a9bee304..b0ed8362 100644 --- a/src/main/java/com/jaoafa/javajaotan2/command/Cmd_Search.java +++ b/src/main/java/com/jaoafa/javajaotan2/command/Cmd_Search.java @@ -1,7 +1,7 @@ /* * jaoLicense * - * Copyright (c) 2021 jao Minecraft Server + * Copyright (c) 2022 jao Minecraft Server * * The following license applies to this project: jaoLicense * @@ -95,12 +95,14 @@ SearchResult customSearch(String gcpKey, String cx, String text) { try { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); - Response response = client.newCall(request).execute(); - ResponseBody body = response.body(); - if (body == null) { - return null; + JSONObject object; + try (Response response = client.newCall(request).execute()) { + ResponseBody body = response.body(); + if (body == null) { + return null; + } + object = new JSONObject(body.string()); } - JSONObject object = new JSONObject(body.string()); if (!object.has("searchInformation") || !object.has("items")) { return null; } diff --git a/src/main/java/com/jaoafa/javajaotan2/command/Cmd_SearchImg.java b/src/main/java/com/jaoafa/javajaotan2/command/Cmd_SearchImg.java index 22ed8931..f0b13d8e 100644 --- a/src/main/java/com/jaoafa/javajaotan2/command/Cmd_SearchImg.java +++ b/src/main/java/com/jaoafa/javajaotan2/command/Cmd_SearchImg.java @@ -1,7 +1,7 @@ /* * jaoLicense * - * Copyright (c) 2021 jao Minecraft Server + * Copyright (c) 2022 jao Minecraft Server * * The following license applies to this project: jaoLicense * @@ -98,12 +98,14 @@ SearchResult customSearch(String gcpKey, String cx, String text) { try { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); - Response response = client.newCall(request).execute(); - ResponseBody body = response.body(); - if (body == null) { - return null; + JSONObject object; + try (Response response = client.newCall(request).execute()) { + ResponseBody body = response.body(); + if (body == null) { + return null; + } + object = new JSONObject(body.string()); } - JSONObject object = new JSONObject(body.string()); if (!object.has("searchInformation") || !object.has("items")) { return null; } diff --git a/src/main/java/com/jaoafa/javajaotan2/command/Cmd_ToRandJa.java b/src/main/java/com/jaoafa/javajaotan2/command/Cmd_ToRandJa.java index 68d2bafd..bef62b4c 100644 --- a/src/main/java/com/jaoafa/javajaotan2/command/Cmd_ToRandJa.java +++ b/src/main/java/com/jaoafa/javajaotan2/command/Cmd_ToRandJa.java @@ -1,7 +1,7 @@ /* * jaoLicense * - * Copyright (c) 2021 jao Minecraft Server + * Copyright (c) 2022 jao Minecraft Server * * The following license applies to this project: jaoLicense * @@ -32,7 +32,6 @@ import java.util.Arrays; import java.util.List; import java.util.Random; -import java.util.stream.Collectors; public class Cmd_ToRandJa implements CommandPremise { @Override @@ -62,7 +61,7 @@ private void translateRandJa(@NotNull Guild guild, @NotNull MessageChannel chann List ignoreAutoUnknown = Arrays.stream(Translate.Language.values()) .filter(l -> l != Translate.Language.AUTO) .filter(l -> l != Translate.Language.UNKNOWN) - .collect(Collectors.toList()); + .toList(); Translate.Language lang1 = ignoreAutoUnknown.get(new Random().nextInt(ignoreAutoUnknown.size())); Translate.Language lang2 = Translate.Language.JA; diff --git a/src/main/java/com/jaoafa/javajaotan2/event/Event_BugReaction.java b/src/main/java/com/jaoafa/javajaotan2/event/Event_BugReaction.java index 21251979..e37458b5 100644 --- a/src/main/java/com/jaoafa/javajaotan2/event/Event_BugReaction.java +++ b/src/main/java/com/jaoafa/javajaotan2/event/Event_BugReaction.java @@ -16,7 +16,13 @@ import com.jaoafa.javajaotan2.lib.JavajaotanLibrary; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.MessageBuilder; -import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.entities.ThreadChannel; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; +import net.dv8tion.jda.api.entities.emoji.Emoji; +import net.dv8tion.jda.api.entities.emoji.EmojiUnion; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; @@ -31,8 +37,8 @@ import java.util.concurrent.TimeUnit; public class Event_BugReaction extends ListenerAdapter { - String targetReaction = "\uD83D\uDC1B"; // :bug: - String repo = "jaoafa/jao-Minecraft-Server"; + final String targetReaction = "\uD83D\uDC1B"; // :bug: + final String repo = "jaoafa/jao-Minecraft-Server"; @Override public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { @@ -43,19 +49,19 @@ public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { if (user == null) user = event.retrieveUser().complete(); if (user.isBot()) return; - MessageReaction.ReactionEmote emote = event.getReactionEmote(); + EmojiUnion emoji = event.getEmoji(); - if (!emote.isEmoji()) return; - if (!emote.getEmoji().equals(targetReaction)) return; + if (emoji.getType() != Emoji.Type.UNICODE) return; + if (!emoji.asUnicode().getName().equals(targetReaction)) return; Message message = event.retrieveMessage().complete(); - List users = message.retrieveReactionUsers(targetReaction).complete(); + List users = message.retrieveReactionUsers(Emoji.fromUnicode(targetReaction)).complete(); if (users.size() != 1) { // 1人以外 = 0もしくは2人以上 = 既に報告済み return; } - TextChannel channel = event.getTextChannel(); + MessageChannelUnion channel = event.getChannel(); // Issueを作成する ZonedDateTime createdAt = message.getTimeCreated().atZoneSameInstant(ZoneId.of("Asia/Tokyo")); @@ -93,22 +99,6 @@ public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { return; } - String mainMessage = """ - ## 不具合と思われるメッセージ (または報告) - - %s - - URL: %s - - ## 不具合報告者 - - %s - """.formatted( - createdAt.format(formatter) + " に送信された " + message.getAuthor().getAsMention() + " による " + message.getChannel().getAsMention() + " でのメッセージ", - message.getJumpUrl(), - user.getAsMention() - ); - List messages = new ArrayList<>(); if (responseType == JavajaotanLibrary.IssueResponseType.SUCCESS) { messages.add("[LINKED-ISSUE:jaoafa/jao-Minecraft-Server#" + issueNumber + "]"); @@ -135,6 +125,6 @@ public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { .setEmbeds(embed.build()) .build()).queue(); - message.addReaction(targetReaction).queue(); + message.addReaction(Emoji.fromUnicode(targetReaction)).queue(); } } diff --git a/src/main/java/com/jaoafa/javajaotan2/event/Event_CheckNitroEmoji.java b/src/main/java/com/jaoafa/javajaotan2/event/Event_CheckNitroEmoji.java index 1954b2ff..9ba8e7bc 100644 --- a/src/main/java/com/jaoafa/javajaotan2/event/Event_CheckNitroEmoji.java +++ b/src/main/java/com/jaoafa/javajaotan2/event/Event_CheckNitroEmoji.java @@ -15,8 +15,10 @@ import com.jaoafa.javajaotan2.lib.JavajaotanLibrary; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.*; -import net.dv8tion.jda.api.events.emote.EmoteAddedEvent; -import net.dv8tion.jda.api.events.emote.EmoteRemovedEvent; +import net.dv8tion.jda.api.entities.emoji.CustomEmoji; +import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji; +import net.dv8tion.jda.api.events.emoji.EmojiAddedEvent; +import net.dv8tion.jda.api.events.emoji.EmojiRemovedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; @@ -31,7 +33,7 @@ import java.util.List; public class Event_CheckNitroEmoji extends ListenerAdapter { - List guildEmojis = null; + List guildEmojis = null; @Override public void onMessageReceived(@Nonnull MessageReceivedEvent event) { @@ -45,14 +47,14 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) { } Guild guild = event.getGuild(); if (guildEmojis == null) { - guildEmojis = guild.retrieveEmotes().complete(); + guildEmojis = guild.retrieveEmojis().complete(); } - List emotes = message.getEmotes(); + List emojis = message.getMentions().getCustomEmojis(); // GIF絵文字を使用しているか - boolean usingAnimated = emotes.stream().anyMatch(Emote::isAnimated); + boolean usingAnimated = emojis.stream().anyMatch(CustomEmoji::isAnimated); // いずれかの絵文字が、このGuildにはない絵文字を利用しているかどうか - boolean isOtherGuildEmoji = emotes + boolean isOtherGuildEmoji = emojis .stream() .anyMatch(e -> guildEmojis .stream() @@ -90,24 +92,24 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) { } @Override - public void onEmoteAdded(@NotNull EmoteAddedEvent event) { + public void onEmojiAdded(@NotNull EmojiAddedEvent event) { if (Main.getConfig().getGuildId() != event.getGuild().getIdLong()) { return; } - Main.getLogger().info("Emote added (Cache refresh): " + event.getEmote().getName()); - event.getGuild().retrieveEmotes().queue( + Main.getLogger().info("Emoji added (Cache refresh): " + event.getEmoji().getName()); + event.getGuild().retrieveEmojis().queue( emojis -> guildEmojis = emojis, Throwable::printStackTrace ); } @Override - public void onEmoteRemoved(@NotNull EmoteRemovedEvent event) { + public void onEmojiRemoved(@NotNull EmojiRemovedEvent event) { if (Main.getConfig().getGuildId() != event.getGuild().getIdLong()) { return; } - Main.getLogger().info("Emote removed (Cache refresh): " + event.getEmote().getName()); - event.getGuild().retrieveEmotes().queue( + Main.getLogger().info("Emoji removed (Cache refresh): " + event.getEmoji().getName()); + event.getGuild().retrieveEmojis().queue( emojis -> guildEmojis = emojis, Throwable::printStackTrace ); diff --git a/src/main/java/com/jaoafa/javajaotan2/event/Event_GameRole.java b/src/main/java/com/jaoafa/javajaotan2/event/Event_GameRole.java index 89e5ced0..bd4d730a 100644 --- a/src/main/java/com/jaoafa/javajaotan2/event/Event_GameRole.java +++ b/src/main/java/com/jaoafa/javajaotan2/event/Event_GameRole.java @@ -15,6 +15,8 @@ import com.jaoafa.javajaotan2.lib.GameRole; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; +import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -23,7 +25,7 @@ import java.util.concurrent.TimeUnit; public class Event_GameRole extends ListenerAdapter { - long SERVER_ID = GameRole.getServerId(); + final long SERVER_ID = GameRole.getServerId(); @Override public void onMessageReactionAdd(@Nonnull MessageReactionAddEvent event) { @@ -35,7 +37,7 @@ public void onMessageReactionAdd(@Nonnull MessageReactionAddEvent event) { return; } - TextChannel channel = event.getTextChannel(); + MessageChannelUnion channel = event.getChannel(); Message message = event.retrieveMessage().complete(); if (!GameRole.isGameRoleMessage(message)) { return; @@ -43,12 +45,16 @@ public void onMessageReactionAdd(@Nonnull MessageReactionAddEvent event) { MessageReaction reaction = event.getReaction(); reaction.removeReaction(user).queue(); + if (reaction.getEmoji().getType() != Emoji.Type.CUSTOM) { + return; + } + List emojis = GameRole.getUsedGameEmojis(); - if (!emojis.contains(reaction.getReactionEmote().getId())) { + if (!emojis.contains(reaction.getEmoji().asCustom().getId())) { return; } - String gameRoleId = GameRole.getGameEmojiFromRoleId(reaction.getReactionEmote().getId()); + String gameRoleId = GameRole.getGameEmojiFromRoleId(reaction.getEmoji().asCustom().getId()); if (gameRoleId == null) { return; } diff --git a/src/main/java/com/jaoafa/javajaotan2/event/Event_Greeting.java b/src/main/java/com/jaoafa/javajaotan2/event/Event_Greeting.java index 1838ae2c..bda6f2ce 100644 --- a/src/main/java/com/jaoafa/javajaotan2/event/Event_Greeting.java +++ b/src/main/java/com/jaoafa/javajaotan2/event/Event_Greeting.java @@ -15,6 +15,8 @@ import com.jaoafa.javajaotan2.lib.Channels; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; +import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -42,7 +44,7 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) { JDA jda = event.getJDA(); Message message = event.getMessage(); Guild guild = event.getGuild(); - TextChannel channel = event.getTextChannel(); + MessageChannelUnion channel = event.getChannel(); String content = message.getContentRaw(); Member member = event.getMember(); if (member == null) { @@ -60,19 +62,19 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) { if (content.equals("jao")) { List roles = member.getRoles().stream().filter(_role -> _role.getIdLong() == role.getIdLong()).toList(); if (roles.size() == 0) { - message.addReaction("\u2753").queue(); // ? + message.addReaction(Emoji.fromUnicode("\u2753")).queue(); // ? jaoPlayers.add(member.getIdLong()); } else { - message.addReaction("\u274C").queue(); // x + message.addReaction(Emoji.fromUnicode("\u274C")).queue(); // x } return; } if (!jaoPlayers.contains(member.getIdLong())) { - message.addReaction("\u274C").queue(); // x + message.addReaction(Emoji.fromUnicode("\u274C")).queue(); // x return; } guild.addRoleToMember(member, role).queue(); - message.addReaction("\u2B55").queue(); // o + message.addReaction(Emoji.fromUnicode("\u2B55")).queue(); // o message.reply(""" あいさつしていただきありがとうございます!これにより、多くのチャンネルを閲覧できるようになりました。 このあとは<#597419057251090443>などで__**「`/link`」を実行(投稿)して、MinecraftアカウントとDiscordアカウントを連携**__しましょう! diff --git a/src/main/java/com/jaoafa/javajaotan2/event/Event_MeetingVote.java b/src/main/java/com/jaoafa/javajaotan2/event/Event_MeetingVote.java index 7962aba1..d324d292 100644 --- a/src/main/java/com/jaoafa/javajaotan2/event/Event_MeetingVote.java +++ b/src/main/java/com/jaoafa/javajaotan2/event/Event_MeetingVote.java @@ -20,6 +20,9 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; +import net.dv8tion.jda.api.entities.emoji.Emoji; +import net.dv8tion.jda.api.entities.emoji.EmojiUnion; import net.dv8tion.jda.api.events.ReadyEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; @@ -98,11 +101,11 @@ public class Event_MeetingVote extends ListenerAdapter { /** clubjaoafa からの自治体関連リクエストプレフィックス */ static final String API_CITIES_PREFIX = "[API-CITIES-"; /** 自治体新規作成リクエスト管理テキストパターン */ - static final Pattern CITIES_CREATE_WAITING_PATTERN = Pattern.compile("\\[API-CITIES-CREATE-WAITING:([0-9]+)]"); + static final Pattern CITIES_CREATE_WAITING_PATTERN = Pattern.compile("\\[API-CITIES-CREATE-WAITING:(\\d+)]"); /** 自治体範囲変更リクエスト管理テキストパターン */ - static final Pattern CITIES_CORNERS_WAITING_PATTERN = Pattern.compile("\\[API-CITIES-CHANGE-CORNERS-WAITING:([0-9]+)]"); + static final Pattern CITIES_CORNERS_WAITING_PATTERN = Pattern.compile("\\[API-CITIES-CHANGE-CORNERS-WAITING:(\\d+)]"); /** 自治体情報変更リクエスト管理テキストパターン */ - static final Pattern CITIES_CHANGE_OTHER_WAITING_PATTERN = Pattern.compile("\\[API-CITIES-CHANGE-OTHER-WAITING:([0-9]+)]"); + static final Pattern CITIES_CHANGE_OTHER_WAITING_PATTERN = Pattern.compile("\\[API-CITIES-CHANGE-OTHER-WAITING:(\\d+)]"); public void initChannels(JDA jda) { this.logger = Main.getLogger(); @@ -122,7 +125,7 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) { if (!event.isFromType(ChannelType.TEXT)) return; User author = event.getAuthor(); - TextChannel channel = event.getTextChannel(); + MessageChannelUnion channel = event.getChannel(); Message message = event.getMessage(); String content = message.getContentRaw(); @@ -179,12 +182,12 @@ public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { if (user == null) user = event.retrieveUser().complete(); if (user.isBot()) return; - MessageReaction.ReactionEmote emote = event.getReactionEmote(); + EmojiUnion emoji = event.getEmoji(); - if (!emote.isEmoji()) return; - if (!emote.getEmoji().equals(VoteReaction.GOOD.getUnicode()) - && !emote.getEmoji().equals(VoteReaction.BAD.getUnicode()) - && !emote.getEmoji().equals(VoteReaction.WHITE.getUnicode())) + if (emoji.getType() != Emoji.Type.UNICODE) return; + if (!emoji.asUnicode().getName().equals(VoteReaction.GOOD.getUnicode()) + && !emoji.asUnicode().getName().equals(VoteReaction.BAD.getUnicode()) + && !emoji.asUnicode().getName().equals(VoteReaction.WHITE.getUnicode())) return; Message message = event.retrieveMessage().complete(); @@ -671,7 +674,7 @@ public String getUnicode() { * @param message メッセージ */ public void addReaction(@NotNull Message message) { - message.addReaction(getUnicode()).queue(); + message.addReaction(Emoji.fromUnicode(getUnicode())).queue(); } /** @@ -683,7 +686,7 @@ public void addReaction(@NotNull Message message) { */ public List getUsers(@NotNull Message message) { return message - .retrieveReactionUsers(getUnicode()) + .retrieveReactionUsers(Emoji.fromUnicode(getUnicode())) .complete() .stream() .filter(u -> !u.isBot()) @@ -699,7 +702,7 @@ public List getUsers(@NotNull Message message) { */ public List getUsersIncludeBot(@NotNull Message message) { return new ArrayList<>(message - .retrieveReactionUsers(getUnicode()) + .retrieveReactionUsers(Emoji.fromUnicode(getUnicode())) .complete()); } @@ -713,7 +716,7 @@ public List getUsersIncludeBot(@NotNull Message message) { */ public boolean isReacted(Message message, User user) { return message - .retrieveReactionUsers(getUnicode()) + .retrieveReactionUsers(Emoji.fromUnicode(getUnicode())) .stream() .anyMatch(u -> u.getIdLong() == user.getIdLong()); } @@ -748,7 +751,7 @@ ZonedDateTime getVoteLimitDateTime(Message message) { * メッセージテキストと白票数から投票ボーダーを算出 */ int getVoteBorderFromContent(String content, int white_count) { - Matcher m = Pattern.compile("\\[Border:([0-9]+)]").matcher(content); + Matcher m = Pattern.compile("\\[Border:(\\d+)]").matcher(content); if (m.find() && JavajaotanLibrary.isInt(m.group(1))) return Integer.parseInt(m.group(1)); diff --git a/src/main/java/com/jaoafa/javajaotan2/event/Event_NeedSupport.java b/src/main/java/com/jaoafa/javajaotan2/event/Event_NeedSupport.java index 9d23f0b2..e6353181 100644 --- a/src/main/java/com/jaoafa/javajaotan2/event/Event_NeedSupport.java +++ b/src/main/java/com/jaoafa/javajaotan2/event/Event_NeedSupport.java @@ -1,7 +1,7 @@ /* * jaoLicense * - * Copyright (c) 2021 jao Minecraft Server + * Copyright (c) 2022 jao Minecraft Server * * The following license applies to this project: jaoLicense * @@ -14,6 +14,7 @@ import com.jaoafa.javajaotan2.Main; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -58,6 +59,6 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) { return; } guild.addRoleToMember(member, needSupport).queue(); - message.addReaction("\uD83D\uDC40").queue(); // :eyes: + message.addReaction(Emoji.fromUnicode("\uD83D\uDC40")).queue(); // :eyes: } } diff --git a/src/main/java/com/jaoafa/javajaotan2/event/Event_PinReaction.java b/src/main/java/com/jaoafa/javajaotan2/event/Event_PinReaction.java index 31aaa430..399c3a7e 100644 --- a/src/main/java/com/jaoafa/javajaotan2/event/Event_PinReaction.java +++ b/src/main/java/com/jaoafa/javajaotan2/event/Event_PinReaction.java @@ -16,6 +16,8 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.MessageReaction; import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.emoji.Emoji; +import net.dv8tion.jda.api.entities.emoji.EmojiUnion; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; @@ -28,17 +30,17 @@ public class Event_PinReaction extends ListenerAdapter { public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { Message message = event.retrieveMessage().complete(); MessageReaction reaction = event.getReaction(); - MessageReaction.ReactionEmote reactionEmote = reaction.getReactionEmote(); - if (!reactionEmote.isEmoji()) { + EmojiUnion emoji = reaction.getEmoji(); + if (emoji.getType() != EmojiUnion.Type.UNICODE) { return; } - if (!reactionEmote.getEmoji().equals("📌")) { + if (!emoji.getName().equals("📌")) { return; } if (message.isPinned()) { return; } - List users = message.retrieveReactionUsers("📌").complete(); + List users = message.retrieveReactionUsers(Emoji.fromUnicode("📌")).complete(); if (users.size() < 2) { return; } @@ -50,7 +52,7 @@ public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { return; } message.pin().queue( - s -> message.addReaction("📌").queue(), + s -> message.addReaction(Emoji.fromUnicode("📌")).queue(), e -> event .getChannel() .sendMessage("<@" + member.getId() + "> Failed to pin message: " + e.getMessage()) diff --git a/src/main/java/com/jaoafa/javajaotan2/event/Event_VCSpeechLogLink.java b/src/main/java/com/jaoafa/javajaotan2/event/Event_VCSpeechLogLink.java index 6c8c812b..316caf50 100644 --- a/src/main/java/com/jaoafa/javajaotan2/event/Event_VCSpeechLogLink.java +++ b/src/main/java/com/jaoafa/javajaotan2/event/Event_VCSpeechLogLink.java @@ -16,7 +16,7 @@ import net.dv8tion.jda.api.entities.ChannelType; import net.dv8tion.jda.api.entities.GuildMessageChannel; import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -25,14 +25,14 @@ import java.util.regex.Pattern; public class Event_VCSpeechLogLink extends ListenerAdapter { - final Pattern messageUrlPattern = Pattern.compile("^https://.*?discord\\.com/channels/([0-9]+)/([0-9]+)/([0-9]+)\\??(.*)$", Pattern.CASE_INSENSITIVE); + final Pattern messageUrlPattern = Pattern.compile("^https://.*?discord\\.com/channels/(\\d+)/(\\d+)/(\\d+)\\??(.*)$", Pattern.CASE_INSENSITIVE); @Override public void onMessageReceived(@Nonnull MessageReceivedEvent event) { if (!event.isFromType(ChannelType.TEXT)) { return; } - TextChannel channel = event.getTextChannel(); + MessageChannelUnion channel = event.getChannel(); Message message = event.getMessage(); String content = message.getContentRaw(); Matcher match = messageUrlPattern.matcher(content); diff --git a/src/main/java/com/jaoafa/javajaotan2/event/Event_WatchEmojis.java b/src/main/java/com/jaoafa/javajaotan2/event/Event_WatchEmojis.java index a10280ac..8c1c14d1 100644 --- a/src/main/java/com/jaoafa/javajaotan2/event/Event_WatchEmojis.java +++ b/src/main/java/com/jaoafa/javajaotan2/event/Event_WatchEmojis.java @@ -1,7 +1,7 @@ /* * jaoLicense * - * Copyright (c) 2021 jao Minecraft Server + * Copyright (c) 2022 jao Minecraft Server * * The following license applies to this project: jaoLicense * @@ -18,24 +18,30 @@ import net.dv8tion.jda.api.audit.ActionType; import net.dv8tion.jda.api.audit.AuditLogEntry; import net.dv8tion.jda.api.entities.*; -import net.dv8tion.jda.api.events.emote.EmoteAddedEvent; -import net.dv8tion.jda.api.events.emote.EmoteRemovedEvent; -import net.dv8tion.jda.api.events.emote.update.EmoteUpdateNameEvent; +import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji; +import net.dv8tion.jda.api.events.emoji.EmojiAddedEvent; +import net.dv8tion.jda.api.events.emoji.EmojiRemovedEvent; +import net.dv8tion.jda.api.events.emoji.update.EmojiUpdateNameEvent; +import net.dv8tion.jda.api.events.emoji.update.EmojiUpdateRolesEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; import java.time.Instant; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; public class Event_WatchEmojis extends ListenerAdapter { @Override - public void onEmoteAdded(@NotNull EmoteAddedEvent event) { + public void onEmojiAdded(@NotNull EmojiAddedEvent event) { JDA jda = event.getJDA(); Guild guild = event.getGuild(); - Emote emote = event.getEmote(); - ListedEmote listedEmote = event.getGuild().retrieveEmoteById(emote.getIdLong()).complete(); - User user = listedEmote.getUser(); + RichCustomEmoji emoji = event.getGuild().retrieveEmoji(event.getEmoji()).complete(); + User user = emoji.getOwner(); + if (user == null) { + Main.getLogger().warn("EmojiAddedEvent#onEmojiAdded: User is null"); + return; + } Optional optGuild = Main.getWatchEmojis().getEmojiGuild(guild); if (optGuild.isEmpty()) { @@ -50,8 +56,8 @@ public void onEmoteAdded(@NotNull EmoteAddedEvent event) { } MessageEmbed embed = new EmbedBuilder() .setAuthor(user.getAsTag(), "https://discord.com/users/" + user.getId(), user.getAvatarUrl()) - .setTitle(String.format(":new:NEW EMOJI : %s (`:%s:`)", emote.getAsMention(), emote.getName())) - .setThumbnail(emote.getImageUrl()) + .setTitle(String.format(":new:NEW EMOJI : %s (`:%s:`)", emoji.getAsMention(), emoji.getName())) + .setThumbnail(emoji.getImageUrl()) .setTimestamp(Instant.now()) .build(); log_channel.sendMessageEmbeds(embed).queue(); @@ -60,11 +66,41 @@ public void onEmoteAdded(@NotNull EmoteAddedEvent event) { } @Override - public void onEmoteUpdateName(@NotNull EmoteUpdateNameEvent event) { - JDA jda = event.getJDA(); - Guild guild = event.getGuild(); - Emote emote = event.getEmote(); + public void onEmojiUpdateName(@NotNull EmojiUpdateNameEvent event) { + onEmojiUpdate( + event.getJDA(), + event.getGuild(), + event.getEmoji(), + new EmojiUpdateRecord( + EmojiUpdateType.NAME, + event.getOldName(), + event.getNewName() + ) + ); + } + + @Override + public void onEmojiUpdateRoles(@NotNull EmojiUpdateRolesEvent event) { + onEmojiUpdate( + event.getJDA(), + event.getGuild(), + event.getEmoji(), + new EmojiUpdateRecord( + EmojiUpdateType.ROLES, + rolesToString(event.getOldRoles()), + rolesToString(event.getNewRoles()) + ) + ); + } + + String rolesToString(List roles) { + return roles + .stream() + .map(Role::getName) + .collect(Collectors.joining(", ")); + } + void onEmojiUpdate(JDA jda, Guild guild, RichCustomEmoji emoji, EmojiUpdateRecord record) { Optional optGuild = Main.getWatchEmojis().getEmojiGuild(guild); if (optGuild.isEmpty()) { return; @@ -77,34 +113,37 @@ public void onEmoteUpdateName(@NotNull EmoteUpdateNameEvent event) { return; } - List entries = guild.retrieveAuditLogs().type(ActionType.EMOTE_UPDATE).limit(5).complete(); + List entries = guild.retrieveAuditLogs().type(ActionType.EMOJI_UPDATE).limit(5).complete(); User user = null; if (!entries.isEmpty()) { for (AuditLogEntry entry : entries) { - if (entry.getTargetIdLong() != emote.getIdLong()) { + if (entry.getTargetIdLong() != emoji.getIdLong()) { continue; } user = entry.getUser(); } } - EmbedBuilder embed = new EmbedBuilder() - .setTitle(String.format(":repeat:CHANGE EMOJI : %s (`:%s:` -> `:%s:`)", emote.getAsMention(), event.getOldName(), event.getNewName())) - .setThumbnail(emote.getImageUrl()) + EmbedBuilder builder = new EmbedBuilder() + .setTitle(":repeat:UPDATED EMOJI(%s) : %s".formatted(record.type.name(), emoji.getAsMention())) + .setThumbnail(emoji.getImageUrl()) + .addField("Old", record.oldValue, true) + .addField("New", record.newValue, true) .setTimestamp(Instant.now()); - if(user != null) { - embed.setAuthor(user.getAsTag(), "https://discord.com/users/" + user.getId(), user.getAvatarUrl()); + + if (user != null) { + builder.setAuthor(user.getAsTag(), "https://discord.com/users/" + user.getId(), user.getAvatarUrl()); } - log_channel.sendMessageEmbeds(embed.build()).queue(); + log_channel.sendMessageEmbeds(builder.build()).queue(); emojiGuild.generateEmojiList(jda); } @Override - public void onEmoteRemoved(@NotNull EmoteRemovedEvent event) { + public void onEmojiRemoved(@NotNull EmojiRemovedEvent event) { JDA jda = event.getJDA(); Guild guild = event.getGuild(); - Emote emote = event.getEmote(); + RichCustomEmoji emoji = event.getEmoji(); Optional optGuild = Main.getWatchEmojis().getEmojiGuild(guild); if (optGuild.isEmpty()) { @@ -118,11 +157,11 @@ public void onEmoteRemoved(@NotNull EmoteRemovedEvent event) { return; } - List entries = guild.retrieveAuditLogs().type(ActionType.EMOTE_DELETE).limit(5).complete(); + List entries = guild.retrieveAuditLogs().type(ActionType.EMOJI_DELETE).limit(5).complete(); User user = null; if (!entries.isEmpty()) { for (AuditLogEntry entry : entries) { - if (entry.getTargetIdLong() != emote.getIdLong()) { + if (entry.getTargetIdLong() != emoji.getIdLong()) { continue; } user = entry.getUser(); @@ -130,14 +169,26 @@ public void onEmoteRemoved(@NotNull EmoteRemovedEvent event) { } EmbedBuilder embed = new EmbedBuilder() - .setTitle(String.format(":wave:DELETED EMOJI : (`:%s:`)", emote.getName())) - .setThumbnail(emote.getImageUrl()) + .setTitle(String.format(":wave:DELETED EMOJI : (`:%s:`)", emoji.getName())) + .setThumbnail(emoji.getImageUrl()) .setTimestamp(Instant.now()); - if(user != null) { + if (user != null) { embed.setAuthor(user.getAsTag(), "https://discord.com/users/" + user.getId(), user.getAvatarUrl()); } log_channel.sendMessageEmbeds(embed.build()).queue(); emojiGuild.generateEmojiList(jda); } + + record EmojiUpdateRecord( + EmojiUpdateType type, + String oldValue, + String newValue + ) { + } + + enum EmojiUpdateType { + NAME, + ROLES + } } diff --git a/src/main/java/com/jaoafa/javajaotan2/lib/GameRole.java b/src/main/java/com/jaoafa/javajaotan2/lib/GameRole.java index 5b4e22f2..7ee399b0 100644 --- a/src/main/java/com/jaoafa/javajaotan2/lib/GameRole.java +++ b/src/main/java/com/jaoafa/javajaotan2/lib/GameRole.java @@ -27,11 +27,11 @@ import java.util.stream.Collectors; public class GameRole { - static Path path = Path.of("gameRoles.json"); - static Path pathMessages = Path.of("gameRoleMessages.json"); - static Pattern emojiPattern = Pattern.compile("<:.+?:(\\d+)>"); - static long SERVER_ID = 597378876556967936L; - static long GAME_ROLE_BORDER_ID = 911556139496374293L; + static final Path path = Path.of("gameRoles.json"); + static final Path pathMessages = Path.of("gameRoleMessages.json"); + static final Pattern emojiPattern = Pattern.compile("<:.+?:(\\d+)>"); + static final long SERVER_ID = 597378876556967936L; + static final long GAME_ROLE_BORDER_ID = 911556139496374293L; public static long getServerId() { return SERVER_ID; diff --git a/src/main/java/com/jaoafa/javajaotan2/lib/JavajaotanCommand.java b/src/main/java/com/jaoafa/javajaotan2/lib/JavajaotanCommand.java index 188b2ceb..fd5671ff 100644 --- a/src/main/java/com/jaoafa/javajaotan2/lib/JavajaotanCommand.java +++ b/src/main/java/com/jaoafa/javajaotan2/lib/JavajaotanCommand.java @@ -23,7 +23,7 @@ public class JavajaotanCommand { public static String permRoles(List roles) { - if(roles == null) return null; + if (roles == null) return null; return roles.stream() .map(Roles::getRoleId) .map(Object::toString).collect(Collectors.joining("|")); @@ -101,7 +101,7 @@ public List getAllowRoles() { } public static class Cmd { - Command[] commands; + final Command[] commands; @SafeVarargs public Cmd(@NonNull Command... commands) { diff --git a/src/main/java/com/jaoafa/javajaotan2/lib/JavajaotanLibrary.java b/src/main/java/com/jaoafa/javajaotan2/lib/JavajaotanLibrary.java index aa7b857b..c4fe5034 100644 --- a/src/main/java/com/jaoafa/javajaotan2/lib/JavajaotanLibrary.java +++ b/src/main/java/com/jaoafa/javajaotan2/lib/JavajaotanLibrary.java @@ -13,6 +13,7 @@ import com.jaoafa.javajaotan2.Main; import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.entities.emoji.CustomEmoji; import okhttp3.*; import org.json.JSONArray; import org.json.JSONObject; @@ -102,7 +103,7 @@ public static boolean isGrantedRole(Member member, Role role) { public static String getContentDisplay(Message message, String raw) { String tmp = raw; - for (User user : message.getMentionedUsers()) { + for (User user : message.getMentions().getUsers()) { String name; if (message.isFromGuild() && message.getGuild().isMember(user)) { Member member = message.getGuild().getMember(user); @@ -113,13 +114,13 @@ public static String getContentDisplay(Message message, String raw) { } tmp = tmp.replaceAll("<@!?" + Pattern.quote(user.getId()) + '>', '@' + Matcher.quoteReplacement(name)); } - for (Emote emote : message.getEmotes()) { - tmp = tmp.replace(emote.getAsMention(), ":" + emote.getName() + ":"); + for (CustomEmoji emoji : message.getMentions().getCustomEmojis()) { + tmp = tmp.replace(emoji.getAsMention(), ":" + emoji.getName() + ":"); } - for (TextChannel mentionedChannel : message.getMentionedChannels()) { + for (GuildChannel mentionedChannel : message.getMentions().getChannels()) { tmp = tmp.replace(mentionedChannel.getAsMention(), '#' + mentionedChannel.getName()); } - for (Role mentionedRole : message.getMentionedRoles()) { + for (Role mentionedRole : message.getMentions().getRoles()) { tmp = tmp.replace(mentionedRole.getAsMention(), '@' + mentionedRole.getName()); } return tmp; diff --git a/src/main/java/com/jaoafa/javajaotan2/lib/SubAccount.java b/src/main/java/com/jaoafa/javajaotan2/lib/SubAccount.java index fa5af9a0..9d6e851f 100644 --- a/src/main/java/com/jaoafa/javajaotan2/lib/SubAccount.java +++ b/src/main/java/com/jaoafa/javajaotan2/lib/SubAccount.java @@ -1,7 +1,7 @@ /* * jaoLicense * - * Copyright (c) 2021 jao Minecraft Server + * Copyright (c) 2022 jao Minecraft Server * * The following license applies to this project: jaoLicense * @@ -12,10 +12,7 @@ package com.jaoafa.javajaotan2.lib; import com.jaoafa.javajaotan2.Main; -import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.Role; -import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.exceptions.ErrorResponseException; import org.jetbrains.annotations.NotNull; @@ -85,7 +82,7 @@ public boolean setMainAccount(@NotNull SubAccount mainAccount) { } catch (SQLException e) { return false; } - jMSGuild.addRoleToMember(user.getId(), SubAccountRole).queue(); + jMSGuild.addRoleToMember(UserSnowflake.fromId(user.getId()), SubAccountRole).queue(); return true; } @@ -104,7 +101,7 @@ public boolean removeMainAccount() { } catch (SQLException e) { return false; } - jMSGuild.removeRoleFromMember(user.getId(), SubAccountRole).queue(); + jMSGuild.removeRoleFromMember(UserSnowflake.fromId(user.getId()), SubAccountRole).queue(); return true; } diff --git a/src/main/java/com/jaoafa/javajaotan2/lib/Translate.java b/src/main/java/com/jaoafa/javajaotan2/lib/Translate.java index 77b27313..b7cd8775 100644 --- a/src/main/java/com/jaoafa/javajaotan2/lib/Translate.java +++ b/src/main/java/com/jaoafa/javajaotan2/lib/Translate.java @@ -1,7 +1,7 @@ /* * jaoLicense * - * Copyright (c) 2021 jao Minecraft Server + * Copyright (c) 2022 jao Minecraft Server * * The following license applies to this project: jaoLicense * @@ -42,12 +42,14 @@ public static TranslateResult translate(Language before, Language after, String MediaType.parse("application/json; charset=utf-8") ); Request request = new Request.Builder().url(url).post(requestBody).build(); - Response response = client.newCall(request).execute(); - ResponseBody body = response.body(); - if (body == null) { - return null; + JSONObject object; + try (Response response = client.newCall(request).execute()) { + ResponseBody body = response.body(); + if (body == null) { + return null; + } + object = new JSONObject(body.string()); } - JSONObject object = new JSONObject(body.string()); return new TranslateResult( object.getJSONObject("response").getString("result"), before, @@ -211,7 +213,7 @@ public enum Language { ZU("ズールー語"), UNKNOWN(null); - String language_name; + final String language_name; Language(String language_name) { this.language_name = language_name; diff --git a/src/main/java/com/jaoafa/javajaotan2/lib/WatchEmojis.java b/src/main/java/com/jaoafa/javajaotan2/lib/WatchEmojis.java index b6504009..d449097a 100644 --- a/src/main/java/com/jaoafa/javajaotan2/lib/WatchEmojis.java +++ b/src/main/java/com/jaoafa/javajaotan2/lib/WatchEmojis.java @@ -1,7 +1,7 @@ /* * jaoLicense * - * Copyright (c) 2021 jao Minecraft Server + * Copyright (c) 2022 jao Minecraft Server * * The following license applies to this project: jaoLicense * @@ -13,7 +13,11 @@ import com.jaoafa.javajaotan2.Main; import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.MessageChannel; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji; import net.dv8tion.jda.api.exceptions.ErrorResponseException; import org.jetbrains.annotations.NotNull; import org.json.JSONArray; @@ -156,9 +160,9 @@ private Message getMessage(TextChannel channel, long message_id) { private List getEmojiList(Guild guild) { return guild - .getEmotes() + .getEmojis() .stream() - .sorted(Comparator.comparing(Emote::getName)) + .sorted(Comparator.comparing(RichCustomEmoji::getName)) .map(e -> String.format("%s = `:%s:`", e.getAsMention(), e.getName())) .collect(Collectors.toList()); } diff --git a/src/main/java/com/jaoafa/javajaotan2/tasks/Task_SyncOtherServerPerm.java b/src/main/java/com/jaoafa/javajaotan2/tasks/Task_SyncOtherServerPerm.java index 06faab21..0f009757 100644 --- a/src/main/java/com/jaoafa/javajaotan2/tasks/Task_SyncOtherServerPerm.java +++ b/src/main/java/com/jaoafa/javajaotan2/tasks/Task_SyncOtherServerPerm.java @@ -1,7 +1,7 @@ /* * jaoLicense * - * Copyright (c) 2021 jao Minecraft Server + * Copyright (c) 2022 jao Minecraft Server * * The following license applies to this project: jaoLicense * @@ -183,14 +183,14 @@ private List getGuildMember(String guildId) { } static class GuildMember { - List roles; - String nick; - ZonedDateTime joined_at; - String user_id; - String user_name; - String user_avatar; - String user_discriminator; - int user_public_flags; + final List roles; + final String nick; + final ZonedDateTime joined_at; + final String user_id; + final String user_name; + final String user_avatar; + final String user_discriminator; + final int user_public_flags; private GuildMember(JSONObject object) { this.roles = object.getJSONArray("roles")