diff --git a/build.gradle b/build.gradle
index 9877449..2d6ec16 100644
--- a/build.gradle
+++ b/build.gradle
@@ -7,7 +7,7 @@ plugins {
}
group 'cc.sukazyo'
-version '0.3.4.3'
+version '0.3.4.4'
project.ext.archiveBaseName = 'Coeur_Morny_Cono'
project.ext.artifactId = 'morny-coeur'
mainClassName = 'cc.sukazyo.cono.morny.MornyCoeur'
diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java
index 85f85a5..87c1e24 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.3.4.3";
- public static final long COMPILE_TIMESTAMP = 1638871718439L;
+ public static final String VERSION = "0.3.4.4";
+ public static final long COMPILE_TIMESTAMP = 1638884621273L;
}
diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java
index ff5ed18..ed1556a 100644
--- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java
+++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java
@@ -30,7 +30,7 @@ public class MornyCoeur {
*
* 出于技术限制,这个字段目前是写死的
*/
- public static final String USERNAME = "morny_cono_annie_bot";
+ public static String username;
/**
* 程序入口
@@ -77,6 +77,10 @@ public class MornyCoeur {
configureSafeExit();
logger.info("args key:\n " + args[0]);
+ if (args.length > 2) {
+ username = args[2];
+ logger.info("login as:\n " + args[2]);
+ }
try { account = login(args[0]); }
catch (Exception e) { logger.error("Cannot login to bot/api. :\n " + e.getMessage()); System.exit(-1); }
@@ -111,7 +115,7 @@ public class MornyCoeur {
*
* 会反复尝试三次进行登录。如果登录失败,则会直接抛出 RuntimeException 结束处理。
* 会通过 GetMe 动作验证是否连接上了 telegram api 服务器,
- * 同时也要求登录获得的 username 和 {@link #USERNAME} 声明值相等
+ * 同时也要求登录获得的 username 和 {@link #username} 声明值相等
*
* @param key bot 的 api-token
* @return 成功登录后的 {@link TelegramBot} 对象
@@ -124,8 +128,9 @@ public class MornyCoeur {
if (i != 1) logger.info("retrying...");
try {
final String username = account.execute(new GetMe()).user().username();
- if (!USERNAME.equals(username))
- throw new RuntimeException("Required the bot @"+USERNAME + " but @"+username + " logged in!");
+ if (username != null && !MornyCoeur.username.equals(username))
+ throw new RuntimeException("Required the bot @" + MornyCoeur.username + " but @" + username + " logged in!");
+ else MornyCoeur.username = username;
logger.info("Succeed login to @" + username);
return account;
} catch (Exception e) {
diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/InputCommand.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/InputCommand.java
new file mode 100644
index 0000000..38e4a0b
--- /dev/null
+++ b/src/main/java/cc/sukazyo/cono/morny/bot/api/InputCommand.java
@@ -0,0 +1,61 @@
+package cc.sukazyo.cono.morny.bot.api;
+
+import cc.sukazyo.cono.morny.util.StringUtils;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import java.util.Arrays;
+
+public class InputCommand {
+
+ private final String target;
+ private final String command;
+ private final String[] args;
+
+ private InputCommand (@Nullable String target, @Nonnull String command, @Nonnull String[] args) {
+ this.target = target;
+ this.command = command;
+ this.args = args;
+ }
+
+ public InputCommand (@Nonnull String[] inputArray) {
+ this(parseInputArray(inputArray));
+ }
+
+ public InputCommand (@Nonnull String input) {
+ this(StringUtils.formatCommand(input));
+ }
+
+ public InputCommand (@Nonnull InputCommand source) {
+ this(source.target, source.command, source.args);
+ }
+
+ public static InputCommand parseInputArray (@Nonnull String[] inputArray) {
+ final String[] cx = inputArray[0].split("@", 2);
+ final String[] args = new String[inputArray.length-1];
+ System.arraycopy(inputArray, 1, args, 0, inputArray.length - 1);
+ return new InputCommand(cx.length == 1 ? null : cx[1], cx[0], args);
+ }
+
+ public String getTarget () {
+ return target;
+ }
+
+ public String getCommand () {
+ return command;
+ }
+
+ public String[] getArgs () {
+ return args;
+ }
+
+ public boolean hasArgs () {
+ return args.length != 0;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("{{%s}@{%s}#{%s}}", command, target, Arrays.toString(args));
+ }
+
+}
diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java
index a80293c..1db0756 100644
--- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java
+++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java
@@ -5,8 +5,8 @@ import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.MornySystem;
import cc.sukazyo.cono.morny.MornyTrusted;
import cc.sukazyo.cono.morny.bot.api.EventListener;
+import cc.sukazyo.cono.morny.bot.api.InputCommand;
import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId;
-import cc.sukazyo.cono.morny.util.StringUtils;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.SendMessage;
@@ -32,37 +32,33 @@ public class OnCommandExecute extends EventListener {
@Override
public boolean onMessage (@Nonnull Update event) {
if (event.message().text() == null) {
- return false;
+ return false; // 检测到无消息文本,忽略掉命令处理
}
- final String[] command = StringUtils.formatCommand(event.message().text());
- if (command.length == 0) return false;
- switch (command[0]) {
+ final InputCommand command = new InputCommand(event.message().text());
+ if (command.getTarget() != null && !MornyCoeur.username.equals(command.getTarget())) {
+ return true; // 检测到命令并非针对 morny,退出整个事件处理链
+ }
+ switch (command.getCommand()) {
case "/user":
- case "/user@" + MornyCoeur.USERNAME:
- GetUsernameAndId.exec(command, event);
+ GetUsernameAndId.exec(command.getArgs(), event);
break;
case "/o":
- case "/o@" + MornyCoeur.USERNAME:
onCommandOnExec(event);
break;
case "/hi":
- case "/hi@" + MornyCoeur.USERNAME:
case "/hello":
- case "/hello@" + MornyCoeur.USERNAME:
onCommandHelloExec(event);
break;
case "/exit":
- case "/exit@" + MornyCoeur.USERNAME:
onCommandExitExec(event);
break;
case "/version":
- case "/version@" + MornyCoeur.USERNAME:
onCommandVersionExec(event);
break;
default:
- return false;
+ return false; // 无法解析的命令,转交事件链后代处理
}
- return true;
+ return true; // 命令执行成功,标记事件为已处理,退出事件链
}
private void onCommandOnExec (@Nonnull Update event) {
diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java
index b575fb5..b54bb9d 100644
--- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java
+++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java
@@ -12,9 +12,9 @@ import javax.annotation.Nonnull;
public class GetUsernameAndId {
- public static void exec (@Nonnull String[] command, @Nonnull Update event) {
+ public static void exec (@Nonnull String[] args, @Nonnull Update event) {
- if (command.length > 2) { MornyCoeur.getAccount().execute(new SendMessage(
+ if (args.length > 1) { MornyCoeur.getAccount().execute(new SendMessage(
event.message().chat().id(),
"[Unavailable] Too much arguments."
).replyToMessageId(event.message().messageId())); return; }
@@ -24,9 +24,9 @@ public class GetUsernameAndId {
if ( event.message().replyToMessage()!= null) {
userId = event.message().replyToMessage().from().id();
}
- if (command.length > 1) {
+ if (args.length > 0) {
try {
- userId = Long.parseLong(command[1]);
+ userId = Long.parseLong(args[0]);
} catch (NumberFormatException e) {
MornyCoeur.getAccount().execute(new SendMessage(
event.message().chat().id(),
@@ -60,18 +60,21 @@ public class GetUsernameAndId {
final StringBuilder userInformation = new StringBuilder();
userInformation.append(String.format(
- "userid :\n" +
- "- %d
\n" +
- "username :\n" +
- "- %s
",
- userId, user.username()
+ """
+ userid :
+ - %d
+ username :
+ - %s
""",
+ userId, user.username()
));
if (user.firstName() == null) {
userInformation.append("\nfirstname : null");
} else {
userInformation.append(String.format(
- "\nfirstname :\n" +
- "- %s
",
+ """
+
+ firstname :
+ - %s
""",
user.firstName()
));
}
@@ -79,15 +82,19 @@ public class GetUsernameAndId {
userInformation.append("\nlastname : null");
} else {
userInformation.append(String.format(
- "\nlastname :\n" +
- "- %s
",
+ """
+
+ lastname :
+ - %s
""",
user.lastName()
));
}
if (user.languageCode() != null) {
userInformation.append(String.format(
- "\nlanguage-code :\n" +
- "- %s
",
+ """
+
+ language-code :
+ - %s
""",
user.languageCode()
));
}