From acd543ae4982164f49a06b9c44853ce39904e492 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Fri, 17 May 2024 00:38:40 +0200 Subject: [PATCH] Fix sender id --- .../languageblockbot/LanguageBlockBot.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/it/cavallium/languageblockbot/LanguageBlockBot.java b/src/main/java/it/cavallium/languageblockbot/LanguageBlockBot.java index a0a4c15..bf525e8 100644 --- a/src/main/java/it/cavallium/languageblockbot/LanguageBlockBot.java +++ b/src/main/java/it/cavallium/languageblockbot/LanguageBlockBot.java @@ -13,6 +13,7 @@ import java.util.stream.Collectors; import org.telegram.telegrambots.abilitybots.api.bot.AbilityBot; import org.telegram.telegrambots.meta.api.methods.groupadministration.GetChatAdministrators; import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage; +import org.telegram.telegrambots.meta.api.objects.Chat; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.chatmember.ChatMember; import org.telegram.telegrambots.meta.api.objects.message.Message; @@ -99,20 +100,26 @@ public class LanguageBlockBot extends AbilityBot { private CompletableFuture isMessageDeletable(Message msg) { var chatId = msg.getChatId(); CompletableFuture canDeleteBecauseItsNotAdmin; - if (msg.getSenderChat() != null) { - canDeleteBecauseItsNotAdmin = getChatAdmins(chatId) - .thenApply(admins -> !admins.contains(msg.getSenderChat().getId())); - } else { - canDeleteBecauseItsNotAdmin = CompletableFuture.completedFuture(true); - } + canDeleteBecauseItsNotAdmin = getChatAdmins(chatId) + .thenApply(admins -> !admins.contains(getSenderChat(msg))); boolean canDeleteBecauseItsNormalMessage = !(msg.getIsAutomaticForward() != null && msg.getIsAutomaticForward()) - && (msg.getSenderChat() != null && msg.getSenderChat().isUserChat()) && !msg.hasViaBot(); + && (getSenderChat(msg) > 0) && !msg.hasViaBot(); return CompletableFuture .completedFuture(canDeleteBecauseItsNormalMessage) .thenCombine(canDeleteBecauseItsNotAdmin, (a, b) -> a && b); } + private Long getSenderChat(Message msg) { + if (msg.getSenderChat() != null) { + return msg.getSenderChat().getId(); + } else if (msg.getFrom() != null) { + return msg.getFrom().getId(); + } else { + return 0L; + } + } + private CompletableFuture> getChatAdmins(Long chatId) { var client = this.getTelegramClient(); if (chatId >= 0) {