mirror of
https://github.com/Eyre-S/Coeur-Morny-Cono.git
synced 2024-11-22 11:14:55 +08:00
添加封装的 command 对象,使 bot 忽略非自己为对象的命令,解锁 bot_username 限制,添加参数化 username 限制
This commit is contained in:
parent
360a9d3504
commit
b32b465a12
@ -7,7 +7,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group 'cc.sukazyo'
|
group 'cc.sukazyo'
|
||||||
version '0.3.4.3'
|
version '0.3.4.4'
|
||||||
project.ext.archiveBaseName = 'Coeur_Morny_Cono'
|
project.ext.archiveBaseName = 'Coeur_Morny_Cono'
|
||||||
project.ext.artifactId = 'morny-coeur'
|
project.ext.artifactId = 'morny-coeur'
|
||||||
mainClassName = 'cc.sukazyo.cono.morny.MornyCoeur'
|
mainClassName = 'cc.sukazyo.cono.morny.MornyCoeur'
|
||||||
|
@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny;
|
|||||||
* the final field that will be updated by gradle automatically.
|
* the final field that will be updated by gradle automatically.
|
||||||
*/
|
*/
|
||||||
public class GradleProjectConfigures {
|
public class GradleProjectConfigures {
|
||||||
public static final String VERSION = "0.3.4.3";
|
public static final String VERSION = "0.3.4.4";
|
||||||
public static final long COMPILE_TIMESTAMP = 1638871718439L;
|
public static final long COMPILE_TIMESTAMP = 1638884621273L;
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ public class MornyCoeur {
|
|||||||
* <br>
|
* <br>
|
||||||
* 出于技术限制,这个字段目前是写死的
|
* 出于技术限制,这个字段目前是写死的
|
||||||
*/
|
*/
|
||||||
public static final String USERNAME = "morny_cono_annie_bot";
|
public static String username;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 程序入口<br>
|
* 程序入口<br>
|
||||||
@ -77,6 +77,10 @@ public class MornyCoeur {
|
|||||||
configureSafeExit();
|
configureSafeExit();
|
||||||
|
|
||||||
logger.info("args key:\n " + args[0]);
|
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]); }
|
try { account = login(args[0]); }
|
||||||
catch (Exception e) { logger.error("Cannot login to bot/api. :\n " + e.getMessage()); System.exit(-1); }
|
catch (Exception e) { logger.error("Cannot login to bot/api. :\n " + e.getMessage()); System.exit(-1); }
|
||||||
@ -111,7 +115,7 @@ public class MornyCoeur {
|
|||||||
* <br>
|
* <br>
|
||||||
* 会反复尝试三次进行登录。如果登录失败,则会直接抛出 RuntimeException 结束处理。
|
* 会反复尝试三次进行登录。如果登录失败,则会直接抛出 RuntimeException 结束处理。
|
||||||
* 会通过 GetMe 动作验证是否连接上了 telegram api 服务器,
|
* 会通过 GetMe 动作验证是否连接上了 telegram api 服务器,
|
||||||
* 同时也要求登录获得的 username 和 {@link #USERNAME} 声明值相等
|
* 同时也要求登录获得的 username 和 {@link #username} 声明值相等
|
||||||
*
|
*
|
||||||
* @param key bot 的 api-token
|
* @param key bot 的 api-token
|
||||||
* @return 成功登录后的 {@link TelegramBot} 对象
|
* @return 成功登录后的 {@link TelegramBot} 对象
|
||||||
@ -124,8 +128,9 @@ public class MornyCoeur {
|
|||||||
if (i != 1) logger.info("retrying...");
|
if (i != 1) logger.info("retrying...");
|
||||||
try {
|
try {
|
||||||
final String username = account.execute(new GetMe()).user().username();
|
final String username = account.execute(new GetMe()).user().username();
|
||||||
if (!USERNAME.equals(username))
|
if (username != null && !MornyCoeur.username.equals(username))
|
||||||
throw new RuntimeException("Required the bot @"+USERNAME + " but @"+username + " logged in!");
|
throw new RuntimeException("Required the bot @" + MornyCoeur.username + " but @" + username + " logged in!");
|
||||||
|
else MornyCoeur.username = username;
|
||||||
logger.info("Succeed login to @" + username);
|
logger.info("Succeed login to @" + username);
|
||||||
return account;
|
return account;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -5,8 +5,8 @@ import cc.sukazyo.cono.morny.MornyCoeur;
|
|||||||
import cc.sukazyo.cono.morny.MornySystem;
|
import cc.sukazyo.cono.morny.MornySystem;
|
||||||
import cc.sukazyo.cono.morny.MornyTrusted;
|
import cc.sukazyo.cono.morny.MornyTrusted;
|
||||||
import cc.sukazyo.cono.morny.bot.api.EventListener;
|
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.bot.event.on_commands.GetUsernameAndId;
|
||||||
import cc.sukazyo.cono.morny.util.StringUtils;
|
|
||||||
import com.pengrad.telegrambot.model.Update;
|
import com.pengrad.telegrambot.model.Update;
|
||||||
import com.pengrad.telegrambot.model.request.ParseMode;
|
import com.pengrad.telegrambot.model.request.ParseMode;
|
||||||
import com.pengrad.telegrambot.request.SendMessage;
|
import com.pengrad.telegrambot.request.SendMessage;
|
||||||
@ -32,37 +32,33 @@ public class OnCommandExecute extends EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public boolean onMessage (@Nonnull Update event) {
|
public boolean onMessage (@Nonnull Update event) {
|
||||||
if (event.message().text() == null) {
|
if (event.message().text() == null) {
|
||||||
return false;
|
return false; // 检测到无消息文本,忽略掉命令处理
|
||||||
}
|
}
|
||||||
final String[] command = StringUtils.formatCommand(event.message().text());
|
final InputCommand command = new InputCommand(event.message().text());
|
||||||
if (command.length == 0) return false;
|
if (command.getTarget() != null && !MornyCoeur.username.equals(command.getTarget())) {
|
||||||
switch (command[0]) {
|
return true; // 检测到命令并非针对 morny,退出整个事件处理链
|
||||||
|
}
|
||||||
|
switch (command.getCommand()) {
|
||||||
case "/user":
|
case "/user":
|
||||||
case "/user@" + MornyCoeur.USERNAME:
|
GetUsernameAndId.exec(command.getArgs(), event);
|
||||||
GetUsernameAndId.exec(command, event);
|
|
||||||
break;
|
break;
|
||||||
case "/o":
|
case "/o":
|
||||||
case "/o@" + MornyCoeur.USERNAME:
|
|
||||||
onCommandOnExec(event);
|
onCommandOnExec(event);
|
||||||
break;
|
break;
|
||||||
case "/hi":
|
case "/hi":
|
||||||
case "/hi@" + MornyCoeur.USERNAME:
|
|
||||||
case "/hello":
|
case "/hello":
|
||||||
case "/hello@" + MornyCoeur.USERNAME:
|
|
||||||
onCommandHelloExec(event);
|
onCommandHelloExec(event);
|
||||||
break;
|
break;
|
||||||
case "/exit":
|
case "/exit":
|
||||||
case "/exit@" + MornyCoeur.USERNAME:
|
|
||||||
onCommandExitExec(event);
|
onCommandExitExec(event);
|
||||||
break;
|
break;
|
||||||
case "/version":
|
case "/version":
|
||||||
case "/version@" + MornyCoeur.USERNAME:
|
|
||||||
onCommandVersionExec(event);
|
onCommandVersionExec(event);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false; // 无法解析的命令,转交事件链后代处理
|
||||||
}
|
}
|
||||||
return true;
|
return true; // 命令执行成功,标记事件为已处理,退出事件链
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onCommandOnExec (@Nonnull Update event) {
|
private void onCommandOnExec (@Nonnull Update event) {
|
||||||
|
@ -12,9 +12,9 @@ import javax.annotation.Nonnull;
|
|||||||
|
|
||||||
public class GetUsernameAndId {
|
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(),
|
event.message().chat().id(),
|
||||||
"[Unavailable] Too much arguments."
|
"[Unavailable] Too much arguments."
|
||||||
).replyToMessageId(event.message().messageId())); return; }
|
).replyToMessageId(event.message().messageId())); return; }
|
||||||
@ -24,9 +24,9 @@ public class GetUsernameAndId {
|
|||||||
if ( event.message().replyToMessage()!= null) {
|
if ( event.message().replyToMessage()!= null) {
|
||||||
userId = event.message().replyToMessage().from().id();
|
userId = event.message().replyToMessage().from().id();
|
||||||
}
|
}
|
||||||
if (command.length > 1) {
|
if (args.length > 0) {
|
||||||
try {
|
try {
|
||||||
userId = Long.parseLong(command[1]);
|
userId = Long.parseLong(args[0]);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
MornyCoeur.getAccount().execute(new SendMessage(
|
MornyCoeur.getAccount().execute(new SendMessage(
|
||||||
event.message().chat().id(),
|
event.message().chat().id(),
|
||||||
@ -60,18 +60,21 @@ public class GetUsernameAndId {
|
|||||||
|
|
||||||
final StringBuilder userInformation = new StringBuilder();
|
final StringBuilder userInformation = new StringBuilder();
|
||||||
userInformation.append(String.format(
|
userInformation.append(String.format(
|
||||||
"userid :\n" +
|
"""
|
||||||
"- <code>%d</code>\n" +
|
userid :
|
||||||
"username :\n" +
|
- <code>%d</code>
|
||||||
"- <code>%s</code>",
|
username :
|
||||||
|
- <code>%s</code>""",
|
||||||
userId, user.username()
|
userId, user.username()
|
||||||
));
|
));
|
||||||
if (user.firstName() == null) {
|
if (user.firstName() == null) {
|
||||||
userInformation.append("\nfirstname : <u>null</u>");
|
userInformation.append("\nfirstname : <u>null</u>");
|
||||||
} else {
|
} else {
|
||||||
userInformation.append(String.format(
|
userInformation.append(String.format(
|
||||||
"\nfirstname :\n" +
|
"""
|
||||||
"- <code>%s</code>",
|
|
||||||
|
firstname :
|
||||||
|
- <code>%s</code>""",
|
||||||
user.firstName()
|
user.firstName()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -79,15 +82,19 @@ public class GetUsernameAndId {
|
|||||||
userInformation.append("\nlastname : <u>null</u>");
|
userInformation.append("\nlastname : <u>null</u>");
|
||||||
} else {
|
} else {
|
||||||
userInformation.append(String.format(
|
userInformation.append(String.format(
|
||||||
"\nlastname :\n" +
|
"""
|
||||||
"- <code>%s</code>",
|
|
||||||
|
lastname :
|
||||||
|
- <code>%s</code>""",
|
||||||
user.lastName()
|
user.lastName()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
if (user.languageCode() != null) {
|
if (user.languageCode() != null) {
|
||||||
userInformation.append(String.format(
|
userInformation.append(String.format(
|
||||||
"\nlanguage-code :\n" +
|
"""
|
||||||
"- <code>%s</code>",
|
|
||||||
|
language-code :
|
||||||
|
- <code>%s</code>""",
|
||||||
user.languageCode()
|
user.languageCode()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user