util迁移:untitled:util-telegram-api:1.2 适应, 删除 debug bug

This commit is contained in:
A.C.Sukazyo Eyre 2022-01-16 03:06:23 +08:00
parent 19b7c65559
commit c6c9f2b0e9
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
16 changed files with 49 additions and 120 deletions

View File

@ -1,6 +1,6 @@
## Core
VERSION = 0.4.3.3
VERSION = 0.4.3.4
# dependencies

View File

@ -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.4.3.3";
public static final long COMPILE_TIMESTAMP = 1641752627626L;
public static final String VERSION = "0.4.3.4";
public static final long COMPILE_TIMESTAMP = 1642273245554L;
}

View File

@ -3,6 +3,8 @@ package cc.sukazyo.cono.morny;
import cc.sukazyo.cono.morny.bot.api.OnUpdate;
import cc.sukazyo.cono.morny.bot.event.EventListeners;
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.request.GetMe;
@ -25,6 +27,7 @@ public class MornyCoeur {
/** morny 的 bot 账户 */
private final TelegramBot account;
private final ExtraAction extraActionInstance;
/**
* morny bot 账户的用户名<br>
* <br>
@ -95,6 +98,8 @@ public class MornyCoeur {
throw ex;
}
this.extraActionInstance = ExtraAction.as(account);
logger.info("Bot login succeed.");
}
@ -224,4 +229,9 @@ public class MornyCoeur {
return INSTANCE.trusted;
}
@Nonnull
public static ExtraAction extra () {
return INSTANCE.extraActionInstance;
}
}

View File

@ -1,7 +1,6 @@
package cc.sukazyo.cono.morny;
import com.pengrad.telegrambot.model.ChatMember;
import com.pengrad.telegrambot.request.GetChatMember;
import com.pengrad.telegrambot.model.ChatMember.Status;
/**
* 对用户进行身份权限验证的管理类
@ -37,13 +36,7 @@ public class MornyTrusted {
*/
public boolean isTrusted (long userId) {
if (userId == MASTER) return true;
final ChatMember chatMember = MornyCoeur.getAccount().execute(new GetChatMember(TRUSTED_CHAT_ID, userId)).chatMember();
return (
chatMember != null && (
chatMember.status() == ChatMember.Status.administrator ||
chatMember.status() == ChatMember.Status.creator
)
);
return MornyCoeur.extra().isUserInGroup(userId, TRUSTED_CHAT_ID, Status.administrator);
}
}

View File

@ -3,6 +3,8 @@ package cc.sukazyo.cono.morny.bot.api;
import com.google.gson.GsonBuilder;
import com.pengrad.telegrambot.model.Update;
import cc.sukazyo.untitled.telegram.api.event.EventRuntimeException;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;

View File

@ -1,35 +0,0 @@
package cc.sukazyo.cono.morny.bot.api;
import com.pengrad.telegrambot.response.BaseResponse;
public class EventRuntimeException extends RuntimeException {
public EventRuntimeException () {
super();
}
public EventRuntimeException (String message) {
super(message);
}
public static class ActionFailed extends EventRuntimeException {
private final BaseResponse response;
public ActionFailed (BaseResponse response) {
super();
this.response = response;
}
public ActionFailed (String message, BaseResponse response) {
super(message);
this.response = response;
}
public BaseResponse getResponse() {
return response;
}
}
}

View File

@ -1,32 +0,0 @@
package cc.sukazyo.cono.morny.bot.api;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.request.BaseRequest;
import com.pengrad.telegrambot.response.BaseResponse;
public class Executor {
private final TelegramBot client;
public Executor (TelegramBot bot) {
client = bot;
}
public static Executor as (TelegramBot bot) {
return new Executor(bot);
}
public <T extends BaseRequest<T, R>, R extends BaseResponse> R exec (T req) {
return exec(req, "");
}
public <T extends BaseRequest<T, R>, R extends BaseResponse> R exec (T req, String errorMessage) {
final R resp = client.execute(req);
if (!resp.isOk()) throw new EventRuntimeException.ActionFailed(
(errorMessage.equals("") ? String.valueOf(resp.errorCode()) : errorMessage),
resp
);
return resp;
}
}

View File

@ -3,7 +3,6 @@ package cc.sukazyo.cono.morny.bot.event;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.MornyTrusted;
import cc.sukazyo.cono.morny.bot.api.EventListener;
import cc.sukazyo.cono.morny.bot.api.Executor;
import cc.sukazyo.cono.morny.data.TelegramStickers;
import com.pengrad.telegrambot.model.Chat;
import com.pengrad.telegrambot.model.Update;
@ -56,7 +55,7 @@ public class OnCallMe extends EventListener {
return false;
}
}
Executor.as(MornyCoeur.getAccount()).exec(new SendSticker(
MornyCoeur.extra().exec(new SendSticker(
update.message().chat().id(),
TelegramStickers.ID_SENT
).replyToMessageId(update.message().messageId())
@ -71,7 +70,7 @@ public class OnCallMe extends EventListener {
* @param event 执行呼叫的tg事件
*/
private static void requestSteamJoin (Update event) {
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
ME, String.format(
"""
request <b>STEAM LIBRARY</b>
@ -91,7 +90,7 @@ public class OnCallMe extends EventListener {
* @param event 执行呼叫的tg事件
*/
private static void requestHanaParesuJoin (Update event) {
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
ME, String.format(
"""
request <b>Hana Paresu</b>
@ -119,7 +118,7 @@ public class OnCallMe extends EventListener {
* @since 0.4.2.2
*/
private static void requestCustomCall (Update event) {
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
ME, String.format(
"""
request <u>[???]</u>
@ -130,7 +129,7 @@ public class OnCallMe extends EventListener {
)
)
).parseMode(ParseMode.HTML));
Executor.as(MornyCoeur.getAccount()).exec(new ForwardMessage(
MornyCoeur.extra().exec(new ForwardMessage(
ME,
event.message().chat().id(),
event.message().messageId()

View File

@ -4,7 +4,6 @@ import cc.sukazyo.cono.morny.GradleProjectConfigures;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.MornySystem;
import cc.sukazyo.cono.morny.bot.api.EventListener;
import cc.sukazyo.cono.morny.bot.api.Executor;
import cc.sukazyo.cono.morny.bot.event.on_commands.EventHack;
import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId;
import cc.sukazyo.cono.morny.bot.event.on_commands.Ip186Query;
@ -81,7 +80,7 @@ public class OnCommandExecute extends EventListener {
private boolean nonCommandExecutable (Update event, InputCommand command) {
if (command.getTarget() == null) return false; // 无法解析的命令转交事件链后代处理
else { // 无法解析的显式命令格式报错找不到命令
Executor.as(MornyCoeur.getAccount()).exec(new SendSticker(
MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(),
TelegramStickers.ID_404
).replyToMessageId(event.message().messageId())
@ -91,7 +90,7 @@ public class OnCommandExecute extends EventListener {
}
private void onCommandOnExec (@Nonnull Update event) {
Executor.as(MornyCoeur.getAccount()).exec(new SendSticker(
MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(),
TelegramStickers.ID_ONLINE_STATUS_RETURN
).replyToMessageId(event.message().messageId())
@ -99,7 +98,7 @@ public class OnCommandExecute extends EventListener {
}
private void onCommandHelloExec (@Nonnull Update event) {
Executor.as(MornyCoeur.getAccount()).exec(new SendSticker(
MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(),
TelegramStickers.ID_HELLO
).replyToMessageId(event.message().messageId())
@ -108,7 +107,7 @@ public class OnCommandExecute extends EventListener {
private void onCommandExitExec (@Nonnull Update event) {
if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) {
Executor.as(MornyCoeur.getAccount()).exec(new SendSticker(
MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(),
TelegramStickers.ID_EXIT
).replyToMessageId(event.message().messageId())
@ -116,7 +115,7 @@ public class OnCommandExecute extends EventListener {
logger.info("Morny exited by user @" + event.message().from().username());
System.exit(0);
} else {
Executor.as(MornyCoeur.getAccount()).exec(new SendSticker(
MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(),
TelegramStickers.ID_403
).replyToMessageId(event.message().messageId())
@ -126,7 +125,7 @@ public class OnCommandExecute extends EventListener {
}
private void onCommandVersionExec (@Nonnull Update event) {
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
String.format(
"""
@ -134,7 +133,7 @@ public class OnCommandExecute extends EventListener {
- <code>%s</code>
core md5_hash:
- <code>%s</code>
compile time<<sta?>mp:
compile timestamp:
- <code>%d</code>
- <code>%s [UTC]</code>""",
escapeHtml(MornySystem.VERSION),
@ -149,7 +148,7 @@ public class OnCommandExecute extends EventListener {
* @since 0.4.1.2
*/
private void onCommandRuntimeExec (@Nonnull Update event) {
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
String.format("""
system:
@ -198,7 +197,7 @@ public class OnCommandExecute extends EventListener {
private void onCommandJrrpExec (Update event) {
final double jrrp = MornyJrrp.getJrrpFromTelegramUser(event.message().from(), System.currentTimeMillis());
final String endChar = jrrp>70 ? "!" : jrrp>30 ? ";" : "...";
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
String.format(
"<a href='tg://user?id=%d'>%s</a> 在(utc的)今天的运气指数是———— <code>%.2f%%</code> %s",
@ -216,13 +215,13 @@ public class OnCommandExecute extends EventListener {
if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) {
logger.info(String.format("called save from command by @%s.", event.message().from().username()));
MornyCoeur.callSaveData();
Executor.as(MornyCoeur.getAccount()).exec(new SendSticker(
MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(),
TelegramStickers.ID_SAVED
).replyToMessageId(event.message().messageId())
);
} else {
Executor.as(MornyCoeur.getAccount()).exec(new SendSticker(
MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(),
TelegramStickers.ID_403
).replyToMessageId(event.message().messageId())

View File

@ -2,7 +2,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.bot.api.Executor;
import cc.sukazyo.untitled.util.telegram.formatting.MsgEscape;
import com.google.gson.GsonBuilder;
@ -66,7 +65,7 @@ public class OnEventHackHandle extends EventListener {
if (x == null) x = hackers.remove("[[]]");
if (x == null) return false;
logger.debug("hacked event by " + x);
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(x.fromChatId, String.format(
MornyCoeur.extra().exec(new SendMessage(x.fromChatId, String.format(
"<code>%s</code>",
MsgEscape.escapeHtml(new GsonBuilder().setPrettyPrinting().create().toJson(update))
)).parseMode(ParseMode.HTML).replyToMessageId((int)x.fromMessageId));

View File

@ -2,7 +2,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.bot.api.Executor;
import cc.sukazyo.cono.morny.util.EncryptUtils;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.InlineQueryResultArticle;
@ -25,7 +24,7 @@ public class OnInlineQuery extends EventListener {
*/
@Override
public boolean onInlineQuery (@Nonnull Update update) {
Executor.as(MornyCoeur.getAccount()).exec(new AnswerInlineQuery(update.inlineQuery().id(), new InlineQueryResultArticle[]{
MornyCoeur.extra().exec(new AnswerInlineQuery(update.inlineQuery().id(), new InlineQueryResultArticle[]{
new InlineQueryResultArticle(
EncryptUtils.encryptByMD5(update.inlineQuery().query()),
"Raw Input",

View File

@ -2,7 +2,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.bot.api.Executor;
import cc.sukazyo.untitled.util.command.CommonCommand;
import cc.sukazyo.untitled.util.string.StringArrays;
@ -49,7 +48,7 @@ public class OnUserSlashAction extends EventListener {
event.message().replyToMessage().from()
));
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
String.format(
"<a href='tg://user?id=%d'>%s</a> %s%s <a href='tg://user?id=%d'>%s</a>%s%s",

View File

@ -2,7 +2,6 @@ package cc.sukazyo.cono.morny.bot.event.on_commands;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.MornyTrusted;
import cc.sukazyo.cono.morny.bot.api.Executor;
import cc.sukazyo.cono.morny.bot.event.OnEventHackHandle;
import cc.sukazyo.cono.morny.data.TelegramStickers;
import cc.sukazyo.untitled.util.telegram.object.InputCommand;
@ -66,13 +65,13 @@ public class EventHack {
}
if (isOk) {
Executor.as(MornyCoeur.getAccount()).exec(new SendSticker(
MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(),
TelegramStickers.ID_WAITING
).replyToMessageId(event.message().messageId())
);
} else {
Executor.as(MornyCoeur.getAccount()).exec(new SendSticker(
MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(),
TelegramStickers.ID_403
).replyToMessageId(event.message().messageId())

View File

@ -1,7 +1,6 @@
package cc.sukazyo.cono.morny.bot.event.on_commands;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.bot.api.Executor;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.User;
@ -18,7 +17,7 @@ public class GetUsernameAndId {
public static void exec (@Nonnull String[] args, @Nonnull Update event) {
if (args.length > 1) { Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
if (args.length > 1) { MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
"[Unavailable] Too much arguments."
).replyToMessageId(event.message().messageId())); return; }
@ -32,7 +31,7 @@ public class GetUsernameAndId {
try {
userId = Long.parseLong(args[0]);
} catch (NumberFormatException e) {
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
"[Unavailable] " + e.getMessage()
).replyToMessageId(event.message().messageId()));
@ -40,12 +39,12 @@ public class GetUsernameAndId {
}
}
final GetChatMemberResponse response = Executor.as(MornyCoeur.getAccount()).exec(
final GetChatMemberResponse response = MornyCoeur.extra().exec(
new GetChatMember(event.message().chat().id(), userId)
);
if (response.chatMember() == null) {
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
"[Unavailable] user not found."
).replyToMessageId(event.message().messageId()));
@ -104,7 +103,7 @@ public class GetUsernameAndId {
));
}
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
userInformation.toString()
).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));

View File

@ -1,7 +1,6 @@
package cc.sukazyo.cono.morny.bot.event.on_commands;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.bot.api.Executor;
import cc.sukazyo.cono.morny.data.ip186.IP186QueryResponse;
import cc.sukazyo.untitled.util.telegram.object.InputCommand;
import cc.sukazyo.cono.morny.data.ip186.IP186QueryHandler;
@ -27,7 +26,7 @@ public class Ip186Query {
arg = event.message().replyToMessage().text();
}
} else if (command.getArgs().length > 1) {
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
"[Unavailable] Too much arguments."
).replyToMessageId(event.message().messageId()));
@ -36,7 +35,7 @@ public class Ip186Query {
arg = command.getArgs()[0];
}
if (arg == null) {
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
"[Unavailable] No ip defined."
).replyToMessageId(event.message().messageId()));
@ -49,12 +48,12 @@ public class Ip186Query {
case "/whois" -> IP186QueryHandler.queryWhois(arg);
default -> throw new IllegalArgumentException("Unknown 186-IP query method " + command.getCommand());
};
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
escapeHtml(response.url()) + "\n<code>" + escapeHtml(response.body()) + "</code>"
).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId()));
} catch (Exception e) {
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
"[Exception] in query:\n<code>" + escapeHtml(e.getMessage()) + "</code>"
).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId()));

View File

@ -5,7 +5,6 @@ import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.SendMessage;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.bot.api.Executor;
import cc.sukazyo.cono.morny.data.NbnhhshQuery;
import cc.sukazyo.untitled.util.string.StringArrays;
import cc.sukazyo.untitled.util.telegram.object.InputCommand;
@ -36,13 +35,13 @@ public class Nbnhhsh {
}
}
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
message.toString()
).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId()));
} catch (Exception e) {
Executor.as(MornyCoeur.getAccount()).exec(new SendMessage(
MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(),
"[Exception] in query:\n<code>" + escapeHtml(e.getMessage()) + "</code>"
).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId()));