From 413f73403465460d8e6e4c38e1a74070874108f4 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 2 Jan 2022 01:04:13 +0800 Subject: [PATCH 1/8] =?UTF-8?q?ip186=20=E7=B3=BB=E5=88=97=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E6=94=AF=E6=8C=81=E5=9B=9E=E5=A4=8D=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=E5=8F=82=E6=95=B0=EF=BC=8C=E6=B7=BB=E5=8A=A0=20/save=20?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E7=AB=8B=E5=8D=B3=E4=BF=9D=E5=AD=98=EF=BC=8C?= =?UTF-8?q?git=20=E6=B7=BB=E5=8A=A0=20.vscode=20=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - gitignore 添加了 .vscode 字段 - /ip 与 /whois 命令支持了回复某条消息,以其消息内容作为参数的查询方式 - 添加 /save 与其对应的 Morny save 指令链,用于手动/统一触发数据持久化储存事件 - 更新了 hack event 的 debug log 文案 - 稍微修改了 Tracker 的保存函数链,用以支持外部触发 --- .gitignore | 1 + gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +-- .../cc/sukazyo/cono/morny/MornyCoeur.java | 17 ++++++++++ .../morny/bot/event/OnCommandExecute.java | 25 ++++++++++++++ .../morny/bot/event/OnEventHackHandle.java | 2 +- .../bot/event/on_commands/Ip186Query.java | 34 +++++++++++++------ .../cono/morny/data/TelegramStickers.java | 1 + .../data/tracker/TrackerDataManager.java | 10 +++--- 9 files changed, 77 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 037aacb..8520bdf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # IDE .idea/ +.vscode/ .gradle/ .settings/ /src/test/* diff --git a/gradle.properties b/gradle.properties index f697b89..6f25b4a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.2.11 +VERSION = 0.4.3.0 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 4760921..b19b3c0 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.4.2.11"; - public static final long COMPILE_TIMESTAMP = 1640595623685L; + public static final String VERSION = "0.4.3.0"; + public static final long COMPILE_TIMESTAMP = 1641056437585L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index 06d7944..7f8a239 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -122,6 +122,14 @@ public class MornyCoeur { logger.error("System already started coeur!!!"); } + /** + * 向所有的数据管理器发起保存数据的指令 + * @since 0.4.3.0 + */ + public void saveDataAll () { + TrackerDataManager.save(); + } + /** * 用于退出时进行缓存的任务处理等进行安全退出 */ @@ -167,6 +175,15 @@ public class MornyCoeur { throw new RuntimeException("Login failed.."); } + /** + * @see #saveDataAll() + * @since 0.4.3.0 + */ + public static void callSaveData () { + INSTANCE.saveDataAll(); + logger.info("done all save action."); + } + /** * 获取登录成功后的 telegram bot 对象 * diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java index a49d884..5b49947 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java @@ -56,6 +56,9 @@ public class OnCommandExecute extends EventListener { case "/runtime": onCommandRuntimeExec(event); break; + case "/save": + onSaveDataExec(event); + break; case "/jrrp": onCommandJrrpExec(event); break; @@ -200,4 +203,26 @@ public class OnCommandExecute extends EventListener { ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } + /** + * @since 0.4.3.0 + */ + private void onSaveDataExec (Update event) { + if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) { + logger.info(String.format("called save from command by @%s.", event.message().from().username())); + MornyCoeur.callSaveData(); + MornyCoeur.getAccount().execute(new SendSticker( + event.message().chat().id(), + TelegramStickers.ID_SAVED + ).replyToMessageId(event.message().messageId()) + ); + } else { + MornyCoeur.getAccount().execute(new SendSticker( + event.message().chat().id(), + TelegramStickers.ID_403 + ).replyToMessageId(event.message().messageId()) + ); + logger.info("403 call save tag from user @" + event.message().from().username()); + } + } + } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java index a4275a4..3f814ff 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java @@ -57,7 +57,7 @@ public class OnEventHackHandle extends EventListener { } private boolean onEventHacked (Update update, long chatId, long fromUser) { - logger.debug(String.format("try hack {{%d}}((%d))", chatId, fromUser)); + logger.debug(String.format("got event signed {{%d}}((%d))", chatId, fromUser)); Hacker x; x = hackers.remove(String.format("((%d))", fromUser)); if (x == null) x = hackers.remove(String.format("{{%d}}", chatId)); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java index 3c73c8a..9128ed1 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java @@ -20,20 +20,32 @@ public class Ip186Query { public static void exec (@Nonnull Update event, @Nonnull InputCommand command) { - if (!command.hasArgs()) { MornyCoeur.getAccount().execute(new SendMessage( - event.message().chat().id(), - "[Unavailable] No ip defined." - ).replyToMessageId(event.message().messageId())); return; } - - if (command.getArgs().length > 1) { MornyCoeur.getAccount().execute(new SendMessage( - event.message().chat().id(), - "[Unavailable] Too much arguments." - ).replyToMessageId(event.message().messageId())); return; } + String arg = null; + if (!command.hasArgs()) { + if (event.message().replyToMessage() != null) { + arg = event.message().replyToMessage().text(); + } + } else if (command.getArgs().length > 1) { + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Unavailable] Too much arguments." + ).replyToMessageId(event.message().messageId())); + return; + } else { + arg = command.getArgs()[0]; + } + if (arg == null) { + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Unavailable] No ip defined." + ).replyToMessageId(event.message().messageId())); + return; + } try { IP186QueryResponse response = switch (command.getCommand()) { - case "/ip" -> IP186QueryHandler.queryIp(command.getArgs()[0]); - case "/whois" -> IP186QueryHandler.queryWhois(command.getArgs()[0]); + case "/ip" -> IP186QueryHandler.queryIp(arg); + case "/whois" -> IP186QueryHandler.queryWhois(arg); default -> throw new IllegalArgumentException("Unknown 186-IP query method " + command.getCommand()); }; MornyCoeur.getAccount().execute(new SendMessage( diff --git a/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java index d478e05..c53024c 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java @@ -13,5 +13,6 @@ public class TelegramStickers { public static final String ID_404 = "CAACAgEAAx0CSQh32gABA966YbRJpbmi2lCHINBDuo1DknSTsbsAAqUoAAJ4_MYFUa8SIaZriAojBA"; public static final String ID_WAITING = "CAACAgEAAx0CSQh32gABA-8DYbh7W2VhJ490ucfZMUMrgMR2FW4AAm4nAAJ4_MYFjx6zpxJPWsQjBA"; public static final String ID_SENT = "CAACAgEAAx0CSQh32gABA--zYbiyU_wOijEitp-0tSl_k7W6l3gAAgMmAAJ4_MYF4GrompjXPx4jBA"; + public static final String ID_SAVED = "CAACAgEAAx0CSQh32gABBExuYdB_G0srfhQldRWkBYxWzCOv4-IAApooAAJ4_MYFcjuNZszfQcQjBA"; } diff --git a/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java b/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java index 753944d..aed2c8d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java @@ -26,7 +26,6 @@ public class TrackerDataManager { @Override public void run () { - trackingLock.lock(); long lastWaitTimestamp = System.currentTimeMillis(); boolean postProcess = false; do { @@ -43,13 +42,10 @@ public class TrackerDataManager { logger.info("CALLED TO EXIT! writing cache."); } if (record.size() != 0) { - logger.info("start writing tracker data."); save(reset()); - logger.info("done writing tracker data."); } else logger.info("nothing to do yet"); } while (!postProcess); - trackingLock.unlock(); } } @@ -68,6 +64,12 @@ public class TrackerDataManager { DAEMON.start(); } + public static void save () { + logger.info("start writing tracker data."); + save(reset()); + logger.info("done writing tracker data."); + } + private static HashMap>> reset () { recordLock.lock(); HashMap>> recordOld = record; From 2a34b576b6a689a23ffa6ed3389803a8ac7b313a Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 3 Jan 2022 17:24:35 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=B0=86=E9=83=A8=E5=88=86=20util=20?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E8=87=B3=20untitled=20=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E4=B8=8A=E4=B8=AA=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=9A=84=20tracker=20=E6=B4=BB=E5=8A=A8=E9=94=81=E8=A2=AB?= =?UTF-8?q?=E8=AF=AF=E5=88=A0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 4 ++ gradle.properties | 4 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cono/morny/bot/api/InputCommand.java | 65 ----------------- .../cono/morny/bot/event/OnCallMe.java | 8 +-- .../morny/bot/event/OnCommandExecute.java | 33 ++++----- .../morny/bot/event/OnEventHackHandle.java | 5 +- .../morny/bot/event/OnUserSlashAction.java | 20 +++--- .../bot/event/on_commands/EventHack.java | 3 +- .../event/on_commands/GetUsernameAndId.java | 10 +-- .../bot/event/on_commands/Ip186Query.java | 8 +-- .../data/tracker/TrackerDataManager.java | 2 + .../sukazyo/cono/morny/util/StringUtils.java | 69 ------------------- 13 files changed, 57 insertions(+), 178 deletions(-) delete mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/api/InputCommand.java delete mode 100644 src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java diff --git a/build.gradle b/build.gradle index 2b36477..4ec752e 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,10 @@ dependencies { compileOnlyApi "com.github.spotbugs:spotbugs-annotations:${libSpotbugsVersion}" + implementation "cc.sukazyo.untitled:util-string-commons:${libUntitledVersion}" + implementation "cc.sukazyo.untitled:util-telegram-commons:${libUntitledVersion}" + implementation "cc.sukazyo.untitled:util-telegram-api:${libUntitledVersion}" + api "cc.sukazyo:messiva:${libMessivaVersion}" implementation "com.github.pengrad:java-telegram-bot-api:${libJavaTelegramBotApiVersion}" diff --git a/gradle.properties b/gradle.properties index 6f25b4a..bc97dde 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,11 +1,13 @@ ## Core -VERSION = 0.4.3.0 +VERSION = 0.4.3.1 # dependencies libSpotbugsVersion = 4.5.2 +libUntitledVersion = 1.+ + libMessivaVersion = 0.1.0.1 libJavaTelegramBotApiVersion = 5.5.0 diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index b19b3c0..7481938 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.4.3.0"; - public static final long COMPILE_TIMESTAMP = 1641056437585L; + public static final String VERSION = "0.4.3.1"; + public static final long COMPILE_TIMESTAMP = 1641201595120L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/InputCommand.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/InputCommand.java deleted file mode 100644 index 743d0da..0000000 --- a/src/main/java/cc/sukazyo/cono/morny/bot/api/InputCommand.java +++ /dev/null @@ -1,65 +0,0 @@ -package cc.sukazyo.cono.morny.bot.api; - -import cc.sukazyo.cono.morny.util.StringUtils; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Arrays; - -public class InputCommand { - - private final String target; - private final String command; - private final String[] args; - - private InputCommand (@Nullable String target, @Nonnull String command, @Nonnull String[] args) { - this.target = target; - this.command = command; - this.args = args; - } - - public InputCommand (@Nonnull String[] inputArray) { - this(parseInputArray(inputArray)); - } - - public InputCommand (@Nonnull String input) { - this(StringUtils.formatCommand(input)); - } - - public InputCommand (@Nonnull InputCommand source) { - this(source.target, source.command, source.args); - } - - public static InputCommand parseInputArray (@Nonnull String[] inputArray) { - final String[] cx = inputArray[0].split("@", 2); - final String[] args = new String[inputArray.length-1]; - System.arraycopy(inputArray, 1, args, 0, inputArray.length - 1); - return new InputCommand(cx.length == 1 ? null : cx[1], cx[0], args); - } - - @Nullable - public String getTarget () { - return target; - } - - @Nonnull - public String getCommand () { - return command; - } - - @Nonnull - public String[] getArgs () { - return args; - } - - public boolean hasArgs () { - return args.length != 0; - } - - @Override - @Nonnull - public String toString() { - return String.format("{{%s}@{%s}#{%s}}", command, target, Arrays.toString(args)); - } - -} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java index f105b9e..10a814b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java @@ -13,7 +13,7 @@ import com.pengrad.telegrambot.request.SendSticker; import javax.annotation.Nonnull; -import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; /** * 通过 bot 呼叫主人的事件监听管理类 @@ -76,7 +76,7 @@ public class OnCallMe extends EventListener { request STEAM LIBRARY from %s""", event.message().from().id(), - escapeHtmlTelegram( + escapeHtml( event.message().from().firstName() + " " + event.message().from().lastName() ) ) @@ -96,7 +96,7 @@ public class OnCallMe extends EventListener { request Hana Paresu from %s""", event.message().from().id(), - escapeHtmlTelegram( + escapeHtml( event.message().from().firstName() + " " + event.message().from().lastName() ) ) @@ -124,7 +124,7 @@ public class OnCallMe extends EventListener { request [???] from %s""", event.message().from().id(), - escapeHtmlTelegram( + escapeHtml( event.message().from().firstName() + " " + event.message().from().lastName() ) ) diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java index 5b49947..1b16641 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java @@ -4,12 +4,13 @@ import cc.sukazyo.cono.morny.GradleProjectConfigures; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornySystem; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.cono.morny.bot.api.InputCommand; import cc.sukazyo.cono.morny.bot.event.on_commands.EventHack; import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId; import cc.sukazyo.cono.morny.bot.event.on_commands.Ip186Query; import cc.sukazyo.cono.morny.data.MornyJrrp; import cc.sukazyo.cono.morny.data.TelegramStickers; +import cc.sukazyo.untitled.util.telegram.object.InputCommand; + import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; @@ -20,7 +21,7 @@ import javax.annotation.Nonnull; import static cc.sukazyo.cono.morny.Log.logger; import static cc.sukazyo.cono.morny.util.CommonFormatUtils.formatDate; import static cc.sukazyo.cono.morny.util.CommonFormatUtils.formatDuration; -import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; public class OnCommandExecute extends EventListener { @@ -131,10 +132,10 @@ public class OnCommandExecute extends EventListener { compile timestamp: - %d - %s [UTC]""", - escapeHtmlTelegram(MornySystem.VERSION), - escapeHtmlTelegram(MornySystem.getJarMd5()), + escapeHtml(MornySystem.VERSION), + escapeHtml(MornySystem.getJarMd5()), GradleProjectConfigures.COMPILE_TIMESTAMP, - escapeHtmlTelegram(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)) + escapeHtml(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)) ) ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } @@ -166,24 +167,24 @@ public class OnCommandExecute extends EventListener { - %s [UTC] - [%d]""", // system - escapeHtmlTelegram(System.getProperty("os.name")), - escapeHtmlTelegram(System.getProperty("os.version")), + escapeHtml(System.getProperty("os.name")), + escapeHtml(System.getProperty("os.version")), Runtime.getRuntime().availableProcessors(), // java - escapeHtmlTelegram(System.getProperty("java.vm.name")), - escapeHtmlTelegram(System.getProperty("java.version")), + escapeHtml(System.getProperty("java.vm.name")), + escapeHtml(System.getProperty("java.version")), // memory Runtime.getRuntime().totalMemory() / 1024 / 1024, Runtime.getRuntime().maxMemory() / 1024 / 1024, // version - escapeHtmlTelegram(MornySystem.VERSION), - escapeHtmlTelegram(MornySystem.getJarMd5()), - escapeHtmlTelegram(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)), + escapeHtml(MornySystem.VERSION), + escapeHtml(MornySystem.getJarMd5()), + escapeHtml(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)), GradleProjectConfigures.COMPILE_TIMESTAMP, // continuous - escapeHtmlTelegram(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)), + escapeHtml(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)), System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp, - escapeHtmlTelegram(formatDate(MornyCoeur.coeurStartTimestamp, 0)), + escapeHtml(formatDate(MornyCoeur.coeurStartTimestamp, 0)), MornyCoeur.coeurStartTimestamp ) ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); @@ -197,8 +198,8 @@ public class OnCommandExecute extends EventListener { String.format( "%s 在(utc的)今天的运气指数是———— %.2f%% %s", event.message().from().id(), - escapeHtmlTelegram(event.message().from().firstName()), - jrrp, escapeHtmlTelegram(endChar) + escapeHtml(event.message().from().firstName()), + jrrp, escapeHtml(endChar) ) ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java index 3f814ff..d61391e 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java @@ -2,7 +2,8 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.cono.morny.util.StringUtils; +import cc.sukazyo.untitled.util.telegram.formatting.MsgEscape; + import com.google.gson.GsonBuilder; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; @@ -66,7 +67,7 @@ public class OnEventHackHandle extends EventListener { logger.debug("hacked event by " + x); MornyCoeur.getAccount().execute(new SendMessage(x.fromChatId, String.format( "%s", - StringUtils.escapeHtmlTelegram(new GsonBuilder().setPrettyPrinting().create().toJson(update)) + MsgEscape.escapeHtml(new GsonBuilder().setPrettyPrinting().create().toJson(update)) )).parseMode(ParseMode.HTML).replyToMessageId((int)x.fromMessageId)); return true; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java index 67dff8a..807d984 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java @@ -2,7 +2,9 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.cono.morny.util.StringUtils; +import cc.sukazyo.untitled.util.command.CommonCommand; +import cc.sukazyo.untitled.util.string.StringArrays; + import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.model.request.ParseMode; @@ -10,7 +12,7 @@ import com.pengrad.telegrambot.request.SendMessage; import javax.annotation.Nonnull; -import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; public class OnUserSlashAction extends EventListener { @@ -35,10 +37,10 @@ public class OnUserSlashAction extends EventListener { prefixLength = 2; } - final String[] action = StringUtils.formatCommand(text.substring(prefixLength)); + final String[] action = CommonCommand.format(text.substring(prefixLength)); final String verb = action[0]; final boolean hasObject = action.length != 1; - final String object = StringUtils.connectStringArray(action, " ", 1, action.length-1); + final String object = StringArrays.connectStringArray(action, " ", 1, action.length-1); final User origin = event.message().from(); final User target = (event.message().replyToMessage() == null ? ( origin @@ -50,11 +52,11 @@ public class OnUserSlashAction extends EventListener { event.message().chat().id(), String.format( "%s %s%s %s%s%s", - origin.id(), escapeHtmlTelegram(origin.firstName()), - verb, escapeHtmlTelegram((useVerbSuffix?"了":"")), - target.id(), escapeHtmlTelegram((origin==target ? "自己" : target.firstName())), - escapeHtmlTelegram((hasObject ? (useObjectPrefix ?" 的": " ") : "")), - escapeHtmlTelegram((hasObject ? object : "")) + origin.id(), escapeHtml(origin.firstName()), + verb, escapeHtml((useVerbSuffix?"了":"")), + target.id(), escapeHtml((origin==target ? "自己" : target.firstName())), + escapeHtml((hasObject ? (useObjectPrefix ?" 的": " ") : "")), + escapeHtml((hasObject ? object : "")) ) ).parseMode(ParseMode.HTML)); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java index 2146535..2638ec6 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java @@ -2,9 +2,10 @@ package cc.sukazyo.cono.morny.bot.event.on_commands; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyTrusted; -import cc.sukazyo.cono.morny.bot.api.InputCommand; import cc.sukazyo.cono.morny.bot.event.OnEventHackHandle; import cc.sukazyo.cono.morny.data.TelegramStickers; +import cc.sukazyo.untitled.util.telegram.object.InputCommand; + import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.request.SendSticker; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java index b5cd025..0bd7830 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java @@ -10,7 +10,7 @@ import com.pengrad.telegrambot.response.GetChatMemberResponse; import javax.annotation.Nonnull; -import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; public class GetUsernameAndId { @@ -67,7 +67,7 @@ public class GetUsernameAndId { username : - %s""", - escapeHtmlTelegram(user.username()) + escapeHtml(user.username()) )); } if (user.firstName() == null) { @@ -78,7 +78,7 @@ public class GetUsernameAndId { firstname : - %s""", - escapeHtmlTelegram(user.firstName()) + escapeHtml(user.firstName()) )); } if (user.lastName() == null) { @@ -89,7 +89,7 @@ public class GetUsernameAndId { lastname : - %s""", - escapeHtmlTelegram(user.lastName()) + escapeHtml(user.lastName()) )); } if (user.languageCode() != null) { @@ -98,7 +98,7 @@ public class GetUsernameAndId { language-code : - %s""", - escapeHtmlTelegram(user.languageCode()) + escapeHtml(user.languageCode()) )); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java index 9128ed1..12ccddb 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java @@ -1,8 +1,8 @@ package cc.sukazyo.cono.morny.bot.event.on_commands; import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.cono.morny.bot.api.InputCommand; import cc.sukazyo.cono.morny.data.ip186.IP186QueryResponse; +import cc.sukazyo.untitled.util.telegram.object.InputCommand; import cc.sukazyo.cono.morny.data.ip186.IP186QueryHandler; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; @@ -10,7 +10,7 @@ import com.pengrad.telegrambot.request.SendMessage; import javax.annotation.Nonnull; -import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; /** * {@value IP186QueryHandler#SITE_URL} 查询的 telegram 命令前端 @@ -50,12 +50,12 @@ public class Ip186Query { }; MornyCoeur.getAccount().execute(new SendMessage( event.message().chat().id(), - escapeHtmlTelegram(response.url()) + "\n" + escapeHtmlTelegram(response.body()) + "" + escapeHtml(response.url()) + "\n" + escapeHtml(response.body()) + "" ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); } catch (Exception e) { MornyCoeur.getAccount().execute(new SendMessage( event.message().chat().id(), - "[Exception] in query:\n" + escapeHtmlTelegram(e.getMessage()) + "" + "[Exception] in query:\n" + escapeHtml(e.getMessage()) + "" ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); } diff --git a/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java b/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java index aed2c8d..06945ab 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java @@ -26,6 +26,7 @@ public class TrackerDataManager { @Override public void run () { + trackingLock.lock(); long lastWaitTimestamp = System.currentTimeMillis(); boolean postProcess = false; do { @@ -46,6 +47,7 @@ public class TrackerDataManager { } else logger.info("nothing to do yet"); } while (!postProcess); + trackingLock.unlock(); } } diff --git a/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java deleted file mode 100644 index ced5080..0000000 --- a/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java +++ /dev/null @@ -1,69 +0,0 @@ -package cc.sukazyo.cono.morny.util; - -import javax.annotation.Nonnegative; -import javax.annotation.Nonnull; -import java.util.ArrayList; - -public class StringUtils { - - @Nonnull - public static String[] formatCommand (@Nonnull String com) { - - final ArrayList arr = new ArrayList<>(); - - final StringBuilder tmp = new StringBuilder(); - final char[] coma = com.toCharArray(); - for (int i = 0; i < coma.length; i++) { - if (coma[i] == ' ') { - if (!tmp.toString().equals("")) { arr.add(tmp.toString()); } - tmp.setLength(0); - } else if (coma[i] == '"') { - while (true) { - i++; - if (coma[i] == '"') { - break; - } else if (coma[i] == '\\' && (coma[i+1] == '"' || coma[i+1] == '\\')) { - i++; - tmp.append(coma[i]); - } else { - tmp.append(coma[i]); - } - } - } else if (coma[i] == '\\' && (coma[i+1] == ' ' || coma[i+1] == '"' || coma[i+1] == '\\')) { - i++; - tmp.append(coma[i]); - } else { - tmp.append(coma[i]); - } - } - if (!tmp.toString().equals("")) { arr.add(tmp.toString()); } - tmp.setLength(0); - - final String[] out = new String[arr.size()]; - arr.toArray(out); - return out; - - } - - @Nonnull - public static String connectStringArray ( - @Nonnull String[] array, @Nonnull String connector, @Nonnegative int startIndex, @Nonnegative int stopIndex - ) { - final StringBuilder builder = new StringBuilder(); - for (int i = startIndex; i < stopIndex; i++) { - builder.append(array[i]); - builder.append(connector); - } - builder.append(array[stopIndex]); - return builder.toString(); - } - - @Nonnull - public static String escapeHtmlTelegram (String raw) { - raw = raw.replaceAll("&", "&"); - raw = raw.replaceAll("<", "<"); - raw = raw.replaceAll(">", ">"); - return raw; - } - -} From 82d557df5184b5e11a9f4d393405e79711725d0a Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 7 Jan 2022 13:07:50 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20/nbnhhsh=20=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../morny/bot/event/OnCommandExecute.java | 4 ++ .../morny/bot/event/on_commands/Nbnhhsh.java | 52 ++++++++++++++++++ .../sukazyo/cono/morny/data/NbnhhshQuery.java | 53 +++++++++++++++++++ 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java diff --git a/gradle.properties b/gradle.properties index bc97dde..e37ff62 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.3.1 +VERSION = 0.4.3.2 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 7481938..9cf6af4 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.4.3.1"; - public static final long COMPILE_TIMESTAMP = 1641201595120L; + public static final String VERSION = "0.4.3.2"; + public static final long COMPILE_TIMESTAMP = 1641531965975L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java index 1b16641..38b4ffd 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java @@ -7,6 +7,7 @@ import cc.sukazyo.cono.morny.bot.api.EventListener; import cc.sukazyo.cono.morny.bot.event.on_commands.EventHack; import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId; import cc.sukazyo.cono.morny.bot.event.on_commands.Ip186Query; +import cc.sukazyo.cono.morny.bot.event.on_commands.Nbnhhsh; import cc.sukazyo.cono.morny.data.MornyJrrp; import cc.sukazyo.cono.morny.data.TelegramStickers; import cc.sukazyo.untitled.util.telegram.object.InputCommand; @@ -67,6 +68,9 @@ public class OnCommandExecute extends EventListener { case "/whois": Ip186Query.exec(event, command); break; + case "/nbnhhsh": + Nbnhhsh.exec(event, command); + break; default: return nonCommandExecutable(event, command); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java new file mode 100644 index 0000000..af6602f --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java @@ -0,0 +1,52 @@ +package cc.sukazyo.cono.morny.bot.event.on_commands; + +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.SendMessage; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.data.NbnhhshQuery; +import cc.sukazyo.untitled.util.string.StringArrays; +import cc.sukazyo.untitled.util.telegram.object.InputCommand; + +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; + +public class Nbnhhsh { + + public static void exec (Update event, InputCommand command) { + + try { + + String queryTarget = ""; + if (event.message().replyToMessage() != null && event.message().replyToMessage().text() != null) + queryTarget = event.message().replyToMessage().text(); + if (command.hasArgs()) + queryTarget = StringArrays.connectStringArray(command.getArgs(), " ", 0, command.getArgs().length-1); + + NbnhhshQuery.GuessResult response = NbnhhshQuery.sendGuess(queryTarget); + + StringBuilder message = new StringBuilder("## Result of nbnhhsh query :"); + + for (NbnhhshQuery.Word word : response.words) { + if (word.trans == null) continue; + message.append("\n\n[[ ").append(escapeHtml(word.name)).append(" ]]"); + for (String trans : word.trans) { + message.append("\n* ").append(escapeHtml(trans)).append(""); + } + } + + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + message.toString() + ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); + + } catch (Exception e) { + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Exception] in query:\n" + escapeHtml(e.getMessage()) + "" + ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); + } + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java b/src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java new file mode 100644 index 0000000..27c5932 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java @@ -0,0 +1,53 @@ +package cc.sukazyo.cono.morny.data; + +import java.io.IOException; + +import com.google.gson.Gson; + +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.ResponseBody; + +public class NbnhhshQuery { + + public static class Word { + public String name; + public String[] trans; + } + + public static class GuessResult { + public Word[] words; + } + + public static record GuessReq (String text) { + } + + public static final String API_URL = "https://lab.magiconch.com/api/nbnhhsh/"; + public static final String API_GUESS_METHOD = "guess/"; + public static final String API_GUESS_DATA_TEMPLATE = "{ \"text\": \"%s\" }"; + + private static final OkHttpClient httpClient = new OkHttpClient(); + public static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); + + public static GuessResult sendGuess (String text) throws IOException { + final String reqJsonText = new Gson().toJson(new GuessReq(text)); + Request request = new Request.Builder() + .url(API_URL + API_GUESS_METHOD) + .post(RequestBody.create(JSON, reqJsonText)) + .build(); + try (Response response = httpClient.newCall(request).execute()) { + final ResponseBody body = response.body(); + if (body == null) throw new IOException("Null body."); + final String x = "{ \"words\": " + body.string() + " }"; + return new Gson().fromJson(x, GuessResult.class); + } + } + + public static void main(String[] args) { + System.out.println(new Gson().toJson(new GuessReq("8h28oey8 qe89 aoHO*)I'[ IK\"@+ )EOI)D\"{AIR\")Q @}"))); + } + +} From 19b7c65559e91914cdc21791d3671063a64abed6 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 10 Jan 2022 02:24:08 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E4=B8=BA=E4=BA=8B=E4=BB=B6=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF=E6=8A=9B=E5=87=BA?= =?UTF-8?q?=E6=94=AF=E6=8C=81=EF=BC=8C=E5=AE=8C=E6=88=90=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E7=9A=84api=E8=AF=B7=E6=B1=82=E9=94=99=E8=AF=AF=E6=8A=9B?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +-- .../morny/bot/api/EventListenerManager.java | 22 ++++++++++++ .../morny/bot/api/EventRuntimeException.java | 35 +++++++++++++++++++ .../sukazyo/cono/morny/bot/api/Executor.java | 32 +++++++++++++++++ .../cono/morny/bot/event/OnCallMe.java | 11 +++--- .../morny/bot/event/OnCommandExecute.java | 23 ++++++------ .../morny/bot/event/OnEventHackHandle.java | 3 +- .../cono/morny/bot/event/OnInlineQuery.java | 3 +- .../morny/bot/event/OnUserSlashAction.java | 3 +- .../bot/event/on_commands/EventHack.java | 5 +-- .../event/on_commands/GetUsernameAndId.java | 12 ++++--- .../bot/event/on_commands/Ip186Query.java | 9 ++--- .../morny/bot/event/on_commands/Nbnhhsh.java | 5 +-- 14 files changed, 134 insertions(+), 35 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/api/EventRuntimeException.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/api/Executor.java diff --git a/gradle.properties b/gradle.properties index e37ff62..284e63c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.3.2 +VERSION = 0.4.3.3 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 9cf6af4..033315e 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.4.3.2"; - public static final long COMPILE_TIMESTAMP = 1641531965975L; + public static final String VERSION = "0.4.3.3"; + public static final long COMPILE_TIMESTAMP = 1641752627626L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java index 343de29..5291509 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java @@ -1,5 +1,6 @@ package cc.sukazyo.cono.morny.bot.api; +import com.google.gson.GsonBuilder; import com.pengrad.telegrambot.model.Update; import javax.annotation.Nonnull; @@ -27,10 +28,31 @@ public class EventListenerManager { public void run () { for (EventListener x : listeners) { try { + if (exec.apply(x)) return; + + } catch (EventRuntimeException e) { + + final StringBuilder errorMessage = new StringBuilder(); + errorMessage.append("Event runtime breaks: " + e.getMessage()).append('\n'); + errorMessage.append("at " + e.getStackTrace()[0].toString()).append('\n'); + errorMessage.append("at " + e.getStackTrace()[1].toString()).append('\n'); + errorMessage.append("at " + e.getStackTrace()[2].toString()).append('\n'); + errorMessage.append("at " + e.getStackTrace()[3].toString()).append('\n'); + if (e instanceof EventRuntimeException.ActionFailed) { + errorMessage.append(( + "\"telegram request track\": " + + new GsonBuilder().setPrettyPrinting().create().toJson(((EventRuntimeException.ActionFailed)e).getResponse()) + ).indent(4)).append('\n'); + } + + logger.error(errorMessage.toString()); + } catch (Exception e) { + logger.error("Event Error!"); e.printStackTrace(System.out); + } } } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/EventRuntimeException.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventRuntimeException.java new file mode 100644 index 0000000..18b9366 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventRuntimeException.java @@ -0,0 +1,35 @@ +package cc.sukazyo.cono.morny.bot.api; + +import com.pengrad.telegrambot.response.BaseResponse; + +public class EventRuntimeException extends RuntimeException { + + public EventRuntimeException () { + super(); + } + + public EventRuntimeException (String message) { + super(message); + } + + public static class ActionFailed extends EventRuntimeException { + + private final BaseResponse response; + + public ActionFailed (BaseResponse response) { + super(); + this.response = response; + } + + public ActionFailed (String message, BaseResponse response) { + super(message); + this.response = response; + } + + public BaseResponse getResponse() { + return response; + } + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/Executor.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/Executor.java new file mode 100644 index 0000000..4d89e3e --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/api/Executor.java @@ -0,0 +1,32 @@ +package cc.sukazyo.cono.morny.bot.api; + +import com.pengrad.telegrambot.TelegramBot; +import com.pengrad.telegrambot.request.BaseRequest; +import com.pengrad.telegrambot.response.BaseResponse; + +public class Executor { + + private final TelegramBot client; + + public Executor (TelegramBot bot) { + client = bot; + } + + public static Executor as (TelegramBot bot) { + return new Executor(bot); + } + + public , R extends BaseResponse> R exec (T req) { + return exec(req, ""); + } + + public , R extends BaseResponse> R exec (T req, String errorMessage) { + final R resp = client.execute(req); + if (!resp.isOk()) throw new EventRuntimeException.ActionFailed( + (errorMessage.equals("") ? String.valueOf(resp.errorCode()) : errorMessage), + resp + ); + return resp; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java index 10a814b..94d3138 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java @@ -3,6 +3,7 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyTrusted; import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.data.TelegramStickers; import com.pengrad.telegrambot.model.Chat; import com.pengrad.telegrambot.model.Update; @@ -55,7 +56,7 @@ public class OnCallMe extends EventListener { return false; } } - MornyCoeur.getAccount().execute(new SendSticker( + Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( update.message().chat().id(), TelegramStickers.ID_SENT ).replyToMessageId(update.message().messageId()) @@ -70,7 +71,7 @@ public class OnCallMe extends EventListener { * @param event 执行呼叫的tg事件 */ private static void requestSteamJoin (Update event) { - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( ME, String.format( """ request STEAM LIBRARY @@ -90,7 +91,7 @@ public class OnCallMe extends EventListener { * @param event 执行呼叫的tg事件 */ private static void requestHanaParesuJoin (Update event) { - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( ME, String.format( """ request Hana Paresu @@ -118,7 +119,7 @@ public class OnCallMe extends EventListener { * @since 0.4.2.2 */ private static void requestCustomCall (Update event) { - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( ME, String.format( """ request [???] @@ -129,7 +130,7 @@ public class OnCallMe extends EventListener { ) ) ).parseMode(ParseMode.HTML)); - MornyCoeur.getAccount().execute(new ForwardMessage( + Executor.as(MornyCoeur.getAccount()).exec(new ForwardMessage( ME, event.message().chat().id(), event.message().messageId() diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java index 38b4ffd..1f6566d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java @@ -4,6 +4,7 @@ import cc.sukazyo.cono.morny.GradleProjectConfigures; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornySystem; import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.bot.event.on_commands.EventHack; import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId; import cc.sukazyo.cono.morny.bot.event.on_commands.Ip186Query; @@ -80,7 +81,7 @@ public class OnCommandExecute extends EventListener { private boolean nonCommandExecutable (Update event, InputCommand command) { if (command.getTarget() == null) return false; // 无法解析的命令,转交事件链后代处理 else { // 无法解析的显式命令格式,报错找不到命令 - MornyCoeur.getAccount().execute(new SendSticker( + Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_404 ).replyToMessageId(event.message().messageId()) @@ -90,7 +91,7 @@ public class OnCommandExecute extends EventListener { } private void onCommandOnExec (@Nonnull Update event) { - MornyCoeur.getAccount().execute(new SendSticker( + Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_ONLINE_STATUS_RETURN ).replyToMessageId(event.message().messageId()) @@ -98,7 +99,7 @@ public class OnCommandExecute extends EventListener { } private void onCommandHelloExec (@Nonnull Update event) { - MornyCoeur.getAccount().execute(new SendSticker( + Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_HELLO ).replyToMessageId(event.message().messageId()) @@ -107,7 +108,7 @@ public class OnCommandExecute extends EventListener { private void onCommandExitExec (@Nonnull Update event) { if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) { - MornyCoeur.getAccount().execute(new SendSticker( + Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_EXIT ).replyToMessageId(event.message().messageId()) @@ -115,7 +116,7 @@ public class OnCommandExecute extends EventListener { logger.info("Morny exited by user @" + event.message().from().username()); System.exit(0); } else { - MornyCoeur.getAccount().execute(new SendSticker( + Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_403 ).replyToMessageId(event.message().messageId()) @@ -125,7 +126,7 @@ public class OnCommandExecute extends EventListener { } private void onCommandVersionExec (@Nonnull Update event) { - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), String.format( """ @@ -133,7 +134,7 @@ public class OnCommandExecute extends EventListener { - %s core md5_hash: - %s - compile timestamp: + compile time<mp: - %d - %s [UTC]""", escapeHtml(MornySystem.VERSION), @@ -148,7 +149,7 @@ public class OnCommandExecute extends EventListener { * @since 0.4.1.2 */ private void onCommandRuntimeExec (@Nonnull Update event) { - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), String.format(""" system: @@ -197,7 +198,7 @@ public class OnCommandExecute extends EventListener { private void onCommandJrrpExec (Update event) { final double jrrp = MornyJrrp.getJrrpFromTelegramUser(event.message().from(), System.currentTimeMillis()); final String endChar = jrrp>70 ? "!" : jrrp>30 ? ";" : "..."; - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), String.format( "%s 在(utc的)今天的运气指数是———— %.2f%% %s", @@ -215,13 +216,13 @@ public class OnCommandExecute extends EventListener { if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) { logger.info(String.format("called save from command by @%s.", event.message().from().username())); MornyCoeur.callSaveData(); - MornyCoeur.getAccount().execute(new SendSticker( + Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_SAVED ).replyToMessageId(event.message().messageId()) ); } else { - MornyCoeur.getAccount().execute(new SendSticker( + Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_403 ).replyToMessageId(event.message().messageId()) diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java index d61391e..f4d0aa0 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java @@ -2,6 +2,7 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.untitled.util.telegram.formatting.MsgEscape; import com.google.gson.GsonBuilder; @@ -65,7 +66,7 @@ public class OnEventHackHandle extends EventListener { if (x == null) x = hackers.remove("[[]]"); if (x == null) return false; logger.debug("hacked event by " + x); - MornyCoeur.getAccount().execute(new SendMessage(x.fromChatId, String.format( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(x.fromChatId, String.format( "%s", MsgEscape.escapeHtml(new GsonBuilder().setPrettyPrinting().create().toJson(update)) )).parseMode(ParseMode.HTML).replyToMessageId((int)x.fromMessageId)); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java index 1867887..7caaa63 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java @@ -2,6 +2,7 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.util.EncryptUtils; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; @@ -24,7 +25,7 @@ public class OnInlineQuery extends EventListener { */ @Override public boolean onInlineQuery (@Nonnull Update update) { - MornyCoeur.getAccount().execute(new AnswerInlineQuery(update.inlineQuery().id(), new InlineQueryResultArticle[]{ + Executor.as(MornyCoeur.getAccount()).exec(new AnswerInlineQuery(update.inlineQuery().id(), new InlineQueryResultArticle[]{ new InlineQueryResultArticle( EncryptUtils.encryptByMD5(update.inlineQuery().query()), "Raw Input", diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java index 807d984..ce02efa 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java @@ -2,6 +2,7 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.untitled.util.command.CommonCommand; import cc.sukazyo.untitled.util.string.StringArrays; @@ -48,7 +49,7 @@ public class OnUserSlashAction extends EventListener { event.message().replyToMessage().from() )); - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), String.format( "%s %s%s %s%s%s", diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java index 2638ec6..60eea5d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java @@ -2,6 +2,7 @@ package cc.sukazyo.cono.morny.bot.event.on_commands; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyTrusted; +import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.bot.event.OnEventHackHandle; import cc.sukazyo.cono.morny.data.TelegramStickers; import cc.sukazyo.untitled.util.telegram.object.InputCommand; @@ -65,13 +66,13 @@ public class EventHack { } if (isOk) { - MornyCoeur.getAccount().execute(new SendSticker( + Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_WAITING ).replyToMessageId(event.message().messageId()) ); } else { - MornyCoeur.getAccount().execute(new SendSticker( + Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_403 ).replyToMessageId(event.message().messageId()) diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java index 0bd7830..2fc1fc1 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java @@ -1,6 +1,8 @@ package cc.sukazyo.cono.morny.bot.event.on_commands; import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.bot.api.Executor; + import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.model.request.ParseMode; @@ -16,7 +18,7 @@ public class GetUsernameAndId { public static void exec (@Nonnull String[] args, @Nonnull Update event) { - if (args.length > 1) { MornyCoeur.getAccount().execute(new SendMessage( + if (args.length > 1) { Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), "[Unavailable] Too much arguments." ).replyToMessageId(event.message().messageId())); return; } @@ -30,7 +32,7 @@ public class GetUsernameAndId { try { userId = Long.parseLong(args[0]); } catch (NumberFormatException e) { - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), "[Unavailable] " + e.getMessage() ).replyToMessageId(event.message().messageId())); @@ -38,12 +40,12 @@ public class GetUsernameAndId { } } - final GetChatMemberResponse response = MornyCoeur.getAccount().execute( + final GetChatMemberResponse response = Executor.as(MornyCoeur.getAccount()).exec( new GetChatMember(event.message().chat().id(), userId) ); if (response.chatMember() == null) { - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), "[Unavailable] user not found." ).replyToMessageId(event.message().messageId())); @@ -102,7 +104,7 @@ public class GetUsernameAndId { )); } - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), userInformation.toString() ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java index 12ccddb..5f56eb4 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java @@ -1,6 +1,7 @@ package cc.sukazyo.cono.morny.bot.event.on_commands; import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.data.ip186.IP186QueryResponse; import cc.sukazyo.untitled.util.telegram.object.InputCommand; import cc.sukazyo.cono.morny.data.ip186.IP186QueryHandler; @@ -26,7 +27,7 @@ public class Ip186Query { arg = event.message().replyToMessage().text(); } } else if (command.getArgs().length > 1) { - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), "[Unavailable] Too much arguments." ).replyToMessageId(event.message().messageId())); @@ -35,7 +36,7 @@ public class Ip186Query { arg = command.getArgs()[0]; } if (arg == null) { - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), "[Unavailable] No ip defined." ).replyToMessageId(event.message().messageId())); @@ -48,12 +49,12 @@ public class Ip186Query { case "/whois" -> IP186QueryHandler.queryWhois(arg); default -> throw new IllegalArgumentException("Unknown 186-IP query method " + command.getCommand()); }; - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), escapeHtml(response.url()) + "\n" + escapeHtml(response.body()) + "" ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); } catch (Exception e) { - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), "[Exception] in query:\n" + escapeHtml(e.getMessage()) + "" ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java index af6602f..dcd449c 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java @@ -5,6 +5,7 @@ import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.data.NbnhhshQuery; import cc.sukazyo.untitled.util.string.StringArrays; import cc.sukazyo.untitled.util.telegram.object.InputCommand; @@ -35,13 +36,13 @@ public class Nbnhhsh { } } - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), message.toString() ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); } catch (Exception e) { - MornyCoeur.getAccount().execute(new SendMessage( + Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( event.message().chat().id(), "[Exception] in query:\n" + escapeHtml(e.getMessage()) + "" ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); From c6c9f2b0e925b1d5bc04df4c73f468e10832e863 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 16 Jan 2022 03:06:23 +0800 Subject: [PATCH 5/8] =?UTF-8?q?util=E8=BF=81=E7=A7=BB=EF=BC=9Auntitled:uti?= =?UTF-8?q?l-telegram-api:1.2=20=E9=80=82=E5=BA=94,=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=20debug=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +-- .../cc/sukazyo/cono/morny/MornyCoeur.java | 10 ++++++ .../cc/sukazyo/cono/morny/MornyTrusted.java | 11 ++---- .../morny/bot/api/EventListenerManager.java | 2 ++ .../morny/bot/api/EventRuntimeException.java | 35 ------------------- .../sukazyo/cono/morny/bot/api/Executor.java | 32 ----------------- .../cono/morny/bot/event/OnCallMe.java | 11 +++--- .../morny/bot/event/OnCommandExecute.java | 23 ++++++------ .../morny/bot/event/OnEventHackHandle.java | 3 +- .../cono/morny/bot/event/OnInlineQuery.java | 3 +- .../morny/bot/event/OnUserSlashAction.java | 3 +- .../bot/event/on_commands/EventHack.java | 5 ++- .../event/on_commands/GetUsernameAndId.java | 11 +++--- .../bot/event/on_commands/Ip186Query.java | 9 +++-- .../morny/bot/event/on_commands/Nbnhhsh.java | 5 ++- 16 files changed, 49 insertions(+), 120 deletions(-) delete mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/api/EventRuntimeException.java delete mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/api/Executor.java diff --git a/gradle.properties b/gradle.properties index 284e63c..0e8762b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.3.3 +VERSION = 0.4.3.4 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 033315e..a927157 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.4.3.3"; - public static final long COMPILE_TIMESTAMP = 1641752627626L; + public static final String VERSION = "0.4.3.4"; + public static final long COMPILE_TIMESTAMP = 1642273245554L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index 7f8a239..b745d2d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -3,6 +3,8 @@ package cc.sukazyo.cono.morny; import cc.sukazyo.cono.morny.bot.api.OnUpdate; import cc.sukazyo.cono.morny.bot.event.EventListeners; import cc.sukazyo.cono.morny.data.tracker.TrackerDataManager; +import cc.sukazyo.untitled.telegram.api.extra.ExtraAction; + import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.request.GetMe; @@ -25,6 +27,7 @@ public class MornyCoeur { /** morny 的 bot 账户 */ private final TelegramBot account; + private final ExtraAction extraActionInstance; /** * morny 的 bot 账户的用户名
*
@@ -95,6 +98,8 @@ public class MornyCoeur { throw ex; } + this.extraActionInstance = ExtraAction.as(account); + logger.info("Bot login succeed."); } @@ -224,4 +229,9 @@ public class MornyCoeur { return INSTANCE.trusted; } + @Nonnull + public static ExtraAction extra () { + return INSTANCE.extraActionInstance; + } + } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java b/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java index f892b9d..eb9305e 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java @@ -1,7 +1,6 @@ package cc.sukazyo.cono.morny; -import com.pengrad.telegrambot.model.ChatMember; -import com.pengrad.telegrambot.request.GetChatMember; +import com.pengrad.telegrambot.model.ChatMember.Status; /** * 对用户进行身份权限验证的管理类 @@ -37,13 +36,7 @@ public class MornyTrusted { */ public boolean isTrusted (long userId) { if (userId == MASTER) return true; - final ChatMember chatMember = MornyCoeur.getAccount().execute(new GetChatMember(TRUSTED_CHAT_ID, userId)).chatMember(); - return ( - chatMember != null && ( - chatMember.status() == ChatMember.Status.administrator || - chatMember.status() == ChatMember.Status.creator - ) - ); + return MornyCoeur.extra().isUserInGroup(userId, TRUSTED_CHAT_ID, Status.administrator); } } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java index 5291509..f38d94a 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java @@ -3,6 +3,8 @@ package cc.sukazyo.cono.morny.bot.api; import com.google.gson.GsonBuilder; import com.pengrad.telegrambot.model.Update; +import cc.sukazyo.untitled.telegram.api.event.EventRuntimeException; + import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/EventRuntimeException.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventRuntimeException.java deleted file mode 100644 index 18b9366..0000000 --- a/src/main/java/cc/sukazyo/cono/morny/bot/api/EventRuntimeException.java +++ /dev/null @@ -1,35 +0,0 @@ -package cc.sukazyo.cono.morny.bot.api; - -import com.pengrad.telegrambot.response.BaseResponse; - -public class EventRuntimeException extends RuntimeException { - - public EventRuntimeException () { - super(); - } - - public EventRuntimeException (String message) { - super(message); - } - - public static class ActionFailed extends EventRuntimeException { - - private final BaseResponse response; - - public ActionFailed (BaseResponse response) { - super(); - this.response = response; - } - - public ActionFailed (String message, BaseResponse response) { - super(message); - this.response = response; - } - - public BaseResponse getResponse() { - return response; - } - - } - -} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/Executor.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/Executor.java deleted file mode 100644 index 4d89e3e..0000000 --- a/src/main/java/cc/sukazyo/cono/morny/bot/api/Executor.java +++ /dev/null @@ -1,32 +0,0 @@ -package cc.sukazyo.cono.morny.bot.api; - -import com.pengrad.telegrambot.TelegramBot; -import com.pengrad.telegrambot.request.BaseRequest; -import com.pengrad.telegrambot.response.BaseResponse; - -public class Executor { - - private final TelegramBot client; - - public Executor (TelegramBot bot) { - client = bot; - } - - public static Executor as (TelegramBot bot) { - return new Executor(bot); - } - - public , R extends BaseResponse> R exec (T req) { - return exec(req, ""); - } - - public , R extends BaseResponse> R exec (T req, String errorMessage) { - final R resp = client.execute(req); - if (!resp.isOk()) throw new EventRuntimeException.ActionFailed( - (errorMessage.equals("") ? String.valueOf(resp.errorCode()) : errorMessage), - resp - ); - return resp; - } - -} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java index 94d3138..f5b80d9 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java @@ -3,7 +3,6 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyTrusted; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.data.TelegramStickers; import com.pengrad.telegrambot.model.Chat; import com.pengrad.telegrambot.model.Update; @@ -56,7 +55,7 @@ public class OnCallMe extends EventListener { return false; } } - Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( + MornyCoeur.extra().exec(new SendSticker( update.message().chat().id(), TelegramStickers.ID_SENT ).replyToMessageId(update.message().messageId()) @@ -71,7 +70,7 @@ public class OnCallMe extends EventListener { * @param event 执行呼叫的tg事件 */ private static void requestSteamJoin (Update event) { - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( ME, String.format( """ request STEAM LIBRARY @@ -91,7 +90,7 @@ public class OnCallMe extends EventListener { * @param event 执行呼叫的tg事件 */ private static void requestHanaParesuJoin (Update event) { - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( ME, String.format( """ request Hana Paresu @@ -119,7 +118,7 @@ public class OnCallMe extends EventListener { * @since 0.4.2.2 */ private static void requestCustomCall (Update event) { - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( ME, String.format( """ request [???] @@ -130,7 +129,7 @@ public class OnCallMe extends EventListener { ) ) ).parseMode(ParseMode.HTML)); - Executor.as(MornyCoeur.getAccount()).exec(new ForwardMessage( + MornyCoeur.extra().exec(new ForwardMessage( ME, event.message().chat().id(), event.message().messageId() diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java index 1f6566d..f4febb9 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java @@ -4,7 +4,6 @@ import cc.sukazyo.cono.morny.GradleProjectConfigures; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornySystem; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.bot.event.on_commands.EventHack; import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId; import cc.sukazyo.cono.morny.bot.event.on_commands.Ip186Query; @@ -81,7 +80,7 @@ public class OnCommandExecute extends EventListener { private boolean nonCommandExecutable (Update event, InputCommand command) { if (command.getTarget() == null) return false; // 无法解析的命令,转交事件链后代处理 else { // 无法解析的显式命令格式,报错找不到命令 - Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( + MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_404 ).replyToMessageId(event.message().messageId()) @@ -91,7 +90,7 @@ public class OnCommandExecute extends EventListener { } private void onCommandOnExec (@Nonnull Update event) { - Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( + MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_ONLINE_STATUS_RETURN ).replyToMessageId(event.message().messageId()) @@ -99,7 +98,7 @@ public class OnCommandExecute extends EventListener { } private void onCommandHelloExec (@Nonnull Update event) { - Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( + MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_HELLO ).replyToMessageId(event.message().messageId()) @@ -108,7 +107,7 @@ public class OnCommandExecute extends EventListener { private void onCommandExitExec (@Nonnull Update event) { if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) { - Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( + MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_EXIT ).replyToMessageId(event.message().messageId()) @@ -116,7 +115,7 @@ public class OnCommandExecute extends EventListener { logger.info("Morny exited by user @" + event.message().from().username()); System.exit(0); } else { - Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( + MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_403 ).replyToMessageId(event.message().messageId()) @@ -126,7 +125,7 @@ public class OnCommandExecute extends EventListener { } private void onCommandVersionExec (@Nonnull Update event) { - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), String.format( """ @@ -134,7 +133,7 @@ public class OnCommandExecute extends EventListener { - %s core md5_hash: - %s - compile time<mp: + compile timestamp: - %d - %s [UTC]""", escapeHtml(MornySystem.VERSION), @@ -149,7 +148,7 @@ public class OnCommandExecute extends EventListener { * @since 0.4.1.2 */ private void onCommandRuntimeExec (@Nonnull Update event) { - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), String.format(""" system: @@ -198,7 +197,7 @@ public class OnCommandExecute extends EventListener { private void onCommandJrrpExec (Update event) { final double jrrp = MornyJrrp.getJrrpFromTelegramUser(event.message().from(), System.currentTimeMillis()); final String endChar = jrrp>70 ? "!" : jrrp>30 ? ";" : "..."; - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), String.format( "%s 在(utc的)今天的运气指数是———— %.2f%% %s", @@ -216,13 +215,13 @@ public class OnCommandExecute extends EventListener { if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) { logger.info(String.format("called save from command by @%s.", event.message().from().username())); MornyCoeur.callSaveData(); - Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( + MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_SAVED ).replyToMessageId(event.message().messageId()) ); } else { - Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( + MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_403 ).replyToMessageId(event.message().messageId()) diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java index f4d0aa0..a85a980 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java @@ -2,7 +2,6 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.untitled.util.telegram.formatting.MsgEscape; import com.google.gson.GsonBuilder; @@ -66,7 +65,7 @@ public class OnEventHackHandle extends EventListener { if (x == null) x = hackers.remove("[[]]"); if (x == null) return false; logger.debug("hacked event by " + x); - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(x.fromChatId, String.format( + MornyCoeur.extra().exec(new SendMessage(x.fromChatId, String.format( "%s", MsgEscape.escapeHtml(new GsonBuilder().setPrettyPrinting().create().toJson(update)) )).parseMode(ParseMode.HTML).replyToMessageId((int)x.fromMessageId)); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java index 7caaa63..5a64be5 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java @@ -2,7 +2,6 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.util.EncryptUtils; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; @@ -25,7 +24,7 @@ public class OnInlineQuery extends EventListener { */ @Override public boolean onInlineQuery (@Nonnull Update update) { - Executor.as(MornyCoeur.getAccount()).exec(new AnswerInlineQuery(update.inlineQuery().id(), new InlineQueryResultArticle[]{ + MornyCoeur.extra().exec(new AnswerInlineQuery(update.inlineQuery().id(), new InlineQueryResultArticle[]{ new InlineQueryResultArticle( EncryptUtils.encryptByMD5(update.inlineQuery().query()), "Raw Input", diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java index ce02efa..d220b4c 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java @@ -2,7 +2,6 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.untitled.util.command.CommonCommand; import cc.sukazyo.untitled.util.string.StringArrays; @@ -49,7 +48,7 @@ public class OnUserSlashAction extends EventListener { event.message().replyToMessage().from() )); - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), String.format( "%s %s%s %s%s%s", diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java index 60eea5d..36e2571 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java @@ -2,7 +2,6 @@ package cc.sukazyo.cono.morny.bot.event.on_commands; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyTrusted; -import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.bot.event.OnEventHackHandle; import cc.sukazyo.cono.morny.data.TelegramStickers; import cc.sukazyo.untitled.util.telegram.object.InputCommand; @@ -66,13 +65,13 @@ public class EventHack { } if (isOk) { - Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( + MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_WAITING ).replyToMessageId(event.message().messageId()) ); } else { - Executor.as(MornyCoeur.getAccount()).exec(new SendSticker( + MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_403 ).replyToMessageId(event.message().messageId()) diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java index 2fc1fc1..24fb3df 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java @@ -1,7 +1,6 @@ package cc.sukazyo.cono.morny.bot.event.on_commands; import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.cono.morny.bot.api.Executor; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.User; @@ -18,7 +17,7 @@ public class GetUsernameAndId { public static void exec (@Nonnull String[] args, @Nonnull Update event) { - if (args.length > 1) { Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + if (args.length > 1) { MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), "[Unavailable] Too much arguments." ).replyToMessageId(event.message().messageId())); return; } @@ -32,7 +31,7 @@ public class GetUsernameAndId { try { userId = Long.parseLong(args[0]); } catch (NumberFormatException e) { - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), "[Unavailable] " + e.getMessage() ).replyToMessageId(event.message().messageId())); @@ -40,12 +39,12 @@ public class GetUsernameAndId { } } - final GetChatMemberResponse response = Executor.as(MornyCoeur.getAccount()).exec( + final GetChatMemberResponse response = MornyCoeur.extra().exec( new GetChatMember(event.message().chat().id(), userId) ); if (response.chatMember() == null) { - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), "[Unavailable] user not found." ).replyToMessageId(event.message().messageId())); @@ -104,7 +103,7 @@ public class GetUsernameAndId { )); } - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), userInformation.toString() ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java index 5f56eb4..48c7f86 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java @@ -1,7 +1,6 @@ package cc.sukazyo.cono.morny.bot.event.on_commands; import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.data.ip186.IP186QueryResponse; import cc.sukazyo.untitled.util.telegram.object.InputCommand; import cc.sukazyo.cono.morny.data.ip186.IP186QueryHandler; @@ -27,7 +26,7 @@ public class Ip186Query { arg = event.message().replyToMessage().text(); } } else if (command.getArgs().length > 1) { - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), "[Unavailable] Too much arguments." ).replyToMessageId(event.message().messageId())); @@ -36,7 +35,7 @@ public class Ip186Query { arg = command.getArgs()[0]; } if (arg == null) { - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), "[Unavailable] No ip defined." ).replyToMessageId(event.message().messageId())); @@ -49,12 +48,12 @@ public class Ip186Query { case "/whois" -> IP186QueryHandler.queryWhois(arg); default -> throw new IllegalArgumentException("Unknown 186-IP query method " + command.getCommand()); }; - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), escapeHtml(response.url()) + "\n" + escapeHtml(response.body()) + "" ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); } catch (Exception e) { - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), "[Exception] in query:\n" + escapeHtml(e.getMessage()) + "" ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java index dcd449c..8cf4649 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java @@ -5,7 +5,6 @@ import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.cono.morny.bot.api.Executor; import cc.sukazyo.cono.morny.data.NbnhhshQuery; import cc.sukazyo.untitled.util.string.StringArrays; import cc.sukazyo.untitled.util.telegram.object.InputCommand; @@ -36,13 +35,13 @@ public class Nbnhhsh { } } - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), message.toString() ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); } catch (Exception e) { - Executor.as(MornyCoeur.getAccount()).exec(new SendMessage( + MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), "[Exception] in query:\n" + escapeHtml(e.getMessage()) + "" ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); From 07f111a6e7cd18fa0c6210599aafe489f5ef7fd3 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 16 Jan 2022 09:47:57 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20nbnhhsh=20=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=AF=B9=20[inputting]"=E5=8F=AF=E8=83=BD=E7=9A=84"?= =?UTF-8?q?=20=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../sukazyo/cono/morny/GradleProjectConfigures.java | 4 ++-- .../cono/morny/bot/event/on_commands/Nbnhhsh.java | 13 +++++++++++-- .../cc/sukazyo/cono/morny/data/NbnhhshQuery.java | 1 + 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/gradle.properties b/gradle.properties index 0e8762b..26864bd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.3.4 +VERSION = 0.4.3.5 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index a927157..1b20bb4 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.4.3.4"; - public static final long COMPILE_TIMESTAMP = 1642273245554L; + public static final String VERSION = "0.4.3.5"; + public static final long COMPILE_TIMESTAMP = 1642297552612L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java index 8cf4649..7dd130e 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java @@ -28,11 +28,20 @@ public class Nbnhhsh { StringBuilder message = new StringBuilder("## Result of nbnhhsh query :"); for (NbnhhshQuery.Word word : response.words) { - if (word.trans == null) continue; + if (word.trans != null && word.trans.length == 0) word.trans = null; + if (word.inputting != null && word.inputting.length == 0) word.inputting = null; + if (word.trans == null && word.inputting == null) continue; message.append("\n\n[[ ").append(escapeHtml(word.name)).append(" ]]"); - for (String trans : word.trans) { + if (word.trans != null) for (String trans : word.trans) { message.append("\n* ").append(escapeHtml(trans)).append(""); } + if (word.inputting != null) { + if (word.trans != null) message.append("\n"); + message.append(" maybe:"); + for (String trans : word.inputting) { + message.append("\n` ").append(escapeHtml(trans)).append(""); + } + } } MornyCoeur.extra().exec(new SendMessage( diff --git a/src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java b/src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java index 27c5932..90119aa 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java @@ -16,6 +16,7 @@ public class NbnhhshQuery { public static class Word { public String name; public String[] trans; + public String[] inputting; } public static class GuessResult { From 8f2c63e3003b36ae1a660f1626cfdc550c07f48c Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 21 Jan 2022 14:53:32 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E6=9A=82=E5=81=9C=E6=96=9C=E7=BA=BF?= =?UTF-8?q?=E5=8A=A8=E4=BD=9C=E5=9C=A8=20.DP7=20=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=BE=A4=E7=BB=84=E8=BF=90=E4=BD=9C=20@Lapis-Apple?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 ++-- .../cono/morny/bot/event/OnUserSlashAction.java | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/gradle.properties b/gradle.properties index 26864bd..91778ef 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.3.5 +VERSION = 0.4.3.6 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 1b20bb4..9710257 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.4.3.5"; - public static final long COMPILE_TIMESTAMP = 1642297552612L; + public static final String VERSION = "0.4.3.6"; + public static final long COMPILE_TIMESTAMP = 1642747921571L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java index d220b4c..317ccce 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java @@ -12,6 +12,7 @@ import com.pengrad.telegrambot.request.SendMessage; import javax.annotation.Nonnull; +import static cc.sukazyo.cono.morny.Log.logger; import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; public class OnUserSlashAction extends EventListener { @@ -21,6 +22,20 @@ public class OnUserSlashAction extends EventListener { final String text = event.message().text(); if (text == null) return false; + // Due to @Lapis_Apple, we stopped slash action function at .DP7 groups. + // It may be enabled after some of updates when the function will not be conflicted to other bots. + // if (event.message().chat().id() == ) return false; + if (event.message().chat().title() != null && event.message().chat().title().contains(".DP7")) { + logger.info(String.format(""" + Chat slash action ignored due to keyword \".DP7\". + - %s + - [%d]""", + event.message().chat().title(), + event.message().chat().id() + )); + return false; + } + if (text.startsWith("/")) { int prefixLength = 1; boolean useVerbSuffix = true; From 3762261cff0c3b7de547264665533b4ead11787b Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 24 Jan 2022 12:25:08 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=20TG=20=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=20untitled=20=E5=BA=93=EF=BC=8C=E6=9B=B4=E6=96=B0=20untitled?= =?UTF-8?q?=20=E7=B1=BB=E5=BA=93=E5=AF=BC=E5=85=A5=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E6=97=A5=E5=B8=B8=E4=BE=9D=E8=B5=96=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - groovy:untitled(String,String) - dependencies - untitled:util-telegram-api-formatter + - spotbugs 4.5.2 -> 4.5.3 - javaTelegramApi 5.5.0 -> 5.6.0 --- build.gradle | 13 +++++-- gradle.properties | 8 ++-- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cono/morny/bot/event/OnCallMe.java | 24 ++++-------- .../morny/bot/event/OnCommandExecute.java | 14 +++---- .../morny/bot/event/OnUserSlashAction.java | 37 ++++++++++--------- 6 files changed, 49 insertions(+), 51 deletions(-) diff --git a/build.gradle b/build.gradle index 4ec752e..1720565 100644 --- a/build.gradle +++ b/build.gradle @@ -17,14 +17,19 @@ repositories { maven { name '-ws'; url 'https://mvn.sukazyo.cc' } } +String untitled (String lib, String upd = null) { + int majorCode = Integer.parseInt(project.libUntitledVersionMajor) + return "cc.sukazyo.untitled:$lib:[$majorCode${upd==null?"":".$upd"}, ${majorCode+1}[" +} dependencies { compileOnlyApi "com.github.spotbugs:spotbugs-annotations:${libSpotbugsVersion}" - implementation "cc.sukazyo.untitled:util-string-commons:${libUntitledVersion}" - implementation "cc.sukazyo.untitled:util-telegram-commons:${libUntitledVersion}" - implementation "cc.sukazyo.untitled:util-telegram-api:${libUntitledVersion}" - + implementation untitled("util-command-parser","1.0") + implementation untitled("util-string-commons", "1.0") + implementation untitled("util-telegram-api", "2.1") + implementation untitled("util-telegram-api-formatter", "3.3") + implementation untitled("util-telegram-commons", "1.0") api "cc.sukazyo:messiva:${libMessivaVersion}" implementation "com.github.pengrad:java-telegram-bot-api:${libJavaTelegramBotApiVersion}" diff --git a/gradle.properties b/gradle.properties index 91778ef..78cc80d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,15 +1,15 @@ ## Core -VERSION = 0.4.3.6 +VERSION = 0.4.3.7 # dependencies -libSpotbugsVersion = 4.5.2 +libSpotbugsVersion = 4.5.3 -libUntitledVersion = 1.+ +libUntitledVersionMajor = 1 libMessivaVersion = 0.1.0.1 -libJavaTelegramBotApiVersion = 5.5.0 +libJavaTelegramBotApiVersion = 5.6.0 libJunitVersion = 5.8.2 diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 9710257..91f2abf 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.4.3.6"; - public static final long COMPILE_TIMESTAMP = 1642747921571L; + public static final String VERSION = "0.4.3.7"; + public static final long COMPILE_TIMESTAMP = 1642998030356L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java index f5b80d9..ef4e85b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java @@ -4,6 +4,7 @@ import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyTrusted; import cc.sukazyo.cono.morny.bot.api.EventListener; import cc.sukazyo.cono.morny.data.TelegramStickers; +import cc.sukazyo.untitled.telegram.api.formatting.TGToString; import com.pengrad.telegrambot.model.Chat; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; @@ -13,8 +14,6 @@ import com.pengrad.telegrambot.request.SendSticker; import javax.annotation.Nonnull; -import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; - /** * 通过 bot 呼叫主人的事件监听管理类 * @since 0.4.2.1 @@ -74,11 +73,8 @@ public class OnCallMe extends EventListener { ME, String.format( """ request STEAM LIBRARY - from %s""", - event.message().from().id(), - escapeHtml( - event.message().from().firstName() + " " + event.message().from().lastName() - ) + from %s""", + TGToString.as(event.message().from()).fullnameRefHtml() ) ).parseMode(ParseMode.HTML)); } @@ -94,11 +90,8 @@ public class OnCallMe extends EventListener { ME, String.format( """ request Hana Paresu - from %s""", - event.message().from().id(), - escapeHtml( - event.message().from().firstName() + " " + event.message().from().lastName() - ) + from %s""", + TGToString.as(event.message().from()).fullnameRefHtml() ) ).parseMode(ParseMode.HTML)); } @@ -122,11 +115,8 @@ public class OnCallMe extends EventListener { ME, String.format( """ request [???] - from %s""", - event.message().from().id(), - escapeHtml( - event.message().from().firstName() + " " + event.message().from().lastName() - ) + from %s""", + TGToString.as(event.message().from()).fullnameRefHtml() ) ).parseMode(ParseMode.HTML)); MornyCoeur.extra().exec(new ForwardMessage( diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java index f4febb9..6196de1 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java @@ -10,6 +10,7 @@ import cc.sukazyo.cono.morny.bot.event.on_commands.Ip186Query; import cc.sukazyo.cono.morny.bot.event.on_commands.Nbnhhsh; import cc.sukazyo.cono.morny.data.MornyJrrp; import cc.sukazyo.cono.morny.data.TelegramStickers; +import cc.sukazyo.untitled.telegram.api.formatting.TGToString; import cc.sukazyo.untitled.util.telegram.object.InputCommand; import com.pengrad.telegrambot.model.Update; @@ -112,7 +113,7 @@ public class OnCommandExecute extends EventListener { TelegramStickers.ID_EXIT ).replyToMessageId(event.message().messageId()) ); - logger.info("Morny exited by user @" + event.message().from().username()); + logger.info("Morny exited by user " + TGToString.as(event.message().from()).toStringLogTag()); System.exit(0); } else { MornyCoeur.extra().exec(new SendSticker( @@ -120,7 +121,7 @@ public class OnCommandExecute extends EventListener { TelegramStickers.ID_403 ).replyToMessageId(event.message().messageId()) ); - logger.info("403 exited tag from user @" + event.message().from().username()); + logger.info("403 exited tag from user " + TGToString.as(event.message().from()).toStringLogTag()); } } @@ -200,9 +201,8 @@ public class OnCommandExecute extends EventListener { MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), String.format( - "%s 在(utc的)今天的运气指数是———— %.2f%% %s", - event.message().from().id(), - escapeHtml(event.message().from().firstName()), + "%s 在(utc的)今天的运气指数是———— %.2f%% %s", + TGToString.as(event.message().from()).fullnameRefHtml(), jrrp, escapeHtml(endChar) ) ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); @@ -213,7 +213,7 @@ public class OnCommandExecute extends EventListener { */ private void onSaveDataExec (Update event) { if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) { - logger.info(String.format("called save from command by @%s.", event.message().from().username())); + logger.info("called save from command by " + TGToString.as(event.message().from()).toStringLogTag()); MornyCoeur.callSaveData(); MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), @@ -226,7 +226,7 @@ public class OnCommandExecute extends EventListener { TelegramStickers.ID_403 ).replyToMessageId(event.message().messageId()) ); - logger.info("403 call save tag from user @" + event.message().from().username()); + logger.info("403 call save tag from user " + TGToString.as(event.message().from()).toStringLogTag()); } } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java index 317ccce..5c4cf79 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java @@ -2,6 +2,7 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.untitled.telegram.api.formatting.TGToString; import cc.sukazyo.untitled.util.command.CommonCommand; import cc.sukazyo.untitled.util.string.StringArrays; @@ -22,21 +23,21 @@ public class OnUserSlashAction extends EventListener { final String text = event.message().text(); if (text == null) return false; - // Due to @Lapis_Apple, we stopped slash action function at .DP7 groups. - // It may be enabled after some of updates when the function will not be conflicted to other bots. - // if (event.message().chat().id() == ) return false; - if (event.message().chat().title() != null && event.message().chat().title().contains(".DP7")) { - logger.info(String.format(""" - Chat slash action ignored due to keyword \".DP7\". - - %s - - [%d]""", - event.message().chat().title(), - event.message().chat().id() - )); - return false; - } - if (text.startsWith("/")) { + + /// Due to @Lapis_Apple, we stopped slash action function at .DP7 groups. + /// It may be enabled after some updates when the function will not be conflicted to other bots. + // if (event.message().chat().id() == ) return false; + if (event.message().chat().title() != null && event.message().chat().title().contains(".DP7")) { + logger.info(String.format(""" + Chat slash action ignored due to the following keyword. + - %s + - ".DP7\"""", + TGToString.as(event.message().chat()).toStringFullNameId() + )); + return false; + } + int prefixLength = 1; boolean useVerbSuffix = true; boolean useObjectPrefix = true; @@ -66,10 +67,12 @@ public class OnUserSlashAction extends EventListener { MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), String.format( - "%s %s%s %s%s%s", - origin.id(), escapeHtml(origin.firstName()), + "%s %s%s %s%s%s", + TGToString.as(origin).firstnameRefHtml(), verb, escapeHtml((useVerbSuffix?"了":"")), - target.id(), escapeHtml((origin==target ? "自己" : target.firstName())), + origin==target ? + "自己" : + TGToString.as(target).firstnameRefHtml(), escapeHtml((hasObject ? (useObjectPrefix ?" 的": " ") : "")), escapeHtml((hasObject ? object : "")) )