mirror of
https://github.com/Eyre-S/Coeur-Morny-Cono.git
synced 2025-05-12 16:17:22 +08:00
- 优化了 coeur 中 exception 的 stackTrace 的输出方式 - 大幅度修改了 EventListenerManager 中报错的逻辑使其更加正常了许多 - 添加了 reportToChat 以及 --report-to 选项用在 MornyReport 中 - 为 coeur 中的错误报告添加了 MornyReport.exception - 为 save/exit 两个需要权限的命令添加了 MornyReport.unauthenticatedAction - 添加了一个方法可以检查 coeur(telegram_bot) 是否已完成初始化以存取 coeur 的内容
119 lines
3.8 KiB
Java
119 lines
3.8 KiB
Java
package cc.sukazyo.cono.morny.bot.api;
|
|
|
|
import cc.sukazyo.cono.morny.Log;
|
|
import cc.sukazyo.cono.morny.daemon.MornyReport;
|
|
import cc.sukazyo.cono.morny.util.tgapi.event.EventRuntimeException;
|
|
import com.google.gson.GsonBuilder;
|
|
import com.pengrad.telegrambot.model.Update;
|
|
|
|
import javax.annotation.Nonnull;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.function.Function;
|
|
|
|
import static cc.sukazyo.cono.morny.Log.logger;
|
|
|
|
public class EventListenerManager {
|
|
|
|
private static final List<EventListener> listeners = new ArrayList<>();
|
|
|
|
private static class EventPublisher extends Thread {
|
|
|
|
private final Function<EventListener, Boolean> exec;
|
|
|
|
public EventPublisher(@Nonnull Update update, @Nonnull Function<EventListener, Boolean> exec) {
|
|
this.setName("EVT"+update.updateId());
|
|
this.exec = exec;
|
|
}
|
|
|
|
@Override
|
|
public void run () {
|
|
for (EventListener x : listeners) {
|
|
try {
|
|
|
|
if (exec.apply(x)) return;
|
|
|
|
} catch (Exception e) {
|
|
|
|
final StringBuilder errorMessage = new StringBuilder();
|
|
errorMessage.append("Event throws unexpected exception:\n");
|
|
errorMessage.append(Log.exceptionLog(e).indent(4));
|
|
if (e instanceof EventRuntimeException.ActionFailed) {
|
|
errorMessage.append("\ntg-api action: response track: ");
|
|
errorMessage.append(new GsonBuilder().setPrettyPrinting().create().toJson(
|
|
((EventRuntimeException.ActionFailed)e).getResponse()
|
|
).indent(4)).append('\n');
|
|
}
|
|
logger.error(errorMessage.toString());
|
|
|
|
MornyReport.exception(e, "on event running");
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public static void addListener (@Nonnull EventListener... listeners) {
|
|
EventListenerManager.listeners.addAll(Arrays.asList(listeners));
|
|
}
|
|
|
|
public static void publishMessageEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onMessage(update)).start();
|
|
}
|
|
|
|
public static void publishEditedMessageEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onEditedMessage(update)).start();
|
|
}
|
|
|
|
public static void publishChannelPostEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onChannelPost(update)).start();
|
|
}
|
|
|
|
public static void publishEditedChannelPostEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onEditedChannelPost(update)).start();
|
|
}
|
|
|
|
public static void publishInlineQueryEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onInlineQuery(update)).start();
|
|
}
|
|
|
|
public static void publishChosenInlineResultEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onChosenInlineResult(update)).start();
|
|
}
|
|
|
|
public static void publishCallbackQueryEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onCallbackQuery(update)).start();
|
|
}
|
|
|
|
public static void publishShippingQueryEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onShippingQuery(update)).start();
|
|
}
|
|
|
|
public static void publishPreCheckoutQueryEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onPreCheckoutQuery(update)).start();
|
|
}
|
|
|
|
public static void publishPollEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onPoll(update)).start();
|
|
}
|
|
|
|
public static void publishPollAnswerEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onPollAnswer(update)).start();
|
|
}
|
|
|
|
public static void publishMyChatMemberUpdatedEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onMyChatMemberUpdated(update)).start();
|
|
}
|
|
|
|
public static void publishChatMemberUpdatedEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onChatMemberUpdated(update)).start();
|
|
}
|
|
|
|
public static void publishChatJoinRequestEvent (@Nonnull Update update) {
|
|
new EventPublisher(update, x -> x.onChatJoinRequest(update)).start();
|
|
}
|
|
|
|
}
|