mirror of
https://github.com/Eyre-S/Coeur-Morny-Cono.git
synced 2024-11-22 19:24:53 +08:00
简单的事件分发系统,log时间戳和线程记录
This commit is contained in:
parent
aed6212ff1
commit
928359034a
@ -3,7 +3,7 @@ plugins {
|
||||
}
|
||||
|
||||
group 'cc.sukazyo'
|
||||
version '0.1'
|
||||
version '0.1.1'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
@ -5,10 +5,7 @@ public class Logger {
|
||||
public static final Logger logger = new Logger();
|
||||
|
||||
public void info(String message) {
|
||||
System.out.println(
|
||||
"[INFO]" +
|
||||
message.replaceAll("\\n", "\n[INFO]")
|
||||
);
|
||||
System.out.println(formatMessage(message, "INFO"));
|
||||
}
|
||||
|
||||
public void warn (String message) {
|
||||
@ -16,9 +13,17 @@ public class Logger {
|
||||
}
|
||||
|
||||
public void waring (String message) {
|
||||
System.out.println(
|
||||
"[WARN]" +
|
||||
message.replaceAll("\\n", "\n[WARN]")
|
||||
System.out.println(formatMessage(message, "WARN"));
|
||||
}
|
||||
|
||||
private String formatMessage (String message, String level) {
|
||||
String levelStr = "\n["+level+"]";
|
||||
return String.format(
|
||||
"[%d][%s][%s]%s",
|
||||
System.currentTimeMillis(),
|
||||
Thread.currentThread().getName(),
|
||||
level,
|
||||
message.replaceAll("\\n", levelStr)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cc.sukazyo.cono.morny;
|
||||
|
||||
import cc.sukazyo.cono.morny.bot.OnUpdate;
|
||||
import cc.sukazyo.cono.morny.bot.api.OnUpdate;
|
||||
import cc.sukazyo.cono.morny.bot.event.EventListeners;
|
||||
import com.pengrad.telegrambot.TelegramBot;
|
||||
import com.pengrad.telegrambot.request.GetMe;
|
||||
|
||||
@ -20,6 +21,7 @@ public class MornyCoeur {
|
||||
|
||||
logger.info("Bot login succeed.");
|
||||
|
||||
EventListeners.registerAllListeners();
|
||||
account.setUpdatesListener(OnUpdate::onNormalUpdate);
|
||||
|
||||
logger.info("System start complete");
|
||||
|
@ -1,21 +0,0 @@
|
||||
package cc.sukazyo.cono.morny.bot;
|
||||
|
||||
import com.pengrad.telegrambot.UpdatesListener;
|
||||
import com.pengrad.telegrambot.model.Update;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OnUpdate {
|
||||
|
||||
public static int onNormalUpdate (List<Update> updates) {
|
||||
for (Update update : updates) {
|
||||
if (update.message() != null) {
|
||||
if (update.message().text() != null) {
|
||||
new Thread(() -> OnCommandExecute.searchForCommands(update)).start();
|
||||
}
|
||||
}
|
||||
}
|
||||
return UpdatesListener.CONFIRMED_UPDATES_ALL;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package cc.sukazyo.cono.morny.bot.api;
|
||||
|
||||
import com.pengrad.telegrambot.model.Update;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class EventListener {
|
||||
|
||||
public boolean onMessage (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onEditedMessage (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onChannelPost (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onEditedChannelPost (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onInlineQuery (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onChosenInlineResult (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onCallbackQuery (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onShippingQuery (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onPreCheckoutQuery (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onPoll (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onPollAnswer (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onMyChatMemberUpdated (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onChatMemberUpdated (Update update) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
package cc.sukazyo.cono.morny.bot.api;
|
||||
|
||||
import com.pengrad.telegrambot.model.Update;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static cc.sukazyo.cono.morny.Logger.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(Update update, Function<EventListener, Boolean> exec) {
|
||||
this.setName("EVT"+update.updateId());
|
||||
this.exec = exec;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run () {
|
||||
for (EventListener x : listeners) {
|
||||
if (exec.apply(x)) return;
|
||||
}
|
||||
logger.info("event exited undone");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void addListener (EventListener... listeners) {
|
||||
EventListenerManager.listeners.addAll(Arrays.asList(listeners));
|
||||
}
|
||||
|
||||
public static void publishMessageEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onMessage(update)).start();
|
||||
}
|
||||
|
||||
public static void publishEditedMessageEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onEditedMessage(update)).start();
|
||||
}
|
||||
|
||||
public static void publishChannelPostEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onChannelPost(update)).start();
|
||||
}
|
||||
|
||||
public static void publishEditedChannelPostEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onEditedChannelPost(update)).start();
|
||||
}
|
||||
|
||||
public static void publishInlineQueryEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onInlineQuery(update)).start();
|
||||
}
|
||||
|
||||
public static void publishChosenInlineResultEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onChosenInlineResult(update)).start();
|
||||
}
|
||||
|
||||
public static void publishCallbackQueryEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onCallbackQuery(update)).start();
|
||||
}
|
||||
|
||||
public static void publishShippingQueryEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onShippingQuery(update)).start();
|
||||
}
|
||||
|
||||
public static void publishPreCheckoutQueryEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onPreCheckoutQuery(update)).start();
|
||||
}
|
||||
|
||||
public static void publishPollEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onPoll(update)).start();
|
||||
}
|
||||
|
||||
public static void publishPollAnswerEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onPollAnswer(update)).start();
|
||||
}
|
||||
|
||||
public static void publishMyChatMemberUpdatedEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onMyChatMemberUpdated(update)).start();
|
||||
}
|
||||
|
||||
public static void publishChatMemberUpdatedEvent (Update update) {
|
||||
new EventPublisher(update, x -> x.onChatMemberUpdated(update)).start();
|
||||
}
|
||||
|
||||
}
|
55
src/main/java/cc/sukazyo/cono/morny/bot/api/OnUpdate.java
Normal file
55
src/main/java/cc/sukazyo/cono/morny/bot/api/OnUpdate.java
Normal file
@ -0,0 +1,55 @@
|
||||
package cc.sukazyo.cono.morny.bot.api;
|
||||
|
||||
import com.pengrad.telegrambot.UpdatesListener;
|
||||
import com.pengrad.telegrambot.model.Update;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OnUpdate {
|
||||
|
||||
public static int onNormalUpdate (List<Update> updates) {
|
||||
for (Update update : updates) {
|
||||
if (update.message() != null) {
|
||||
EventListenerManager.publishMessageEvent(update);
|
||||
}
|
||||
if (update.editedMessage() != null) {
|
||||
EventListenerManager.publishEditedMessageEvent(update);
|
||||
}
|
||||
if (update.channelPost() != null) {
|
||||
EventListenerManager.publishChannelPostEvent(update);
|
||||
}
|
||||
if (update.editedChannelPost() != null) {
|
||||
EventListenerManager.publishEditedChannelPostEvent(update);
|
||||
}
|
||||
if (update.inlineQuery() != null) {
|
||||
EventListenerManager.publishInlineQueryEvent(update);
|
||||
}
|
||||
if (update.chosenInlineResult() != null) {
|
||||
EventListenerManager.publishChosenInlineResultEvent(update);
|
||||
}
|
||||
if (update.callbackQuery() != null) {
|
||||
EventListenerManager.publishCallbackQueryEvent(update);
|
||||
}
|
||||
if (update.shippingQuery() != null) {
|
||||
EventListenerManager.publishShippingQueryEvent(update);
|
||||
}
|
||||
if (update.preCheckoutQuery() != null) {
|
||||
EventListenerManager.publishPreCheckoutQueryEvent(update);
|
||||
}
|
||||
if (update.poll() != null) {
|
||||
EventListenerManager.publishPollEvent(update);
|
||||
}
|
||||
if (update.pollAnswer() != null) {
|
||||
EventListenerManager.publishPollAnswerEvent(update);
|
||||
}
|
||||
if (update.myChatMember() != null) {
|
||||
EventListenerManager.publishMyChatMemberUpdatedEvent(update);
|
||||
}
|
||||
if (update.chatMember() != null) {
|
||||
EventListenerManager.publishChatMemberUpdatedEvent(update);
|
||||
}
|
||||
}
|
||||
return UpdatesListener.CONFIRMED_UPDATES_ALL;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package cc.sukazyo.cono.morny.bot.event;
|
||||
|
||||
import cc.sukazyo.cono.morny.bot.api.EventListenerManager;
|
||||
|
||||
public class EventListeners {
|
||||
|
||||
public static final OnCommandExecute COMMANDS_LISTENER = new OnCommandExecute();
|
||||
|
||||
public static void registerAllListeners () {
|
||||
EventListenerManager.addListener(
|
||||
COMMANDS_LISTENER
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -1,20 +1,25 @@
|
||||
package cc.sukazyo.cono.morny.bot;
|
||||
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 com.pengrad.telegrambot.model.Update;
|
||||
import com.pengrad.telegrambot.request.SendSticker;
|
||||
|
||||
import static cc.sukazyo.cono.morny.Logger.logger;
|
||||
|
||||
public class OnCommandExecute {
|
||||
public class OnCommandExecute extends EventListener {
|
||||
|
||||
private static final String ONLINE_STATUS_RETURN_STICKER_ID = "CAACAgEAAx0CW-CvvgAC5eBhhhODGRuu0pxKLwoQ3yMsowjviAACcycAAnj8xgVVU666si1utiIE";
|
||||
private static final String HELLO_STICKER_ID = "CAACAgEAAxkBAAMnYYYWKNXO4ibo9dlsmDctHhhV6fIAAqooAAJ4_MYFJJhrHS74xUAiBA";
|
||||
private static final String EXIT_STICKER_ID = "CAACAgEAAxkBAAMoYYYWt8UjvP0N405SAyvg2SQZmokAAkMiAAJ4_MYFw6yZLu06b-MiBA";
|
||||
private static final String EXIT_403_STICKER_ID = "CAACAgEAAxkBAAMqYYYa_7hpXH6hMOYMX4Nh8AVYd74AAnQnAAJ4_MYFRdmmsQKLDZgiBA";
|
||||
|
||||
public static void searchForCommands (Update event) {
|
||||
@Override
|
||||
public boolean onMessage (Update event) {
|
||||
if (event.message().text() == null) {
|
||||
return false;
|
||||
}
|
||||
switch (event.message().text()) {
|
||||
case "/o":
|
||||
onCommandOnExec(event);
|
||||
@ -27,10 +32,12 @@ public class OnCommandExecute {
|
||||
onCommandExitExec(event);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void onCommandOnExec (Update event) {
|
||||
private void onCommandOnExec (Update event) {
|
||||
MornyCoeur.getAccount().execute(new SendSticker(
|
||||
event.message().chat().id(),
|
||||
ONLINE_STATUS_RETURN_STICKER_ID
|
||||
@ -38,7 +45,7 @@ public class OnCommandExecute {
|
||||
);
|
||||
}
|
||||
|
||||
private static void onCommandHelloExec (Update event) {
|
||||
private void onCommandHelloExec (Update event) {
|
||||
MornyCoeur.getAccount().execute(new SendSticker(
|
||||
event.message().chat().id(),
|
||||
HELLO_STICKER_ID
|
||||
@ -46,7 +53,7 @@ public class OnCommandExecute {
|
||||
);
|
||||
}
|
||||
|
||||
private static void onCommandExitExec (Update event) {
|
||||
private void onCommandExitExec (Update event) {
|
||||
if (MornyTrusted.isTrusted(event.message().from().id())) {
|
||||
MornyCoeur.getAccount().execute(new SendSticker(
|
||||
event.message().chat().id(),
|
||||
@ -61,6 +68,7 @@ public class OnCommandExecute {
|
||||
EXIT_403_STICKER_ID
|
||||
).replyToMessageId(event.message().messageId())
|
||||
);
|
||||
logger.info("403 exited tag from user @" + event.message().from().username());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user