From 8b41111a495e3cb5b96b2208a6a8a2588801ff2f Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 3 Jun 2022 13:53:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=B9=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E7=9A=84=E6=A3=80=E6=9F=A5=EF=BC=8C=E4=B8=BA?= =?UTF-8?q?=20exit=20=E6=B7=BB=E5=8A=A0=20/quit=20/stop=20=E9=9A=90?= =?UTF-8?q?=E8=97=8F=E5=88=AB=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 OnTelegramCommand 的命令格式检查优化,在确定为 telegram command 格式时不会进行下一步执行 - 在 SlashAction 的命令格式检查支持正常检查 @ 后缀 --- gradle.properties | 2 +- .../sukazyo/cono/morny/GradleProjectConfigures.java | 4 ++-- .../sukazyo/cono/morny/bot/command/MornyCommands.java | 11 ++++++++--- .../cono/morny/bot/event/OnTelegramCommand.java | 7 ++++++- .../cono/morny/bot/event/OnUserSlashAction.java | 2 +- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/gradle.properties b/gradle.properties index 5a613bb..f9896ea 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.16 +VERSION = 0.7.1.0 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 da03946..b0888fc 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.16"; + public static final String VERSION = "0.7.1.0"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1654058419036L; + public static final long COMPILE_TIMESTAMP = 1654235503515L; } 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 1975177..2918a80 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 @@ -14,6 +14,7 @@ import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.request.SendSticker; import com.pengrad.telegrambot.request.SetMyCommands; +import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -60,8 +61,7 @@ public class MornyCommands { register( new ON(), - new Hello(), - new HelloOnStart(), + new Hello(), new HelloOnStart(), new GetUsernameAndId(), new EventHack(), new Nbnhhsh(), @@ -71,7 +71,7 @@ public class MornyCommands { new Version(), new MornyRuntime(), new Jrrp(), - new Exit() + new Exit(), new ExitAlias() ); // 特殊的命令 @@ -197,6 +197,11 @@ public class MornyCommands { @Nonnull @Override public String getDescription () { return "关闭 Bot (仅可信成员)"; } @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { onCommandExitExec(event); } } + private static class ExitAlias implements ISimpleCommand { + @Nonnull @Override public String getName () { return "quit"; } + @Nullable @Override public String[] getAliases () { return new String[]{"stop"}; } + @Override public void execute (@NotNull InputCommand command, @NotNull Update event) { onCommandExitExec(event); } + } private static void onCommandExitExec (@Nonnull Update event) { if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) { MornyCoeur.extra().exec(new SendSticker( 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 0c632ab..0266cfa 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 @@ -8,14 +8,19 @@ import com.pengrad.telegrambot.model.Update; import javax.annotation.Nonnull; +import static cc.sukazyo.cono.morny.Log.logger; + public class OnTelegramCommand extends EventListener { @Override public boolean onMessage (@Nonnull Update event) { - if (event.message().text() == null || !event.message().text().startsWith("/")) { + if (event.message().text() == null || !event.message().text().startsWith("/") || event.message().text().startsWith("/ ")) { + logger.debug("not command"); return false; // 检测到非(命令格式)文本,忽略掉命令处理 } final InputCommand command = new InputCommand(event.message().text().substring(1)); + if (!command.getCommand().matches("^\\w+$")) { logger.debug("not command");return false; } + logger.debug("is command"); if (command.getTarget() != null && !MornyCoeur.getUsername().equals(command.getTarget())) { return true; // 检测到命令并非针对 morny,退出整个事件处理链 } 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 960ff17..900f387 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 @@ -41,7 +41,7 @@ public class OnUserSlashAction extends EventListener { final String[] action = CommonCommand.format(text); action[0] = action[0].substring(1); - if (action[0].matches("^[a-zA-Z_]+$")) { + if (action[0].matches("^\\w+(@\\w+)?$")) { return false; // 忽略掉 Telegram 命令格式的输入 } else if (action[0].contains("/")) { return false; // 忽略掉疑似目录格式的输入