From 8fc7a7aac2621de1d78c29b8bdd29ee909be32a9 Mon Sep 17 00:00:00 2001 From: MORIMORI0317 Date: Thu, 18 Aug 2022 23:39:23 +0900 Subject: [PATCH] 1.30 --- src/main/java/dev/felnull/ttsvoice/Main.java | 1 + .../felnull/ttsvoice/data/ServerSaveData.java | 15 +++++++++++++++ .../dev/felnull/ttsvoice/tts/TTSListener.java | 17 +++++++++++++---- .../dev/felnull/ttsvoice/util/DiscordUtils.java | 7 ++++++- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/main/java/dev/felnull/ttsvoice/Main.java b/src/main/java/dev/felnull/ttsvoice/Main.java index e2e7709..bc06480 100644 --- a/src/main/java/dev/felnull/ttsvoice/Main.java +++ b/src/main/java/dev/felnull/ttsvoice/Main.java @@ -75,6 +75,7 @@ public static void main(String[] args) throws Exception { .addSubcommands(new SubcommandData("join-say-name", "VCに参加時に名前を読み上げ").addOptions(new OptionData(OptionType.BOOLEAN, "enable", "有効かどうか").setRequired(true))) .addSubcommands(new SubcommandData("read-around-limit", "最大読み上げ文字数").addOptions(new OptionData(OptionType.INTEGER, "max-count", "最大文字数").setMinValue(1).setMaxValue(Integer.MAX_VALUE).setRequired(true))) .addSubcommands(new SubcommandData("non-reading-prefix", "先頭につけると読み上げなくなる文字").addOptions(new OptionData(OptionType.STRING, "prefix", "接頭辞").setRequired(true))) + .addSubcommands(new SubcommandData("read-around-name-limit", "最大名前読み上げ文字数").addOptions(new OptionData(OptionType.INTEGER, "max-count", "最大文字数").setMinValue(1).setMaxValue(Integer.MAX_VALUE).setRequired(true))) .addSubcommands(new SubcommandData("show", "現在のコンフィグを表示")).setGuildOnly(true); var vnick = Commands.slash("vnick", "読み上げユーザ名変更").addOptions(new OptionData(OptionType.STRING, "name", "名前").setRequired(true)).addOptions(new OptionData(OptionType.USER, "user", "ユーザー指定")).setGuildOnly(true); diff --git a/src/main/java/dev/felnull/ttsvoice/data/ServerSaveData.java b/src/main/java/dev/felnull/ttsvoice/data/ServerSaveData.java index edd054e..11634bf 100644 --- a/src/main/java/dev/felnull/ttsvoice/data/ServerSaveData.java +++ b/src/main/java/dev/felnull/ttsvoice/data/ServerSaveData.java @@ -18,6 +18,7 @@ public class ServerSaveData extends BaseSaveData { private int maxReadAroundCharacterLimit = 200; private String nonReadingPrefix = ";"; private final Map lastJoinChannels = new HashMap<>(); + private int maxReadAroundNameLimit = 10; private boolean dirty; public ServerSaveData(long guildId) { @@ -53,6 +54,9 @@ public void load(JsonObject jo) { if (nrp != null) nonReadingPrefix = nrp; + var mranl = JsonUtils.getInteger(jo, "max_read_around_name_limit"); + if (mranl != null) + maxReadAroundNameLimit = mranl; if (jo.has("last_join") && jo.get("last_join").isJsonObject()) { var joe = jo.getAsJsonObject("last_join"); @@ -70,6 +74,7 @@ public void save(JsonObject jo) { jo.addProperty("cookie_mode", cookieMode); jo.addProperty("join_say_name", joinSayName); jo.addProperty("max_read_around_character_limit", maxReadAroundCharacterLimit); + jo.addProperty("max_read_around_name_limit", maxReadAroundNameLimit); jo.addProperty("non-reading_prefix", nonReadingPrefix); var ljjo = new JsonObject(); @@ -107,6 +112,10 @@ public int getMaxReadAroundCharacterLimit() { return maxReadAroundCharacterLimit; } + public int getMaxReadAroundNameLimit() { + return maxReadAroundNameLimit; + } + public String getNonReadingPrefix() { return nonReadingPrefix; } @@ -161,6 +170,12 @@ public void setNonReadingPrefix(String NonReadingPrefix) { saved(); } + public void setMaxReadAroundNameLimit(int maxReadAroundNameLimit) { + this.maxReadAroundNameLimit = maxReadAroundNameLimit; + dirty = true; + saved(); + } + public void setLastJoinChannel(long botUserId, TTSEntry ttsEntry) { lastJoinChannels.put(botUserId, ttsEntry); dirty = true; diff --git a/src/main/java/dev/felnull/ttsvoice/tts/TTSListener.java b/src/main/java/dev/felnull/ttsvoice/tts/TTSListener.java index 37f4834..a0128ca 100644 --- a/src/main/java/dev/felnull/ttsvoice/tts/TTSListener.java +++ b/src/main/java/dev/felnull/ttsvoice/tts/TTSListener.java @@ -52,7 +52,7 @@ public class TTSListener extends ListenerAdapter { @Override public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent e) { - if (!e.isFromGuild()) return; + if (!e.isFromGuild() || e.getMember() == null) return; switch (e.getName()) { case "join" -> { @@ -274,6 +274,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent e) { sbr.append("VCに参加時に名前を読み上げ").append(" ").append(sc.isJoinSayName() ? "有効" : "無効").append("\n"); sbr.append("最大読み上げ文字数").append(" ").append(sc.getMaxReadAroundCharacterLimit()).append("文字").append("\n"); + sbr.append("最大名前読み上げ文字数").append(" ").append(sc.getMaxReadAroundNameLimit()).append("文字").append("\n"); sbr.append("先頭につけると読み上げなくなる文字").append(" \"").append(sc.getNonReadingPrefix()).append("\"").append("\n"); msg.appendCodeLine(sbr.toString()); @@ -363,6 +364,14 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent e) { sc.setMaxReadAroundCharacterLimit(iv); e.reply("最大読み上げ文字数を" + iv + "にしました").queue(); } + case "read-around-name-limit" -> { + if (sc.getMaxReadAroundNameLimit() == iv) { + e.reply("すでに最大名前読み上げ文字数は" + iv + "です").setEphemeral(true).queue(); + return; + } + sc.setMaxReadAroundNameLimit(iv); + e.reply("最大名前読み上げ文字数を" + iv + "にしました").queue(); + } } } else if (en.getType() == OptionType.STRING) { String pre = en.getAsString(); @@ -409,7 +418,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent e) { @Override public void onCommandAutoCompleteInteraction(@NotNull CommandAutoCompleteInteractionEvent e) { - if (!e.isFromGuild()) return; + if (!e.isFromGuild() || e.getMember() == null) return; if ("voice".equals(e.getName()) && "change".equals(e.getSubcommandName())) { var opc = e.getInteraction().getOption("voice_category"); @@ -467,7 +476,7 @@ public void onCommandAutoCompleteInteraction(@NotNull CommandAutoCompleteInterac @Override public void onMessageReceived(@NotNull MessageReceivedEvent e) { - if (!e.isFromGuild()) return; + if (!e.isFromGuild() || e.getMember() == null) return; var sc = Main.getServerSaveData(e.getGuild().getIdLong()); var tm = TTSManager.getInstance(); @@ -668,7 +677,7 @@ public void onMessageDelete(@NotNull MessageDeleteEvent event) { @Override public void onMessageUpdate(@NotNull MessageUpdateEvent event) { - if (!event.isFromGuild()) return; + if (!event.isFromGuild() || event.getMember() == null) return; var botLocation = BotLocation.of(event); var mtkey = new TTSManager.TextMessageTTSTrackerKey(botLocation, event.getChannel().getIdLong(), event.getMessageIdLong()); diff --git a/src/main/java/dev/felnull/ttsvoice/util/DiscordUtils.java b/src/main/java/dev/felnull/ttsvoice/util/DiscordUtils.java index 12a30f5..1f359da 100644 --- a/src/main/java/dev/felnull/ttsvoice/util/DiscordUtils.java +++ b/src/main/java/dev/felnull/ttsvoice/util/DiscordUtils.java @@ -36,7 +36,12 @@ public static String toNoMention(String txt) { } public static String getName(BotLocation botLocation, User user, long userId) { - return toNoMention(getName_(botLocation, user, userId)); + var name = toNoMention(getName_(botLocation, user, userId)); + int maxr = Main.getServerSaveData(botLocation.guildId()).getMaxReadAroundNameLimit(); + if (name.length() > maxr) { + name = name.substring(0, maxr) + "以下略"; + } + return name; } private static String getName_(BotLocation botLocation, User user, long userId) {