From 82c9faea1edbd457df34b766c31dc50bcb865d6e Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 16 Mar 2022 02:43:47 +0800 Subject: [PATCH 01/19] =?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/19] =?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/19] =?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/19] =?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/19] =?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/19] =?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 ecd4902dbe88da0cacfb04f7dab90760d0e7a68f Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 17 May 2022 14:26:46 +0800 Subject: [PATCH 07/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=E7=9A=84=20trusted-readers-of-dinner=20=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=98=BE=E7=A4=BA=E5=92=8C=20version=20=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=98=BE=E7=A4=BA?= 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 ++-- src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java | 7 +++++-- src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java | 6 +++++- src/main/java/cc/sukazyo/cono/morny/ServerMain.java | 6 ++++++ 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/gradle.properties b/gradle.properties index 6094b3a..8d5c313 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.6.4.0 +VERSION = 0.6.5.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 edcbadd..db94338 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.4.0"; - public static final long COMPILE_TIMESTAMP = 1652197269644L; + public static final String VERSION = "0.6.5.0"; + public static final long COMPILE_TIMESTAMP = 1652768698515L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index 90ae3e1..84101b9 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -94,13 +94,16 @@ public class MornyCoeur { this.account = loginResult.account; this.username = loginResult.username; this.trusted = new MornyTrusted(master, trustedChat, trustedRDinner); + StringBuilder trustedReadersDinnerIds = new StringBuilder(); + trusted.getTrustedReadersOfDinnerSet().forEach(id -> trustedReadersDinnerIds.append("\n ").append(id)); logger.info(String.format(""" trusted param set: - master (id) %d - trusted chat (id) - %d""", - master, trustedChat + %d + - trusted reader-of-dinner (id)%s""", + master, trustedChat, trustedReadersDinnerIds )); } catch (Exception e) { diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java b/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java index 1c7009a..ba93789 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java @@ -21,7 +21,7 @@ public class MornyTrusted { */ public final long MASTER; - public final Set TRUSTED_READERS_OF_DINNER; + private final Set TRUSTED_READERS_OF_DINNER; public MornyTrusted (long master, long trustedChatId, Set trustedRDinner) { this.TRUSTED_CHAT_ID = trustedChatId; @@ -51,4 +51,8 @@ public class MornyTrusted { return TRUSTED_READERS_OF_DINNER.contains(userId); } + public Set getTrustedReadersOfDinnerSet () { + return Set.copyOf(TRUSTED_READERS_OF_DINNER); + } + } diff --git a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java index f809434..a0fa6ea 100644 --- a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java +++ b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java @@ -168,6 +168,12 @@ public class ServerMain { if (showWelcome) logger.info(MornyHello.MORNY_PREVIEW_IMAGE_ASCII); if (welcomeEchoMode) return; + logger.info(String.format(""" + ServerMain.java Loaded >>> + - version %s(%s)(%d)""", + MornySystem.VERSION, MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP + )); + if (key == null) { logger.info("Parameter required has no value:\n --token."); return; From 9bfa35710fe01b9fcb476c400c074ada36ccf933 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 17 May 2022 15:08:56 +0800 Subject: [PATCH 08/19] =?UTF-8?q?=E4=B8=BA=E5=90=AF=E5=8A=A8=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=A4=A7=E9=87=8F=20aliase?= =?UTF-8?q?s?= 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 | 23 +++++++++++++------ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/gradle.properties b/gradle.properties index 8d5c313..884ff8e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.6.5.0 +VERSION = 0.6.5.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 db94338..efee5b7 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.5.0"; - public static final long COMPILE_TIMESTAMP = 1652768698515L; + public static final String VERSION = "0.6.5.1"; + public static final long COMPILE_TIMESTAMP = 1652771241057L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java index a0fa6ea..518c9b4 100644 --- a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java +++ b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java @@ -53,6 +53,9 @@ public class ServerMain { * 赋值为程序启动的时间,从而造成阻挡程序启动之前的消息事件处理效果。 * *
  • + * {@code --auto-cmd} (下面两个)选项 {@code --auto-cmd-list} 和 {@code --auto-cmd-remove} 的合并版本 + *
  • + *
  • * {@code --auto-cmd-list} 使 morny 在启动时自动依据程序本体更新登录 bot 的命令列表 *
  • *
  • @@ -92,11 +95,11 @@ public class ServerMain { outdatedBlock = true; continue; } - case "--no-hello", "-hf" -> { + case "--no-hello", "-hf", "--quiet", "-q" -> { showWelcome = false; continue; } - case "--only-hello", "-o" -> { + case "--only-hello", "-ho", "-o", "-hi" -> { welcomeEchoMode = true; continue; } @@ -104,31 +107,37 @@ public class ServerMain { versionEchoMode = true; continue; } - case "--token" -> { + case "--token", "-t" -> { i++; key = args[i]; continue; } - case "--username" -> { + case "--username", "-u" -> { i++; username = args[i]; continue; } - case "--master" -> { + case "--master", "-mm" -> { i++; master = Long.parseLong(args[i]); continue; } - case "--trusted-chat" -> { + case "--trusted-chat", "-trs" -> { i++; trustedChat = Long.parseLong(args[i]); continue; } - case "--trusted-reader-dinner" -> { + //noinspection SpellCheckingInspection + case "--trusted-reader-dinner", "-trsd" -> { i++; trustedReadersOfDinner.add(Long.parseLong(args[i])); continue; } + case "--auto-cmd", "-cmd", "-c" -> { + autoCmdList = true; + autoCmdRemove = true; + continue; + } case "--auto-cmd-list", "-ca" -> { autoCmdList = true; continue; From 16d74a3af56a8acf741baca29604b9dad00cb77b Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Thu, 19 May 2022 15:14:18 +0800 Subject: [PATCH 09/19] =?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 10/19] =?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 11/19] =?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 12/19] =?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 13/19] =?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 14/19] =?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,退出整个事件处理链 } From 88cc868c90ac9270933a9e55c115ebceb212aa4c Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 25 May 2022 10:34:00 +0800 Subject: [PATCH 15/19] =?UTF-8?q?=E5=B0=8F=E6=94=B9=20TGUserInfo=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=20display=20name=20=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 first name 和 last name 合并为 display name 栏目 --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +-- .../morny/util/TelegramUserInformation.java | 30 +++++-------------- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/gradle.properties b/gradle.properties index b211493..097003d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.11 +VERSION = 0.7.0.12 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index b304e90..c9bbcf6 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.11"; - public static final long COMPILE_TIMESTAMP = 1653443614307L; + public static final String VERSION = "0.7.0.12"; + public static final long COMPILE_TIMESTAMP = 1653445911339L; } 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 4d0126d..efa3257 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java @@ -60,28 +60,14 @@ public class TelegramUserInformation { if (dataCenter == null) { userInformation.append("\ndatacenter : null"); } else { userInformation.append(String.format("\ndatacenter : %s", escapeHtml(dataCenter))); } } - if (user.firstName() == null) { - userInformation.append("\nfirstname : null"); - } else { - userInformation.append(String.format( - """ - - firstname : - - %s""", - escapeHtml(user.firstName()) - )); - } - if (user.lastName() == null) { - userInformation.append("\nlastname : null"); - } else { - userInformation.append(String.format( - """ - - lastname : - - %s""", - escapeHtml(user.lastName()) - )); - } + userInformation.append(String.format( + """ + + display name : + - %s%s""", + escapeHtml(user.firstName()), + user.lastName()==null ? "" : String.format("\n- %s", escapeHtml(user.lastName())) + )); if (user.languageCode() != null) { userInformation.append(String.format( """ From c4bdf2846bc853bce263f0b1969de2fcdbefe175 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 28 May 2022 21:36:36 +0800 Subject: [PATCH 16/19] =?UTF-8?q?=E5=85=B3=E9=97=AD=20=E5=AF=B9DP7?= =?UTF-8?q?=E5=85=B3=E9=97=ADSlashAction=20=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E5=85=B3=E9=97=AD=20OnKuohuanhuanNeedSleep=20=E7=B3=BB?= =?UTF-8?q?=E5=88=97=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 ++-- .../cono/morny/bot/event/EventListeners.java | 4 ++-- .../morny/bot/event/OnUserSlashAction.java | 23 +++++++++---------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/gradle.properties b/gradle.properties index 097003d..435c0cb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.12 +VERSION = 0.7.0.13 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index c9bbcf6..ce9ca67 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.12"; - public static final long COMPILE_TIMESTAMP = 1653445911339L; + public static final String VERSION = "0.7.0.13"; + public static final long COMPILE_TIMESTAMP = 1653744934038L; } 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 53d6c72..c50e6b2 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 @@ -11,7 +11,7 @@ public class EventListeners { public static final OnInlineQueries INLINE_QUERY = new OnInlineQueries(); 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(); + @SuppressWarnings("unused") 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(); @@ -19,7 +19,7 @@ public class EventListeners { EventListenerManager.addListener( ACTIVITY_RECORDER, UPDATE_TIMESTAMP_OFFSET_LOCK, - KUOHUANHUAN_NEED_SLEEP, +// KUOHUANHUAN_NEED_SLEEP, COMMANDS_LISTENER, USER_RANDOMS, USER_SLASH_ACTION, 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 b624a23..960ff17 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 @@ -3,7 +3,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.util.tgapi.TGToStringFromMessage; -import cc.sukazyo.untitled.telegram.api.formatting.TGToString; import cc.sukazyo.untitled.util.command.CommonCommand; import cc.sukazyo.untitled.util.string.StringArrays; @@ -14,7 +13,6 @@ 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 { @@ -24,20 +22,21 @@ public class OnUserSlashAction extends EventListener { final String text = event.message().text(); if (text == null) return false; - if (text.startsWith("/")) { + 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; - } +//{ 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; +// } final String[] action = CommonCommand.format(text); action[0] = action[0].substring(1); From 07d864fb5d48dc31ad2f5cb5b07adf3bd0161054 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 30 May 2022 23:34:49 +0800 Subject: [PATCH 17/19] =?UTF-8?q?=E4=B8=BA=20morny=20system=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=20CODENAME=20=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 并添加支持 /runtime /version 命令显示 - 并添加支持 --version 选项显示支持 - 并添加支持启动的版本报告显示支持 --- build.gradle | 3 +++ gradle.properties | 4 +++- .../cc/sukazyo/cono/morny/GradleProjectConfigures.java | 5 +++-- src/main/java/cc/sukazyo/cono/morny/MornySystem.java | 10 ++++++++++ src/main/java/cc/sukazyo/cono/morny/ServerMain.java | 9 +++++---- .../sukazyo/cono/morny/bot/command/MornyCommands.java | 5 ++++- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index 1720565..35dfc91 100644 --- a/build.gradle +++ b/build.gradle @@ -43,6 +43,9 @@ task updateVersionCode { ant.replaceregexp(match:'VERSION = ["a-zA-Z0-9.\\-_+@]+;', replace:"VERSION = \"$project.version\";", flags:'g', byline:true) { fileset(dir: 'src/main/java/cc/sukazyo/cono/morny', includes: 'GradleProjectConfigures.java') } + ant.replaceregexp(match:'CODENAME = ["a-zA-Z0-9]+;', replace:"CODENAME = \"${CODENAME}\";", flags:'g', byline:true) { + fileset(dir: 'src/main/java/cc/sukazyo/cono/morny', includes: 'GradleProjectConfigures.java') + } ant.replaceregexp(match:'COMPILE_TIMESTAMP = [0-9]+L;', replace:"COMPILE_TIMESTAMP = ${System.currentTimeMillis()}L;", flags:'g', byline:true) { fileset(dir: 'src/main/java/cc/sukazyo/cono/morny', includes: 'GradleProjectConfigures.java') } diff --git a/gradle.properties b/gradle.properties index 435c0cb..1b57c05 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,8 @@ ## Core -VERSION = 0.7.0.13 +VERSION = 0.7.0.14 + +CODENAME = fuzhou # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index ce9ca67..48e6fba 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,7 @@ 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.13"; - public static final long COMPILE_TIMESTAMP = 1653744934038L; + public static final String VERSION = "0.7.0.14"; + public static final String CODENAME = "fuzhou"; + public static final long COMPILE_TIMESTAMP = 1653924732379L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornySystem.java b/src/main/java/cc/sukazyo/cono/morny/MornySystem.java index 4dbca43..e9a486d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornySystem.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornySystem.java @@ -18,6 +18,16 @@ public class MornySystem { */ public static final String VERSION = GradleProjectConfigures.VERSION; + /** + * Morny Coeur 当前的版本代号.
    + * 一个单个单词,一般作为一个大版本的名称,只在重大更新改变
    + * 格式保持为仅由小写字母和数字组成
    + * 有时也可能是复合词或特殊的词句
    + *
    + * 会由 gradle 任务 {@code updateVersionCode} 更新 + */ + public static final String CODENAME = GradleProjectConfigures.CODENAME; + /** * 获取程序 jar 文件的 md5-hash 值
    *
    diff --git a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java index bec90e2..eb1ef08 100644 --- a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java +++ b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java @@ -177,13 +177,13 @@ public class ServerMain { logger.info(String.format(""" Morny Cono Version - version : - %s + %s %s - md5hash : %s - co.time : %d %s [UTC]""", - MornySystem.VERSION, + MornySystem.VERSION, MornySystem.CODENAME.toUpperCase(), MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP, CommonFormatUtils.formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0) @@ -197,8 +197,9 @@ public class ServerMain { logger.info(String.format(""" ServerMain.java Loaded >>> - - version %s(%s)(%d)""", - MornySystem.VERSION, MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP + - version %s %s (%s)(%d)""", + MornySystem.VERSION, MornySystem.CODENAME.toUpperCase(), + MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP )); //# 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 f61c7f6..1975177 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 @@ -229,12 +229,14 @@ public class MornyCommands { String.format( """ version: + - Morny %s - %s core md5_hash: - %s compile timestamp: - %d - %s [UTC]""", + escapeHtml(MornySystem.CODENAME.toUpperCase()), escapeHtml(MornySystem.VERSION), escapeHtml(MornySystem.getJarMd5()), GradleProjectConfigures.COMPILE_TIMESTAMP, @@ -274,7 +276,7 @@ public class MornyCommands { - %d / %d MB - %d cores coeur version: - - %s + - %s (%s) - %s - %s [UTC] - [%d] @@ -296,6 +298,7 @@ public class MornyCommands { Runtime.getRuntime().availableProcessors(), // version escapeHtml(MornySystem.VERSION), + escapeHtml(MornySystem.CODENAME), escapeHtml(MornySystem.getJarMd5()), escapeHtml(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)), GradleProjectConfigures.COMPILE_TIMESTAMP, From b35b0600dc6112cae66b83ed7df7df853589a18a Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 31 May 2022 11:41:02 +0800 Subject: [PATCH 18/19] =?UTF-8?q?=E5=90=83=E8=8D=AF=E6=8F=90=E9=86=92?= =?UTF-8?q?=E6=B6=88=E6=81=AF=20pin=20=E6=94=AF=E6=8C=81=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../java/cc/sukazyo/cono/morny/GradleProjectConfigures.java | 4 ++-- .../java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java | 5 ++++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/gradle.properties b/gradle.properties index 1b57c05..ab4a9cd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.14 +VERSION = 0.7.0.15 CODENAME = fuzhou diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 48e6fba..f471d62 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,7 +4,7 @@ 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.14"; + public static final String VERSION = "0.7.0.15"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1653924732379L; + public static final long COMPILE_TIMESTAMP = 1653968380239L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java index f081bf2..36d464a 100644 --- a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java @@ -2,7 +2,9 @@ package cc.sukazyo.cono.morny.daemon; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.data.TelegramStickers; +import com.pengrad.telegrambot.request.PinChatMessage; import com.pengrad.telegrambot.request.SendSticker; +import com.pengrad.telegrambot.response.SendResponse; import static cc.sukazyo.cono.morny.Log.logger; @@ -33,7 +35,8 @@ public class MedicationTimer extends Thread { } private static void sendNotification () { - MornyCoeur.extra().exec(new SendSticker(NOTIFY_RECEIVE_CHAT, TelegramStickers.ID_PROGYNOVA)); + SendResponse m = MornyCoeur.extra().exec(new SendSticker(NOTIFY_RECEIVE_CHAT, TelegramStickers.ID_PROGYNOVA)); + if (m.isOk()) MornyCoeur.extra().exec(new PinChatMessage(NOTIFY_RECEIVE_CHAT, m.message().messageId())); } private static long calcNextRoutineTimestamp () { From 538167dfa9cc6a1d81dd691396b0f2ab7edd15ef Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 1 Jun 2022 12:40:28 +0800 Subject: [PATCH 19/19] =?UTF-8?q?=E4=B8=BA=20local=20bot=20api=20server=20?= =?UTF-8?q?=E8=AE=BE=E5=AE=9A=E6=8F=90=E4=BE=9B=E6=94=AF=E6=8C=81=20#33?= 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 | 40 +++++++++++++++++-- .../cc/sukazyo/cono/morny/ServerMain.java | 40 ++++++++++++++----- 4 files changed, 70 insertions(+), 16 deletions(-) diff --git a/gradle.properties b/gradle.properties index ab4a9cd..5a613bb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.15 +VERSION = 0.7.0.16 CODENAME = fuzhou diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index f471d62..da03946 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,7 +4,7 @@ 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.15"; + public static final String VERSION = "0.7.0.16"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1653968380239L; + public static final long COMPILE_TIMESTAMP = 1654058419036L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index f010985..ac5a6dd 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -8,6 +8,7 @@ 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; +import com.pengrad.telegrambot.impl.FileApi; import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.request.GetMe; @@ -82,6 +83,7 @@ public class MornyCoeur { * 单位为毫秒 */ private MornyCoeur ( + @Nullable String botApi, @Nullable String botApi4File, @Nonnull String botKey, @Nullable String botUsername, long master, long trustedChat, Set trustedRDinner, long latestEventTimestamp, @@ -98,7 +100,7 @@ public class MornyCoeur { } try { - final LogInResult loginResult = login(botKey, botUsername); + final LogInResult loginResult = login(botApi, botApi4File, botKey, botUsername); this.account = loginResult.account; this.username = loginResult.username; this.userid = loginResult.userid; @@ -135,6 +137,7 @@ public class MornyCoeur { * @see #MornyCoeur 程序初始化方法 */ public static void main ( + @Nullable String botApi, @Nullable String botApi4File, @Nonnull String botKey, @Nullable String botUsername, long master, long trustedChat, Set trustedRDinner, long latestEventTimestamp, boolean isAutomaticResetCommandList, boolean isRemoveCommandListWhenExit @@ -142,6 +145,7 @@ public class MornyCoeur { if (INSTANCE == null) { logger.info("Coeur Starting"); INSTANCE = new MornyCoeur( + botApi, botApi4File, botKey, botUsername, master, trustedChat, trustedRDinner, latestEventTimestamp, @@ -193,12 +197,42 @@ public class MornyCoeur { * 会通过 GetMe 动作验证是否连接上了 telegram api 服务器, * 同时也要求登录获得的 username 和 {@link #username} 声明值相等 * + * @param api bot client 将会连接到的 telegram bot api 位置 + * @param api4File bot client 将会连接到的 telegram file api 位置,如果不指定则会跟随 {@code api} 选项的设定 * @param key bot 的 api-token + * @param requireName 要求登录到的需要的 username,如果登陆后的 username 与此不同则会报错退出 * @return 成功登录后的 {@link TelegramBot} 对象 */ @Nonnull - private static LogInResult login (@Nonnull String key, @Nullable String requireName) { - final TelegramBot account = new TelegramBot(key); + private static LogInResult login ( + @Nullable String api, @Nullable String api4File, + @Nonnull String key, @Nullable String requireName + ) { + final TelegramBot.Builder accountConfig = new TelegramBot.Builder(key); + boolean isCustomApi = false; + String apiUrlSet = "https://api.telegram.org/bot"; + String api4FileUrlSet = FileApi.FILE_API; + if (api != null) { + api = api.endsWith("/") ? api.substring(0, api.length() - 1) : api; + accountConfig.apiUrl(apiUrlSet = api.endsWith("/bot")? api : api + "/bot"); + isCustomApi = true; + } + if (api4File != null) { + api4File = api4File.endsWith("/") ? api4File : api4File + "/"; + accountConfig.fileApiUrl(api4FileUrlSet = api4File.endsWith("/file/bot")? api4File : api4File + "/file/bot"); + isCustomApi = true; + } else if (api != null && !api.endsWith("/bot")) { + accountConfig.fileApiUrl(api4FileUrlSet = api + "/file/bot"); + } + if (isCustomApi) { + logger.info(String.format(""" + Telegram Bot API set to : + - %s + - %s""", + apiUrlSet, api4FileUrlSet + )); + } + final TelegramBot account = accountConfig.build(); logger.info("Trying to login..."); for (int i = 1; i < 4; i++) { if (i != 1) logger.info("retrying..."); diff --git a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java index eb1ef08..d7ec325 100644 --- a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java +++ b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java @@ -43,6 +43,14 @@ public class ServerMain { * {@code --username} {@link MornyCoeur#getUsername() bot 的 username} 预定义 *
  • *
  • + * {@code --api} 设定 {@link MornyCoeur#getAccount() bot client} 使用的 telegram bot api server。 + * 需要注意的是如果带有后缀 {@code /bot} 则会单独设定 api server + * 而不会适应性的同时为 {@code --api-files} 设定值。 + *
  • + *
  • + * {@code --api-files} 单独设定 {@link MornyCoeur#getAccount() bot client} 使用的 telegram bot file api server + *
  • + *
  • * {@code --no-hello} 不在主程序启动时输出用于欢迎消息的字符画。 * 与 {@code --only-hello} 参数不兼容 —— 会导致程序完全没有任何输出 *
  • @@ -60,14 +68,11 @@ public class ServerMain { * {@code --auto-cmd-remove} 使 morny 在关闭时自动依据程序本体删除 bot 的命令列表 * * - * 除去选项之外,第一个参数会被赋值为 bot 的 telegram bot api token, - * 第二个参数会被赋值为 bot 的 username 限定名。其余的参数会被认定为无法理解。
    - *
    + * 除去选项之外,第一个参数会被赋值为 bot 的 telegram bot api token, + * 第二个参数会被赋值为 bot 的 username 限定名。其余的参数会被认定为无法理解。
    * 自 {@code 0.4.2.3},token 和 username 的赋值已被选项组支持
    - * 使用参数所进行取值的 token 和 username 已被转移至 {@code --token} 和 {@code --username} 参数, - * 或许,直接参数赋值的支持将计划在 {@code 0.4.3} 标记废弃并在 {@code 0.5} 删除。 - * 但实际上这并不影响现在的使用,选项赋值目前仍属于测试功能
    - * 但请勿混用,这将使两个赋值出现混淆并产生不可知的结果 + * 自 {@code 0.5.0.4},旧的直接通过参数为 bot token & username 赋值的方式已被删除 + * 使用参数所进行取值的 token 和 username 已被转移至 {@code --token} 和 {@code --username} 参数
    * * @see MornyCoeur#main * @since 0.4.0.0 @@ -90,6 +95,8 @@ public class ServerMain { long trustedChat = -1001541451710L; boolean autoCmdList = false; boolean autoCmdRemove = false; + String api = null; + String api4File = null; for (int i = 0; i < args.length; i++) { @@ -151,6 +158,16 @@ public class ServerMain { autoCmdRemove = true; continue; } + case "--api", "-a" -> { + i++; + api = args[i]; + continue; + } + case "--api-files", "files-api", "-af" -> { + i++; + api4File = args[i]; + continue; + } } } @@ -197,9 +214,11 @@ public class ServerMain { logger.info(String.format(""" ServerMain.java Loaded >>> - - version %s %s (%s)(%d)""", - MornySystem.VERSION, MornySystem.CODENAME.toUpperCase(), - MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP + - version %s (%s)(%d) + - Morny %s""", + MornySystem.VERSION, + MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP, + MornySystem.CODENAME.toUpperCase() )); //# @@ -215,6 +234,7 @@ public class ServerMain { return; } MornyCoeur.main( + api, api4File, key, username, master, trustedChat, trustedReadersOfDinner, outdatedBlock?System.currentTimeMillis():0,