From 82c9faea1edbd457df34b766c31dc50bcb865d6e Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 16 Mar 2022 02:43:47 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=AE=9E=E9=AA=8C=E6=80=A7=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BA=86=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E6=89=80?= =?UTF-8?q?=E5=9C=A8dc=20(cdn)=20#6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +-- .../morny/util/TelegramUserInformation.java | 36 ++++++++++++++++++- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/gradle.properties b/gradle.properties index f0b6faa..6a0e7c4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.6.0.2 +VERSION = 0.7.0.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 1316cb7..600389b 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.6.0.2"; - public static final long COMPILE_TIMESTAMP = 1647237887029L; + public static final String VERSION = "0.7.0.0"; + public static final long COMPILE_TIMESTAMP = 1647369713069L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java b/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java index 011e4da..18e109f 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java @@ -1,10 +1,40 @@ package cc.sukazyo.cono.morny.util; import com.pengrad.telegrambot.model.User; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; public class TelegramUserInformation { + public static final String DC_QUERY_SOURCE_SITE = "https://t.me/"; + public static final Pattern DC_QUERY_PROCESSOR_REGEX = Pattern.compile("(cdn[1-9]).tele(sco.pe|gram-cdn.org)"); + + private static final OkHttpClient httpClient = new OkHttpClient(); + + @Nullable + public static String getDataCenterFromUsername (String username) { + final Request request = new Request.Builder().url(DC_QUERY_SOURCE_SITE + username).build(); + try (Response response = httpClient.newCall(request).execute()) { + final ResponseBody body = response.body(); + if (body == null) return "empty upstream response"; + final Matcher matcher = DC_QUERY_PROCESSOR_REGEX.matcher(body.string()); + if (matcher.find()) { + return matcher.group(1); + } + } catch (IOException e) { + return e.getMessage(); + } + return null; + } + public static String informationOutputHTML (User user) { final StringBuilder userInformation = new StringBuilder(); @@ -15,7 +45,7 @@ public class TelegramUserInformation { user.id() )); if (user.username() == null) { - userInformation.append("\nusername : null"); + userInformation.append("\nusername : null\ndatacenter : null"); } else { userInformation.append(String.format( """ @@ -24,6 +54,10 @@ public class TelegramUserInformation { - %s""", escapeHtml(user.username()) )); + // 依赖 username 的 datacenter 查询 + final String dataCenter = getDataCenterFromUsername(user.username()); + if (dataCenter == null) { userInformation.append("\ndatacenter : null"); } + else { userInformation.append(String.format("\ndatacenter : %s", escapeHtml(dataCenter))); } } if (user.firstName() == null) { userInformation.append("\nfirstname : null"); From 5046a272665d45471f9f2d27584db99fbc9010c7 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 16 Mar 2022 17:51:56 +0800 Subject: [PATCH 02/12] =?UTF-8?q?runtime=20=E4=B8=BB=E6=9C=BA=E5=90=8D?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=EF=BC=8C=E4=BF=A1=E6=81=AF=E5=AE=B9=E9=94=99?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 runtime - system 栏中添加主机名显示 - 如果显示出错则会是 "" - runtime - morny version 改名为 coeur version - MornySystem.getJarMd5 出错时现在不会再直接返回错误而是会返回 "" 或是 "" 信息 --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +-- .../cc/sukazyo/cono/morny/MornySystem.java | 14 +++++++---- .../cono/morny/bot/command/MornyCommands.java | 12 ++++++++- .../cc/sukazyo/cono/morny/util/FileUtils.java | 25 ++++++++----------- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/gradle.properties b/gradle.properties index 6a0e7c4..92502e9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.0 +VERSION = 0.7.0.1 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 600389b..c8c9109 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.7.0.0"; - public static final long COMPILE_TIMESTAMP = 1647369713069L; + public static final String VERSION = "0.7.0.1"; + public static final long COMPILE_TIMESTAMP = 1647424091182L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornySystem.java b/src/main/java/cc/sukazyo/cono/morny/MornySystem.java index 4059bd0..4dbca43 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornySystem.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornySystem.java @@ -3,7 +3,9 @@ package cc.sukazyo.cono.morny; import cc.sukazyo.cono.morny.util.FileUtils; import javax.annotation.Nonnull; +import java.io.IOException; import java.net.URISyntaxException; +import java.security.NoSuchAlgorithmException; /** * Morny Cono 的 Coeur 的程序属性存放类 @@ -20,20 +22,22 @@ public class MornySystem { * 获取程序 jar 文件的 md5-hash 值
*
* 只支持 jar 文件方式启动的程序 —— - * 如果是通过 classpath 来启动,则会返回找不到文件的错误数据
- * - 或许需要注意,这种情况下会出现程序文件所在的路径
+ * 如果是通过 classpath 来启动,程序无法找到本体jar文件,则会返回 {@code } 文本 *
* 值格式为 {@link java.lang.String} * - * @return 程序jar文件的 md5-hash 值字符串,或错误信息 + * @return 程序jar文件的 md5-hash 值字符串,或 {@code } 如果出现错误 */ @Nonnull public static String getJarMd5() { try { return FileUtils.getMD5Three(MornyCoeur.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); - } catch (URISyntaxException e) { + } catch (IOException | URISyntaxException e) { e.printStackTrace(System.out); - return e.getMessage(); + return ""; + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(System.out); + return ""; } } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java index 9b53ac1..69173d4 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java @@ -18,6 +18,8 @@ import com.pengrad.telegrambot.request.SetMyCommands; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -245,19 +247,26 @@ public class MornyCommands { * @since 0.4.1.2 */ private static void onCommandRuntimeExec (@Nonnull Update event) { + String hostname; + try { + hostname = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + hostname = ""; + } MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), String.format(""" system: - %s - %s + - %s - %d cores java runtime: - %s - %s vm memory: - %d / %d MB - morny version: + coeur version: - %s - %s - %s [UTC] @@ -268,6 +277,7 @@ public class MornyCommands { - %s [UTC] - [%d]""", // system + escapeHtml(hostname), escapeHtml(System.getProperty("os.name")), escapeHtml(System.getProperty("os.version")), Runtime.getRuntime().availableProcessors(), diff --git a/src/main/java/cc/sukazyo/cono/morny/util/FileUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/FileUtils.java index c64a3db..57ff657 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/FileUtils.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/FileUtils.java @@ -10,23 +10,18 @@ import java.security.NoSuchAlgorithmException; public class FileUtils { @Nonnull - public static String getMD5Three (@Nonnull String path) { + public static String getMD5Three (@Nonnull String path) throws IOException, NoSuchAlgorithmException { final BigInteger bi; - try { - final byte[] buffer = new byte[8192]; - int len; - final MessageDigest md = MessageDigest.getInstance("MD5"); - final FileInputStream fis = new FileInputStream(path); - while ((len = fis.read(buffer)) != -1) { - md.update(buffer, 0, len); - } - fis.close(); - final byte[] b = md.digest(); - bi = new BigInteger(1, b); - } catch (NoSuchAlgorithmException | IOException e) { - e.printStackTrace(System.out); - return e.getMessage(); + final byte[] buffer = new byte[8192]; + int len; + final MessageDigest md = MessageDigest.getInstance("MD5"); + final FileInputStream fis = new FileInputStream(path); + while ((len = fis.read(buffer)) != -1) { + md.update(buffer, 0, len); } + fis.close(); + final byte[] b = md.digest(); + bi = new BigInteger(1, b); return bi.toString(16); } From ce53e70dfa3be62a6790684cd3015f603d39ca55 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 16 Mar 2022 17:51:56 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20XXX=E8=BF=98?= =?UTF-8?q?=E6=98=AFXXX=20=E7=9A=84=E5=8A=9F=E8=83=BD=20=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E6=96=9C=E7=BA=BF=E5=89=8D=E7=BC=80=E8=A7=A6=E5=8F=91=20#2?= 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/EventListeners.java | 2 + .../cono/morny/bot/event/OnUserRandoms.java | 48 +++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java diff --git a/gradle.properties b/gradle.properties index 92502e9..8213035 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.1 +VERSION = 0.7.0.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 c8c9109..b1abc49 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.7.0.1"; - public static final long COMPILE_TIMESTAMP = 1647424091182L; + public static final String VERSION = "0.7.0.2"; + public static final long COMPILE_TIMESTAMP = 1647451512009L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java index 4eca5a5..2f62796 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java @@ -12,6 +12,7 @@ public class EventListeners { public static final OnCallMe CALL_ME = new OnCallMe(); public static final OnEventHackHandle EVENT_HACK_HANDLE = new OnEventHackHandle(); public static final OnKuohuanhuanNeedSleep KUOHUANHUAN_NEED_SLEEP = new OnKuohuanhuanNeedSleep(); + public static final OnUserRandoms USER_RANDOMS = new OnUserRandoms(); public static void registerAllListeners () { EventListenerManager.addListener( @@ -19,6 +20,7 @@ public class EventListeners { UPDATE_TIMESTAMP_OFFSET_LOCK, KUOHUANHUAN_NEED_SLEEP, COMMANDS_LISTENER, + USER_RANDOMS, USER_SLASH_ACTION, INLINE_QUERY, CALL_ME, diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java new file mode 100644 index 0000000..cd90b67 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java @@ -0,0 +1,48 @@ +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.util.command.CommonCommand; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.request.SendMessage; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.ThreadLocalRandom; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class OnUserRandoms extends EventListener { + + private static final Pattern USER_OR_CN_QUERY = Pattern.compile("(.+)还是(.+)"); + private static final Pattern USER_OR_EN_QUERY = Pattern.compile("(.+)or(.+)"); + + @Override + public boolean onMessage (@NotNull Update update) { + + if (update.message().text() == null) return false; + if (!update.message().text().startsWith("/")) return false; + + final String[] preProcess = CommonCommand.format(update.message().text()); + if (preProcess.length > 1) return false; + final String query = preProcess[0]; + + String result = null; + final Matcher matcher; + if (query.contains("还是")) { + matcher = USER_OR_CN_QUERY.matcher(query); + } else { + matcher = USER_OR_EN_QUERY.matcher(query); + } + if (matcher.find()) { + result = ThreadLocalRandom.current().nextBoolean() ? matcher.group(1) : matcher.group(2); + } + + if (result == null) return false; + MornyCoeur.extra().exec(new SendMessage( + update.message().chat().id(), result + ).replyToMessageId(update.message().messageId())); + return true; + + } + +} From 653ba4c0a849a46dc80bd389cad98386b96f9417 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Thu, 17 Mar 2022 01:31:56 +0800 Subject: [PATCH 04/12] =?UTF-8?q?(=E6=B0=B4)=20=E8=A1=A5=E4=B8=80=E7=82=B9?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=97=B2=E8=81=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java index cd90b67..3655a97 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java @@ -26,6 +26,9 @@ public class OnUserRandoms extends EventListener { if (preProcess.length > 1) return false; final String query = preProcess[0]; + // ----- START CODE BLOCK COMMENT ----- + // 这里实现思路和代码优化有至少一半是 copilot 和 IDEA 提供的 + // 实现思路都可以从人类手里抢一半贡献太恐怖了aba String result = null; final Matcher matcher; if (query.contains("还是")) { @@ -36,6 +39,7 @@ public class OnUserRandoms extends EventListener { if (matcher.find()) { result = ThreadLocalRandom.current().nextBoolean() ? matcher.group(1) : matcher.group(2); } + // ----- STOP CODE BLOCK COMMENT ----- if (result == null) return false; MornyCoeur.extra().exec(new SendMessage( From 6dfaade55ce44c4c5692f9b83a2267aad8ee2eea Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 1 Apr 2022 16:59:51 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=88=A0=E9=99=A4=20bot=20=E5=9B=9E=E5=A4=8D=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=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 +- .../cc/sukazyo/cono/morny/MornyCoeur.java | 25 ++++++--- .../morny/bot/command/DirectMsgClear.java | 52 +++++++++++++++++++ .../cono/morny/bot/command/MornyCommands.java | 5 ++ 5 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java diff --git a/gradle.properties b/gradle.properties index 8213035..045c096 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.2 +VERSION = 0.7.0.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 b1abc49..da0d29d 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.7.0.2"; - public static final long COMPILE_TIMESTAMP = 1647451512009L; + public static final String VERSION = "0.7.0.3"; + public static final long COMPILE_TIMESTAMP = 1648803402848L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index 07ff390..23c7539 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -8,7 +8,7 @@ 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.model.DeleteMyCommands; +import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.request.GetMe; import javax.annotation.Nonnull; @@ -44,7 +44,13 @@ public class MornyCoeur { * 如果在登陆之前就定义了此字段,则登陆代码会验证登陆的 bot 的 username * 是否与定义的 username 符合。如果不符合则会报错。 */ - private final String username; + public final String username; + /** + * morny 的 bot 账户的 telegram id
+ *
+ * 这个字段将会在登陆成功后赋值为登录到的 bot 的 id。 + */ + public final long userid; /** * morny 的事件忽略前缀时间
*
@@ -58,7 +64,7 @@ public class MornyCoeur { */ public static final long coeurStartTimestamp = System.currentTimeMillis(); - private record LogInResult(TelegramBot account, String username) { } + private record LogInResult(TelegramBot account, String username, long userid) { } /** * 执行 bot 初始化 @@ -91,6 +97,7 @@ public class MornyCoeur { final LogInResult loginResult = login(botKey, botUsername); this.account = loginResult.account; this.username = loginResult.username; + this.userid = loginResult.userid; this.trusted = new MornyTrusted(master, trustedChat); logger.info(String.format(""" trusted param set: @@ -190,11 +197,11 @@ public class MornyCoeur { for (int i = 1; i < 4; i++) { if (i != 1) logger.info("retrying..."); try { - final String username = account.execute(new GetMe()).user().username(); - if (requireName != null && !requireName.equals(username)) - throw new RuntimeException("Required the bot @" + requireName + " but @" + username + " logged in!"); - logger.info("Succeed login to @" + username); - return new LogInResult(account, username); + final User remote = account.execute(new GetMe()).user(); + if (requireName != null && !requireName.equals(remote.username())) + throw new RuntimeException("Required the bot @" + requireName + " but @" + remote.username() + " logged in!"); + logger.info("Succeed login to @" + remote.username()); + return new LogInResult(account, remote.username(), remote.id()); } catch (Exception e) { e.printStackTrace(System.out); logger.error("login failed."); @@ -267,4 +274,6 @@ public class MornyCoeur { return INSTANCE.extraActionInstance; } + public static long getUserid () { return INSTANCE.userid; } + } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java new file mode 100644 index 0000000..dc9372f --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java @@ -0,0 +1,52 @@ +package cc.sukazyo.cono.morny.bot.command; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.untitled.util.telegram.object.InputCommand; +import com.pengrad.telegrambot.model.Chat; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.request.DeleteMessage; +import com.pengrad.telegrambot.request.GetChatMember; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class DirectMsgClear implements ISimpleCommand { + + @Nonnull @Override public String getName () { return "/r"; } + + @Nullable @Override public String[] getAliases () { return new String[0]; } + + @Override + public void execute (@Nonnull InputCommand command, @Nonnull Update event) { + + if (event.message().replyToMessage() == null) return; + if (event.message().replyToMessage().from().id() != MornyCoeur.getUserid()) return; + if (event.message().replyToMessage().date() - System.currentTimeMillis()/1000 < 48*60*60) return; + + final boolean isTrusted = MornyCoeur.trustedInstance().isTrusted(event.message().chat().id()); + + if ( + isTrusted || ( + event.message().replyToMessage().replyToMessage() != null && + event.message().replyToMessage().replyToMessage().from().id().equals(event.message().from().id()) + ) + ) { + + MornyCoeur.extra().exec(new DeleteMessage( + event.message().chat().id(), event.message().replyToMessage().messageId() + )); + if (event.message().chat().type() == Chat.Type.Private || ( + MornyCoeur.extra().exec( + new GetChatMember(event.message().chat().id(), event.message().from().id()) + ).chatMember().canDeleteMessages() + )) { + MornyCoeur.extra().exec(new DeleteMessage( + event.message().chat().id(), event.message().messageId() + )); + } + + } + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java index 69173d4..450db41 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java @@ -73,6 +73,11 @@ public class MornyCommands { new Exit() ); + // 特殊的命令 + register( + new DirectMsgClear() + ); + // 统一注册这些奇怪的东西&.& register( new 喵呜.抱抱(), From 9f9b9fbfa5f7f8cb0a1889dc72287e1eadef071c Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 2 Apr 2022 17:12:04 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8A=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=8F=90=E4=BA=A4=E7=9A=84=E4=B8=80=E4=B8=AA=20typo?= =?UTF-8?q?=20bug?= 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/command/DirectMsgClear.java | 12 +++++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/gradle.properties b/gradle.properties index 045c096..23d4136 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.3 +VERSION = 0.7.0.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 da0d29d..d94b461 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.7.0.3"; - public static final long COMPILE_TIMESTAMP = 1648803402848L; + public static final String VERSION = "0.7.0.4"; + public static final long COMPILE_TIMESTAMP = 1648890683777L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java index dc9372f..7e3cc82 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java @@ -10,6 +10,8 @@ import com.pengrad.telegrambot.request.GetChatMember; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import static cc.sukazyo.cono.morny.Log.logger; + public class DirectMsgClear implements ISimpleCommand { @Nonnull @Override public String getName () { return "/r"; } @@ -19,11 +21,15 @@ public class DirectMsgClear implements ISimpleCommand { @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { + logger.debug("Executing command /r"); if (event.message().replyToMessage() == null) return; + logger.trace("Message is a reply"); if (event.message().replyToMessage().from().id() != MornyCoeur.getUserid()) return; - if (event.message().replyToMessage().date() - System.currentTimeMillis()/1000 < 48*60*60) return; + logger.trace("Message is from me"); + if (System.currentTimeMillis()/1000 - event.message().replyToMessage().date() > 48*60*60) return; + logger.trace("Message is not older than 48 hours"); - final boolean isTrusted = MornyCoeur.trustedInstance().isTrusted(event.message().chat().id()); + final boolean isTrusted = MornyCoeur.trustedInstance().isTrusted(event.message().from().id()); if ( isTrusted || ( @@ -45,7 +51,7 @@ public class DirectMsgClear implements ISimpleCommand { )); } - } + } else logger.trace("User is not trusted"); } From 16d74a3af56a8acf741baca29604b9dad00cb77b Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Thu, 19 May 2022 15:14:18 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E7=A8=8D=E5=BE=AE=E4=BF=AE=E6=94=B9=20/r?= =?UTF-8?q?untime=20=E5=91=BD=E4=BB=A4=E7=9A=84=E8=BF=94=E5=9B=9E=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E6=B7=BB=E5=8A=A0=20/start=20=E6=89=93?= =?UTF-8?q?=E6=8B=9B=E5=91=BC=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/command/MornyCommands.java | 12 +++++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/gradle.properties b/gradle.properties index 23d4136..791e9a4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.4 +VERSION = 0.7.0.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 d94b461..ad433d0 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.7.0.4"; - public static final long COMPILE_TIMESTAMP = 1648890683777L; + public static final String VERSION = "0.7.0.5"; + public static final long COMPILE_TIMESTAMP = 1652944218716L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java index 450db41..7c72538 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java @@ -61,6 +61,7 @@ public class MornyCommands { register( new ON(), new Hello(), + new HelloOnStart(), new GetUsernameAndId(), new EventHack(), new Nbnhhsh(), @@ -180,6 +181,7 @@ public class MornyCommands { @Nonnull @Override public String getDescription () { return "打招呼"; } @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { onCommandHelloExec(event); } } + private static class HelloOnStart implements ISimpleCommand { @Nonnull @Override public String getName () { return "/start"; }@Nullable @Override public String[] getAliases () { return new String[0]; }@Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { onCommandHelloExec(event); }} private static void onCommandHelloExec (@Nonnull Update event) { MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), @@ -265,12 +267,12 @@ public class MornyCommands { - %s - %s - %s - - %d cores java runtime: - %s - %s vm memory: - %d / %d MB + - %d cores coeur version: - %s - %s @@ -283,15 +285,15 @@ public class MornyCommands { - [%d]""", // system escapeHtml(hostname), - escapeHtml(System.getProperty("os.name")), + escapeHtml(String.format("%s (%s)", System.getProperty("os.name"), System.getProperty("os.arch"))), escapeHtml(System.getProperty("os.version")), - Runtime.getRuntime().availableProcessors(), // java - escapeHtml(System.getProperty("java.vm.name")), - escapeHtml(System.getProperty("java.version")), + escapeHtml(System.getProperty("java.vm.vendor")+"."+System.getProperty("java.vm.name")), + escapeHtml(System.getProperty("java.vm.version")), // memory Runtime.getRuntime().totalMemory() / 1024 / 1024, Runtime.getRuntime().maxMemory() / 1024 / 1024, + Runtime.getRuntime().availableProcessors(), // version escapeHtml(MornySystem.VERSION), escapeHtml(MornySystem.getJarMd5()), From 35fa1ed5c4179f1d6a2fce8624b80a6a2c9c380c Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Thu, 19 May 2022 15:44:57 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BA=86=E4=BB=8E?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E8=AE=BE=E7=BD=AE=20token?= =?UTF-8?q?=20#34?= 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/ServerMain.java | 34 ++++++++++++++++--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/gradle.properties b/gradle.properties index 791e9a4..ad8c396 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.5 +VERSION = 0.7.0.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 ad433d0..c4542ff 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.7.0.5"; - public static final long COMPILE_TIMESTAMP = 1652944218716L; + public static final String VERSION = "0.7.0.6"; + public static final long COMPILE_TIMESTAMP = 1652946142762L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java index b7d9d78..b434bc7 100644 --- a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java +++ b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java @@ -15,10 +15,8 @@ import static cc.sukazyo.cono.morny.Log.logger; */ public class ServerMain { - private static boolean versionEchoMode = false; - private static boolean welcomeEchoMode = false; - - private static boolean showWelcome = true; + public static final String PROP_TOKEN_KEY = "TELEGRAM_BOT_API_TOKEN"; + public static final String PROP_TOKEN_MORNY_KEY = "MORNY_TG_TOKEN"; /** * 程序入口,也是参数处理器
@@ -71,6 +69,13 @@ public class ServerMain { */ public static void main (@Nonnull String[] args) { + //# + //# 启动参数设置区块 + //# + + boolean versionEchoMode = false; + boolean welcomeEchoMode = false; + boolean showWelcome = true; String key = null; String username = null; boolean outdatedBlock = false; @@ -136,6 +141,19 @@ public class ServerMain { } + String propToken = null; + String propTokenKey = null; + for (String iKey : new String[]{PROP_TOKEN_KEY, PROP_TOKEN_MORNY_KEY}) { + if (System.getenv(iKey) != null) { + propToken = System.getenv(iKey); + propTokenKey = iKey; + } + } + + //# + //# 启动相关参数的检查和处理 + //# + if (versionEchoMode) { logger.info(String.format(""" @@ -159,6 +177,14 @@ public class ServerMain { if (showWelcome) logger.info(MornyHello.MORNY_PREVIEW_IMAGE_ASCII); if (welcomeEchoMode) return; + //# + //# Coeur 参数检查和正式启动主程序 + //# + + if (propToken != null) { + key = propToken; + logger.info("Parameter set by EnvVar $"+propTokenKey); + } if (key == null) { logger.info("Parameter required has no value:\n --token."); return; From f963c76d9d0ed0a241c32e25cbb3162254210047 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 20 May 2022 19:10:50 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20daemon=20=E5=8C=85?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=90=83=E8=8D=AF=E6=8F=90=E9=86=92?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=20#35?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > merged from major/0.7-sp/medication-time - 添加 annie 的吃药提醒功能 #35 - 添加了 MornyDaemons 用于管理 morny 常驻任务 - 将 TrackerDataManager 移入 daemon 部分 - 一些 log 的更改 - 启动提示的 "System" 改为 "Coeur" - 为 tracker daemon 的启动关闭也添加了log --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cc/sukazyo/cono/morny/MornyCoeur.java | 14 +++--- .../morny/bot/event/OnActivityRecord.java | 2 +- .../cono/morny/daemon/MedicationTimer.java | 47 +++++++++++++++++++ .../cono/morny/daemon/MornyDaemons.java | 31 ++++++++++++ .../TrackerDataManager.java | 4 +- .../cono/morny/data/TelegramStickers.java | 1 + 8 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java rename src/main/java/cc/sukazyo/cono/morny/{data/tracker => daemon}/TrackerDataManager.java (97%) diff --git a/gradle.properties b/gradle.properties index ad8c396..d9dfb9e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.6 +VERSION = 0.7.0.7 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index c4542ff..f97e70e 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.7.0.6"; - public static final long COMPILE_TIMESTAMP = 1652946142762L; + public static final String VERSION = "0.7.0.7"; + public static final long COMPILE_TIMESTAMP = 1653044177443L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index 23c7539..e61cbbb 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -4,7 +4,8 @@ import cc.sukazyo.cono.morny.bot.api.OnUpdate; import cc.sukazyo.cono.morny.bot.command.MornyCommands; import cc.sukazyo.cono.morny.bot.event.EventListeners; import cc.sukazyo.cono.morny.bot.query.MornyQueries; -import cc.sukazyo.cono.morny.data.tracker.TrackerDataManager; +import cc.sukazyo.cono.morny.daemon.MornyDaemons; +import cc.sukazyo.cono.morny.daemon.TrackerDataManager; import cc.sukazyo.untitled.telegram.api.extra.ExtraAction; import com.pengrad.telegrambot.TelegramBot; @@ -133,24 +134,24 @@ public class MornyCoeur { boolean isAutomaticResetCommandList, boolean isRemoveCommandListWhenExit ) { if (INSTANCE == null) { - logger.info("System Starting"); + logger.info("Coeur Starting"); INSTANCE = new MornyCoeur( botKey, botUsername, master, trustedChat, latestEventTimestamp, isRemoveCommandListWhenExit ); - TrackerDataManager.init(); + MornyDaemons.start(); EventListeners.registerAllListeners(); INSTANCE.account.setUpdatesListener(OnUpdate::onNormalUpdate); if (isAutomaticResetCommandList) { logger.info("resetting telegram command list"); commandManager().automaticUpdateList(); } - logger.info("System start complete"); + logger.info("Coeur start complete"); return; } - logger.error("System already started coeur!!!"); + logger.error("Coeur already started!!!"); } /** @@ -166,8 +167,7 @@ public class MornyCoeur { */ private void exitCleanup () { logger.info("clean:save tracker data."); - TrackerDataManager.DAEMON.interrupt(); - TrackerDataManager.trackingLock.lock(); + MornyDaemons.stop(); if (isRemoveCommandListWhenExit) { commandManager.automaticRemoveList(); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnActivityRecord.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnActivityRecord.java index 9c3ed6e..6b26436 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnActivityRecord.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnActivityRecord.java @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.cono.morny.data.tracker.TrackerDataManager; +import cc.sukazyo.cono.morny.daemon.TrackerDataManager; import com.pengrad.telegrambot.model.Chat; import com.pengrad.telegrambot.model.Update; diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java new file mode 100644 index 0000000..f081bf2 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java @@ -0,0 +1,47 @@ +package cc.sukazyo.cono.morny.daemon; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.data.TelegramStickers; +import com.pengrad.telegrambot.request.SendSticker; + +import static cc.sukazyo.cono.morny.Log.logger; + +public class MedicationTimer extends Thread { + + public static final long NOTIFY_RECEIVE_CHAT = 5028252995L; + + MedicationTimer () { + super("TIMER_Medication"); + } + + @Override + public void run () { + logger.info("MedicationTimer started"); + while (!interrupted()) { + try { + waitToNextRoutine(); + sendNotification(); + } catch (InterruptedException e) { + interrupt(); + logger.info("MedicationTimer was interrupted, will be exit now"); + } catch (Exception e) { + logger.error("Unexpected error occurred"); + e.printStackTrace(System.out); + } + } + logger.info("MedicationTimer stopped"); + } + + private static void sendNotification () { + MornyCoeur.extra().exec(new SendSticker(NOTIFY_RECEIVE_CHAT, TelegramStickers.ID_PROGYNOVA)); + } + + private static long calcNextRoutineTimestamp () { + return ((System.currentTimeMillis()+8*60*60*1000) / (12*60*60*1000) + 1) * 12*60*60*1000 - 8*60*60*1000; + } + + private void waitToNextRoutine () throws InterruptedException { + sleep(calcNextRoutineTimestamp() - System.currentTimeMillis()); + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java new file mode 100644 index 0000000..7fe2678 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java @@ -0,0 +1,31 @@ +package cc.sukazyo.cono.morny.daemon; + +import static cc.sukazyo.cono.morny.Log.logger; + +public class MornyDaemons { + + static MedicationTimer medicationTimerInstance; + + public static void start () { + logger.info("ALL Morny Daemons starting..."); + TrackerDataManager.init(); + medicationTimerInstance = new MedicationTimer(); + medicationTimerInstance.start(); + logger.info("Morny Daemons started."); + } + + public static void stop () { + + logger.info("ALL Morny Daemons stopping..."); + + TrackerDataManager.DAEMON.interrupt(); + medicationTimerInstance.interrupt(); + + TrackerDataManager.trackingLock.lock(); + try { medicationTimerInstance.join(); } catch (InterruptedException e) { e.printStackTrace(System.out); } + + logger.info("ALL Morny Daemons STOPPED."); + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java b/src/main/java/cc/sukazyo/cono/morny/daemon/TrackerDataManager.java similarity index 97% rename from src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java rename to src/main/java/cc/sukazyo/cono/morny/daemon/TrackerDataManager.java index 7b2372f..d0f8ab0 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/TrackerDataManager.java @@ -1,4 +1,4 @@ -package cc.sukazyo.cono.morny.data.tracker; +package cc.sukazyo.cono.morny.daemon; import java.io.File; import java.io.FileOutputStream; @@ -27,6 +27,7 @@ public class TrackerDataManager { @Override public void run () { trackingLock.lock(); + logger.info("Tracker started."); long lastWaitTimestamp = System.currentTimeMillis(); boolean postProcess = false; do { @@ -48,6 +49,7 @@ public class TrackerDataManager { else logger.info("nothing to do yet"); } while (!postProcess); trackingLock.unlock(); + logger.info("Tracker exited."); } } 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 c53024c..ecf4921 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java @@ -14,5 +14,6 @@ public class TelegramStickers { 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"; + public static final String ID_PROGYNOVA = "CAACAgUAAxkBAAICm2KEuL7UQqNP7vSPCg2DHJIND6UsAAKLAwACH4WSBszIo722aQ3jJAQ"; } From 729d3fdd47cfa5f0b11e6755e230663364783b3f Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 21 May 2022 19:32:38 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E5=AE=8C=E6=88=90=20*msg=20=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20#31?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > merged from major/0.7-sp/msg-exec-shell --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cono/morny/bot/event/EventListeners.java | 2 + .../cono/morny/bot/event/OnCallMsgSend.java | 215 ++++++++++++++++++ 4 files changed, 220 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java diff --git a/gradle.properties b/gradle.properties index d9dfb9e..4280de8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.7 +VERSION = 0.7.0.8 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index f97e70e..9536859 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.7.0.7"; - public static final long COMPILE_TIMESTAMP = 1653044177443L; + public static final String VERSION = "0.7.0.8"; + public static final long COMPILE_TIMESTAMP = 1653132713941L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java index 2f62796..53d6c72 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java @@ -13,6 +13,7 @@ public class EventListeners { public static final OnEventHackHandle EVENT_HACK_HANDLE = new OnEventHackHandle(); public static final OnKuohuanhuanNeedSleep KUOHUANHUAN_NEED_SLEEP = new OnKuohuanhuanNeedSleep(); public static final OnUserRandoms USER_RANDOMS = new OnUserRandoms(); + public static final OnCallMsgSend CALL_MSG_SEND = new OnCallMsgSend(); public static void registerAllListeners () { EventListenerManager.addListener( @@ -24,6 +25,7 @@ public class EventListeners { USER_SLASH_ACTION, INLINE_QUERY, CALL_ME, + CALL_MSG_SEND, EVENT_HACK_HANDLE ); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java new file mode 100644 index 0000000..e54a443 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java @@ -0,0 +1,215 @@ +package cc.sukazyo.cono.morny.bot.event; + +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import com.pengrad.telegrambot.model.Chat; +import com.pengrad.telegrambot.model.Message; +import com.pengrad.telegrambot.model.MessageEntity; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.GetChat; +import com.pengrad.telegrambot.request.SendMessage; +import com.pengrad.telegrambot.request.SendSticker; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.cono.morny.data.TelegramStickers; +import com.pengrad.telegrambot.response.GetChatResponse; +import com.pengrad.telegrambot.response.SendResponse; + +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; + +public class OnCallMsgSend extends EventListener { + + private static final Pattern REGEX_MSG_SENDREQ_DATA_HEAD = Pattern.compile("^\\*msg([\\d-]+)(\\*\\S+)?\\n([\\s\\S]+)$"); + + private record MessageToSend ( + String message, + MessageEntity[] entities, + ParseMode parseMode, + long targetId + ) { } + + @Override + public boolean onMessage(Update update) { + + // 执行体检查 + if (update.message().chat().type() != Chat.Type.Private) return false; + if (update.message().text() == null) return false; + if (!update.message().text().startsWith("*msg")) return false; + + // 权限检查 + if (!MornyCoeur.trustedInstance().isTrusted(update.message().from().id())) { + MornyCoeur.extra().exec(new SendSticker( + update.message().chat().id(), + TelegramStickers.ID_403 + ).replyToMessageId(update.message().messageId())); + return true; + } + + Message msgsendReqRaw; // 用户书写的发送请求原文 + MessageToSend msgsendReqBody; // 解析后的发送请求实例 + + // *msgsend 发送标识 + // 处理发送要求 + if (update.message().text().equals("*msgsend")) { + // 发送体处理 + if (update.message().replyToMessage() == null) return answer404(update); + msgsendReqBody = parseRequest(update.message().replyToMessage()); + if (msgsendReqBody == null) return answer404(update); + // 执行发送任务 + SendResponse sendResponse = MornyCoeur.getAccount().execute(parseMessageToSend(msgsendReqBody)); + if (!sendResponse.isOk()) { // 发送失败 + MornyCoeur.extra().exec(new SendMessage( + update.message().chat().id(), + String.format(""" + %d FAILED + %s""", + sendResponse.errorCode(), + sendResponse.description() + ) + ).replyToMessageId(update.message().messageId()).parseMode(ParseMode.HTML)); + } + return true; + // 发送完成/失败 - 事件结束 + } + + // *msg 检查标识 + if (update.message().text().equals("*msg")) { // 处理对曾经的原文的检查 + if (update.message().replyToMessage() == null) { + return answer404(update); + } + msgsendReqRaw = update.message().replyToMessage(); + } else if (update.message().text().startsWith("*msg")) { // 对接受到的原文进行检查 + msgsendReqRaw = update.message(); + } else { + return answer404(update); // 未定义的动作 + } + + // 对发送请求的用户原文进行解析 + msgsendReqBody = parseRequest(msgsendReqRaw); + if (msgsendReqBody == null) { + return answer404(update); + } + + // 输出发送目标信息 + GetChatResponse targetChatReq = MornyCoeur.getAccount().execute(new GetChat(msgsendReqBody.targetId())); + if (!targetChatReq.isOk()) { + MornyCoeur.extra().exec(new SendMessage( + update.message().chat().id(), + String.format(""" + %d FAILED + %s""", + targetChatReq.errorCode(), + targetChatReq.description() + ) + ).replyToMessageId(update.message().messageId()).parseMode(ParseMode.HTML)); + } { + MornyCoeur.extra().exec(new SendMessage( + update.message().chat().id(), + targetChatReq.chat().type()== Chat.Type.Private ? ( + String.format(""" + %d@%s + 🔒 %s %s""", + msgsendReqBody.targetId(), + escapeHtml(targetChatReq.chat().type().name()), + escapeHtml(targetChatReq.chat().firstName()+(targetChatReq.chat().lastName()==null?"":" "+targetChatReq.chat().lastName())), + targetChatReq.chat().username()==null? + String.format("@@", targetChatReq.chat().id()): + (escapeHtml("@"+targetChatReq.chat().username())) + ) + ) : ( + String.format(""" + %d@%s::: + %s %s%s""", + msgsendReqBody.targetId(), + escapeHtml(targetChatReq.chat().type().name()), + switch (targetChatReq.chat().type()) { + case group -> "💭"; + case channel -> "📢"; + case supergroup -> "💬"; + default -> "⭕️"; + }, + escapeHtml(targetChatReq.chat().title()), + targetChatReq.chat().username() != null?String.format( + " @%s", escapeHtml(targetChatReq.chat().username()) + ):"" + ) + ) + ).replyToMessageId(update.message().messageId()).parseMode(ParseMode.HTML)); + } + // 发送文本测试 + SendResponse testSendResp = MornyCoeur.getAccount().execute( + parseMessageToSend(msgsendReqBody, update.message().chat().id()).replyToMessageId(update.message().messageId()) + ); + if (!testSendResp.isOk()) { + MornyCoeur.extra().exec(new SendMessage( + update.message().chat().id(), + String.format(""" + %d FAILED + %s""", + testSendResp.errorCode(), + testSendResp.description() + ) + ).replyToMessageId(update.message().messageId()).parseMode(ParseMode.HTML)); + } + + return true; + + } + + @Nullable + private static MessageToSend parseRequest (@Nonnull Message requestBody) { + + final Matcher matcher = REGEX_MSG_SENDREQ_DATA_HEAD.matcher(requestBody.text()); + if (matcher.matches()) { + long targetId = Long.parseLong(matcher.group(1)); + ParseMode parseMode = matcher.group(2) == null ? null : switch (matcher.group(2)) { + case "*markdown", "*md", "*m↓" -> ParseMode.MarkdownV2; + case "*md1" -> ParseMode.Markdown; + case "*html" -> ParseMode.HTML; + default -> null; + }; + final int offset = "*msg".length()+matcher.group(1).length()+(matcher.group(2)==null?0:matcher.group(2).length())+1; + final ArrayList entities = new ArrayList<>(); + if (requestBody.entities() != null) for (MessageEntity entity : requestBody.entities()) { + final MessageEntity parsed = new MessageEntity(entity.type(), entity.offset() - offset, entity.length()); + if (entity.url() != null) parsed.url(entity.url()); + if (entity.user() != null) parsed.user(entity.user()); + if (entity.language() != null) parsed.language(entity.language()); + entities.add(parsed); + } + return new MessageToSend(matcher.group(3), entities.toArray(MessageEntity[]::new), parseMode, targetId); + } + + return null; + + } + + @Nonnull + private static SendMessage parseMessageToSend (@Nonnull MessageToSend body) { + return parseMessageToSend(body, body.targetId); + } + + @Nonnull + private static SendMessage parseMessageToSend (@Nonnull MessageToSend body, long targetId) { + SendMessage sendingBody = new SendMessage(targetId, body.message); + if (body.entities != null) sendingBody.entities(body.entities); + if (body.parseMode != null) sendingBody.parseMode(body.parseMode); + return sendingBody; + } + + private static boolean answer404 (@Nonnull Update update) { + MornyCoeur.extra().exec(new SendSticker( + update.message().chat().id(), + TelegramStickers.ID_404 + ).replyToMessageId(update.message().messageId())); + return true; + } + +} From ce83e3284d6f77a0e1a21ec0f1e7631272d68a30 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 22 May 2022 14:07:39 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E4=B8=BA=20*msgsend=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=8F=91=E9=80=81=E6=88=90=E5=8A=9F=E4=BF=A1=E5=8F=B7=E5=9B=9E?= =?UTF-8?q?=E6=98=BE=EF=BC=8C=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=B8=AA=E5=88=86?= =?UTF-8?q?=E6=94=AF=E6=95=85=E9=9A=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cc/sukazyo/cono/morny/GradleProjectConfigures.java | 4 ++-- .../cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java | 9 +++++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/gradle.properties b/gradle.properties index 4280de8..2e99ea0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.8 +VERSION = 0.7.0.9 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 9536859..3bee94b 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.7.0.8"; - public static final long COMPILE_TIMESTAMP = 1653132713941L; + public static final String VERSION = "0.7.0.9"; + public static final long COMPILE_TIMESTAMP = 1653199608824L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java index e54a443..b5d7a34 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java @@ -74,6 +74,11 @@ public class OnCallMsgSend extends EventListener { sendResponse.description() ) ).replyToMessageId(update.message().messageId()).parseMode(ParseMode.HTML)); + } else { // 发送成功信号 + MornyCoeur.extra().exec(new SendSticker( + update.message().chat().id(), + TelegramStickers.ID_SENT + ).replyToMessageId(update.message().messageId())); } return true; // 发送完成/失败 - 事件结束 @@ -109,10 +114,10 @@ public class OnCallMsgSend extends EventListener { targetChatReq.description() ) ).replyToMessageId(update.message().messageId()).parseMode(ParseMode.HTML)); - } { + } else { MornyCoeur.extra().exec(new SendMessage( update.message().chat().id(), - targetChatReq.chat().type()== Chat.Type.Private ? ( + targetChatReq.chat().type() == Chat.Type.Private ? ( String.format(""" %d@%s 🔒 %s %s""", From 9d0a1efc5396b8f21ba2feb8d2fc5276a4d347c2 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 22 May 2022 14:19:56 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=A3=B0=E6=98=8E=E8=A7=84=E8=8C=83=E7=A7=BB=E9=99=A4"/"?= =?UTF-8?q?=E5=89=8D=E7=BC=80=EF=BC=8C=E4=BF=AE=E5=A4=8D=E9=9D=9E=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E5=8D=B4=E7=94=B1=E4=BA=8E"@"=E7=AC=A6=E5=8F=B7?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E4=BA=8B=E4=BB=B6=E8=A2=AB=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= 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/command/DirectMsgClear.java | 2 +- .../cono/morny/bot/command/EventHack.java | 2 +- .../cono/morny/bot/command/GetUsernameAndId.java | 2 +- .../cono/morny/bot/command/Ip186Query.java | 11 +++++++---- .../cono/morny/bot/command/MornyCommands.java | 16 ++++++++-------- .../sukazyo/cono/morny/bot/command/Nbnhhsh.java | 2 +- .../cc/sukazyo/cono/morny/bot/command/喵呜.java | 8 ++++---- .../sukazyo/cono/morny/bot/command/私わね.java | 2 +- .../cono/morny/bot/event/OnTelegramCommand.java | 6 +++--- 11 files changed, 30 insertions(+), 27 deletions(-) diff --git a/gradle.properties b/gradle.properties index 2e99ea0..d8b400a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.9 +VERSION = 0.7.0.10 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 3bee94b..f42d08b 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.7.0.9"; - public static final long COMPILE_TIMESTAMP = 1653199608824L; + public static final String VERSION = "0.7.0.10"; + public static final long COMPILE_TIMESTAMP = 1653200212040L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java index 7e3cc82..7c66a8f 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java @@ -14,7 +14,7 @@ import static cc.sukazyo.cono.morny.Log.logger; public class DirectMsgClear implements ISimpleCommand { - @Nonnull @Override public String getName () { return "/r"; } + @Nonnull @Override public String getName () { return "r"; } @Nullable @Override public String[] getAliases () { return new String[0]; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/EventHack.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/EventHack.java index b43df8d..18edc7b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/EventHack.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/EventHack.java @@ -18,7 +18,7 @@ import javax.annotation.Nullable; */ public class EventHack implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/event_hack"; } + @Nonnull @Override public String getName () { return "event_hack"; } @Nullable @Override public String[] getAliases () { return null; } @Nonnull @Override public String getParamRule () { return "[(user|group|any)]"; } @Nonnull @Override public String getDescription () { return "输出 bot 下一个获取到的事件序列化数据"; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java index f7d136f..da67bb7 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java @@ -15,7 +15,7 @@ import javax.annotation.Nullable; public class GetUsernameAndId implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/user"; } + @Nonnull @Override public String getName () { return "user"; } @Nullable @Override public String[] getAliases () { return null; } @Nonnull @Override public String getParamRule () { return "[userid]"; } @Nonnull @Override public String getDescription () { return "获取指定或回复的用户相关信息"; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java index 7a47390..1390096 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java @@ -20,8 +20,11 @@ import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; */ public class Ip186Query { + public static final String CMD_IP = "ip"; + public static final String CMD_WHOIS = "whois"; + public static class Ip implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/ip"; } + @Nonnull @Override public String getName () { return CMD_IP; } @Nullable @Override public String[] getAliases () { return new String[0]; } @Nonnull @Override public String getParamRule () { return "[ip]"; } @Nonnull @Override public String getDescription () { return "通过 https://ip.186526.xyz 查询 ip 资料"; } @@ -29,7 +32,7 @@ public class Ip186Query { } public static class Whois implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/whois"; } + @Nonnull @Override public String getName () { return CMD_WHOIS; } @Nullable @Override public String[] getAliases () { return new String[0]; } @Nonnull @Override public String getParamRule () { return "[domain]"; } @Nonnull @Override public String getDescription () { return "通过 https://ip.186526.xyz 查询域名资料"; } @@ -62,8 +65,8 @@ public class Ip186Query { try { IP186QueryResponse response = switch (command.getCommand()) { - case "/ip" -> IP186QueryHandler.queryIp(arg); - case "/whois" -> IP186QueryHandler.queryWhoisPretty(arg); + case CMD_IP -> IP186QueryHandler.queryIp(arg); + case CMD_WHOIS -> IP186QueryHandler.queryWhoisPretty(arg); default -> throw new IllegalArgumentException("Unknown 186-IP query method " + command.getCommand()); }; MornyCoeur.extra().exec(new SendMessage( diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java index 7c72538..9e2efe6 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java @@ -159,7 +159,7 @@ public class MornyCommands { /// private static class ON implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/o"; } + @Nonnull @Override public String getName () { return "o"; } @Nullable @Override public String[] getAliases () { return null; } @Nonnull @Override public String getParamRule () { return ""; } @@ -175,8 +175,8 @@ public class MornyCommands { } private static class Hello implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/hello"; } - @Nullable @Override public String[] getAliases () { return new String[]{"/hi"}; } + @Nonnull @Override public String getName () { return "hello"; } + @Nullable @Override public String[] getAliases () { return new String[]{"hi"}; } @Nonnull @Override public String getParamRule () { return ""; } @Nonnull @Override public String getDescription () { return "打招呼"; } @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { onCommandHelloExec(event); } @@ -191,7 +191,7 @@ public class MornyCommands { } private static class Exit implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/exit"; } + @Nonnull @Override public String getName () { return "exit"; } @Nullable @Override public String[] getAliases () { return new String[0]; } @Nonnull @Override public String getParamRule () { return ""; } @Nonnull @Override public String getDescription () { return "关闭 Bot (仅可信成员)"; } @@ -217,7 +217,7 @@ public class MornyCommands { } private static class Version implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/version"; } + @Nonnull @Override public String getName () { return "version"; } @Nullable @Override public String[] getAliases () { return null; } @Nonnull @Override public String getParamRule () { return ""; } @Nonnull @Override public String getDescription () { return "检查 Bot 版本信息"; } @@ -244,7 +244,7 @@ public class MornyCommands { } private static class MornyRuntime implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/runtime"; } + @Nonnull @Override public String getName () { return "runtime"; } @Nullable @Override public String[] getAliases () { return null; } @Nonnull @Override public String getParamRule () { return ""; } @Nonnull @Override public String getDescription () { return "获取 Bot 运行时信息(包括版本号)"; } @@ -309,7 +309,7 @@ public class MornyCommands { } private static class Jrrp implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/jrrp"; } + @Nonnull @Override public String getName () { return "jrrp"; } @Nullable @Override public String[] getAliases () { return null; } @Nonnull @Override public String getParamRule () { return ""; } @Nonnull @Override public String getDescription () { return "获取 (假的) jrrp"; } @@ -329,7 +329,7 @@ public class MornyCommands { } private static class SaveData implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/save"; } + @Nonnull @Override public String getName () { return "save"; } @Nullable @Override public String[] getAliases () { return null; } @Nonnull @Override public String getParamRule () { return ""; } @Nonnull @Override public String getDescription () { return "保存缓存数据到文件(仅可信成员)"; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.java index b72127d..fced75b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.java @@ -16,7 +16,7 @@ import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; public class Nbnhhsh implements ITelegramCommand { - @Nonnull @Override public String getName () { return "/nbnhhsh"; } + @Nonnull @Override public String getName () { return "nbnhhsh"; } @Nullable @Override public String[] getAliases () { return null; } @Nonnull @Override public String getParamRule () { return "[text]"; } @Nonnull @Override public String getDescription () { return "检索文本内 nbnhhsh 词条"; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/喵呜.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/喵呜.java index ca31006..67d644a 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/喵呜.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/喵呜.java @@ -13,7 +13,7 @@ import javax.annotation.Nullable; public class 喵呜 { public static class 抱抱 implements ISimpleCommand { - @Nonnull @Override public String getName () { return "/抱抱"; } + @Nonnull @Override public String getName () { return "抱抱"; } @Nullable @Override public String[] getAliases () { return new String[0]; } @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { MornyCoeur.extra().exec(new SendMessage( @@ -24,7 +24,7 @@ public class 喵呜 { } public static class 揉揉 implements ISimpleCommand { - @Nonnull @Override public String getName () { return "/揉揉"; } + @Nonnull @Override public String getName () { return "揉揉"; } @Nullable @Override public String[] getAliases () { return new String[0]; } @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { MornyCoeur.extra().exec(new SendMessage( @@ -35,7 +35,7 @@ public class 喵呜 { } public static class 蹭蹭 implements ISimpleCommand { - @Nonnull @Override public String getName () { return "/蹭蹭"; } + @Nonnull @Override public String getName () { return "蹭蹭"; } @Nullable @Override public String[] getAliases () { return new String[0]; } @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { MornyCoeur.extra().exec(new SendMessage( @@ -46,7 +46,7 @@ public class 喵呜 { } public static class 贴贴 implements ISimpleCommand { - @Nonnull @Override public String getName () { return "/贴贴"; } + @Nonnull @Override public String getName () { return "贴贴"; } @Nullable @Override public String[] getAliases () { return new String[0]; } @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { MornyCoeur.extra().exec(new SendMessage( diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java index e06f66a..a8ad800 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java @@ -13,7 +13,7 @@ import java.util.concurrent.ThreadLocalRandom; public class 私わね implements ISimpleCommand { @Nonnull - @Override public String getName () { return "/me"; } + @Override public String getName () { return "me"; } @Nullable @Override public String[] getAliases () { return null; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnTelegramCommand.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnTelegramCommand.java index e0f2419..0c632ab 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnTelegramCommand.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnTelegramCommand.java @@ -12,10 +12,10 @@ public class OnTelegramCommand extends EventListener { @Override public boolean onMessage (@Nonnull Update event) { - if (event.message().text() == null) { - return false; // 检测到无消息文本,忽略掉命令处理 + if (event.message().text() == null || !event.message().text().startsWith("/")) { + return false; // 检测到非(命令格式)文本,忽略掉命令处理 } - final InputCommand command = new InputCommand(event.message().text()); + final InputCommand command = new InputCommand(event.message().text().substring(1)); if (command.getTarget() != null && !MornyCoeur.getUsername().equals(command.getTarget())) { return true; // 检测到命令并非针对 morny,退出整个事件处理链 }