From 6dfaade55ce44c4c5692f9b83a2267aad8ee2eea Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 1 Apr 2022 16:59:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=B7=E6=B1=82=E5=88=A0?= =?UTF-8?q?=E9=99=A4=20bot=20=E5=9B=9E=E5=A4=8D=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cc/sukazyo/cono/morny/MornyCoeur.java | 25 ++++++--- .../morny/bot/command/DirectMsgClear.java | 52 +++++++++++++++++++ .../cono/morny/bot/command/MornyCommands.java | 5 ++ 5 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java diff --git a/gradle.properties b/gradle.properties index 8213035..045c096 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.2 +VERSION = 0.7.0.3 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index b1abc49..da0d29d 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.7.0.2"; - public static final long COMPILE_TIMESTAMP = 1647451512009L; + public static final String VERSION = "0.7.0.3"; + public static final long COMPILE_TIMESTAMP = 1648803402848L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index 07ff390..23c7539 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -8,7 +8,7 @@ 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.model.DeleteMyCommands; +import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.request.GetMe; import javax.annotation.Nonnull; @@ -44,7 +44,13 @@ public class MornyCoeur { * 如果在登陆之前就定义了此字段,则登陆代码会验证登陆的 bot 的 username * 是否与定义的 username 符合。如果不符合则会报错。 */ - private final String username; + public final String username; + /** + * morny 的 bot 账户的 telegram id
+ *
+ * 这个字段将会在登陆成功后赋值为登录到的 bot 的 id。 + */ + public final long userid; /** * morny 的事件忽略前缀时间
*
@@ -58,7 +64,7 @@ public class MornyCoeur { */ public static final long coeurStartTimestamp = System.currentTimeMillis(); - private record LogInResult(TelegramBot account, String username) { } + private record LogInResult(TelegramBot account, String username, long userid) { } /** * 执行 bot 初始化 @@ -91,6 +97,7 @@ public class MornyCoeur { final LogInResult loginResult = login(botKey, botUsername); this.account = loginResult.account; this.username = loginResult.username; + this.userid = loginResult.userid; this.trusted = new MornyTrusted(master, trustedChat); logger.info(String.format(""" trusted param set: @@ -190,11 +197,11 @@ public class MornyCoeur { for (int i = 1; i < 4; i++) { if (i != 1) logger.info("retrying..."); try { - final String username = account.execute(new GetMe()).user().username(); - if (requireName != null && !requireName.equals(username)) - throw new RuntimeException("Required the bot @" + requireName + " but @" + username + " logged in!"); - logger.info("Succeed login to @" + username); - return new LogInResult(account, username); + final User remote = account.execute(new GetMe()).user(); + if (requireName != null && !requireName.equals(remote.username())) + throw new RuntimeException("Required the bot @" + requireName + " but @" + remote.username() + " logged in!"); + logger.info("Succeed login to @" + remote.username()); + return new LogInResult(account, remote.username(), remote.id()); } catch (Exception e) { e.printStackTrace(System.out); logger.error("login failed."); @@ -267,4 +274,6 @@ public class MornyCoeur { return INSTANCE.extraActionInstance; } + public static long getUserid () { return INSTANCE.userid; } + } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java new file mode 100644 index 0000000..dc9372f --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java @@ -0,0 +1,52 @@ +package cc.sukazyo.cono.morny.bot.command; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.untitled.util.telegram.object.InputCommand; +import com.pengrad.telegrambot.model.Chat; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.request.DeleteMessage; +import com.pengrad.telegrambot.request.GetChatMember; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class DirectMsgClear implements ISimpleCommand { + + @Nonnull @Override public String getName () { return "/r"; } + + @Nullable @Override public String[] getAliases () { return new String[0]; } + + @Override + public void execute (@Nonnull InputCommand command, @Nonnull Update event) { + + if (event.message().replyToMessage() == null) return; + if (event.message().replyToMessage().from().id() != MornyCoeur.getUserid()) return; + if (event.message().replyToMessage().date() - System.currentTimeMillis()/1000 < 48*60*60) return; + + final boolean isTrusted = MornyCoeur.trustedInstance().isTrusted(event.message().chat().id()); + + if ( + isTrusted || ( + event.message().replyToMessage().replyToMessage() != null && + event.message().replyToMessage().replyToMessage().from().id().equals(event.message().from().id()) + ) + ) { + + MornyCoeur.extra().exec(new DeleteMessage( + event.message().chat().id(), event.message().replyToMessage().messageId() + )); + if (event.message().chat().type() == Chat.Type.Private || ( + MornyCoeur.extra().exec( + new GetChatMember(event.message().chat().id(), event.message().from().id()) + ).chatMember().canDeleteMessages() + )) { + MornyCoeur.extra().exec(new DeleteMessage( + event.message().chat().id(), event.message().messageId() + )); + } + + } + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java index 69173d4..450db41 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java @@ -73,6 +73,11 @@ public class MornyCommands { new Exit() ); + // 特殊的命令 + register( + new DirectMsgClear() + ); + // 统一注册这些奇怪的东西&.& register( new 喵呜.抱抱(),