添加请求删除 bot 回复的功能

This commit is contained in:
A.C.Sukazyo Eyre 2022-04-01 16:59:51 +08:00
parent 653ba4c0a8
commit 6dfaade55c
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
5 changed files with 77 additions and 11 deletions

View File

@ -1,6 +1,6 @@
## Core ## Core
VERSION = 0.7.0.2 VERSION = 0.7.0.3
# dependencies # dependencies

View File

@ -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.7.0.2"; public static final String VERSION = "0.7.0.3";
public static final long COMPILE_TIMESTAMP = 1647451512009L; public static final long COMPILE_TIMESTAMP = 1648803402848L;
} }

View File

@ -8,7 +8,7 @@ import cc.sukazyo.cono.morny.data.tracker.TrackerDataManager;
import cc.sukazyo.untitled.telegram.api.extra.ExtraAction; import cc.sukazyo.untitled.telegram.api.extra.ExtraAction;
import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.DeleteMyCommands; import com.pengrad.telegrambot.model.User;
import com.pengrad.telegrambot.request.GetMe; import com.pengrad.telegrambot.request.GetMe;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -44,7 +44,13 @@ public class MornyCoeur {
* 如果在登陆之前就定义了此字段则登陆代码会验证登陆的 bot username * 如果在登陆之前就定义了此字段则登陆代码会验证登陆的 bot username
* 是否与定义的 username 符合如果不符合则会报错 * 是否与定义的 username 符合如果不符合则会报错
*/ */
private final String username; public final String username;
/**
* morny bot 账户的 telegram id<br>
* <br>
* 这个字段将会在登陆成功后赋值为登录到的 bot id
*/
public final long userid;
/** /**
* morny 的事件忽略前缀时间<br> * morny 的事件忽略前缀时间<br>
* <br> * <br>
@ -58,7 +64,7 @@ public class MornyCoeur {
*/ */
public static final long coeurStartTimestamp = System.currentTimeMillis(); public static final long coeurStartTimestamp = System.currentTimeMillis();
private record LogInResult(TelegramBot account, String username) { } private record LogInResult(TelegramBot account, String username, long userid) { }
/** /**
* 执行 bot 初始化 * 执行 bot 初始化
@ -91,6 +97,7 @@ public class MornyCoeur {
final LogInResult loginResult = login(botKey, botUsername); final LogInResult loginResult = login(botKey, botUsername);
this.account = loginResult.account; this.account = loginResult.account;
this.username = loginResult.username; this.username = loginResult.username;
this.userid = loginResult.userid;
this.trusted = new MornyTrusted(master, trustedChat); this.trusted = new MornyTrusted(master, trustedChat);
logger.info(String.format(""" logger.info(String.format("""
trusted param set: trusted param set:
@ -190,11 +197,11 @@ public class MornyCoeur {
for (int i = 1; i < 4; i++) { for (int i = 1; i < 4; i++) {
if (i != 1) logger.info("retrying..."); if (i != 1) logger.info("retrying...");
try { try {
final String username = account.execute(new GetMe()).user().username(); final User remote = account.execute(new GetMe()).user();
if (requireName != null && !requireName.equals(username)) if (requireName != null && !requireName.equals(remote.username()))
throw new RuntimeException("Required the bot @" + requireName + " but @" + username + " logged in!"); throw new RuntimeException("Required the bot @" + requireName + " but @" + remote.username() + " logged in!");
logger.info("Succeed login to @" + username); logger.info("Succeed login to @" + remote.username());
return new LogInResult(account, username); return new LogInResult(account, remote.username(), remote.id());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(System.out); e.printStackTrace(System.out);
logger.error("login failed."); logger.error("login failed.");
@ -267,4 +274,6 @@ public class MornyCoeur {
return INSTANCE.extraActionInstance; return INSTANCE.extraActionInstance;
} }
public static long getUserid () { return INSTANCE.userid; }
} }

View File

@ -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()
));
}
}
}
}

View File

@ -73,6 +73,11 @@ public class MornyCommands {
new Exit() new Exit()
); );
// 特殊的命令
register(
new DirectMsgClear()
);
// 统一注册这些奇怪的东西&.& // 统一注册这些奇怪的东西&.&
register( register(
new 喵呜.抱抱(), new 喵呜.抱抱(),