From 0f304c210a0ec219091520fa34de54c37941fb9a Mon Sep 17 00:00:00 2001 From: Ayrat Hudaygulov Date: Tue, 3 Oct 2023 21:22:50 +0100 Subject: [PATCH] removed annoying warning about tasks --- src/VahterBanBot/Bot.fs | 82 +++++++++++++++------------- src/VahterBanBot/VahterBanBot.fsproj | 1 + 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/VahterBanBot/Bot.fs b/src/VahterBanBot/Bot.fs index 04cfe79..28b9007 100644 --- a/src/VahterBanBot/Bot.fs +++ b/src/VahterBanBot/Bot.fs @@ -123,48 +123,54 @@ let onUpdate // check if message is a known command // and check that user is allowed to ban others elif isBanOnReplyMessage message && isBanAuthorized botConfig message logger then + // we are creating a task here, to work around F# compiler bug + // https://www.github.com/dotnet/fsharp/issues/16068 + // otherwise we would get a warning about "This state machine is not statically compilable blah blah" + // it happens because task builder has too many bindings inside if branch + return! task { - // delete command message - let deleteCmdTask = botClient.DeleteMessageAsync(ChatId(message.Chat.Id), message.MessageId) - // delete message that was replied to - let deleteReplyTask = botClient.DeleteMessageAsync(ChatId(message.Chat.Id), message.ReplyToMessage.MessageId) - // update user in DB - let banUserInDb = - message.ReplyToMessage.From - |> DbUser.newUser - |> DbUser.banUser message.From.Id (Option.ofObj message.ReplyToMessage.Text) - |> DB.upsertUser + // delete command message + let deleteCmdTask = botClient.DeleteMessageAsync(ChatId(message.Chat.Id), message.MessageId) + // delete message that was replied to + let deleteReplyTask = botClient.DeleteMessageAsync(ChatId(message.Chat.Id), message.ReplyToMessage.MessageId) + // update user in DB + let banUserInDb = + message.ReplyToMessage.From + |> DbUser.newUser + |> DbUser.banUser message.From.Id (Option.ofObj message.ReplyToMessage.Text) + |> DB.upsertUser + + let deletedUserMessagesTask = task { + let fromUserId = message.ReplyToMessage.From.Id + + // delete all recorded messages from user in all chats + let! allUserMessages = DB.getUserMessages fromUserId + for msg in allUserMessages do + // try to delete each message separately + try + do! botClient.DeleteMessageAsync(ChatId(msg.Chat_Id), msg.Message_Id) + with e -> + logger.LogError ($"Failed to delete message {msg.Message_Id} from chat {msg.Chat_Id}", e) + + // delete recorded messages from DB + return! DB.deleteUserMessages fromUserId + } - let deletedUserMessagesTask = task { - let fromUserId = message.ReplyToMessage.From.Id + // try ban user in all monitored chats + let! banResults = banInAllChats botConfig botClient message.ReplyToMessage.From.Id + let! deletedUserMessages = deletedUserMessagesTask - // delete all recorded messages from user in all chats - let! allUserMessages = DB.getUserMessages fromUserId - for msg in allUserMessages do - // try to delete each message separately - try - do! botClient.DeleteMessageAsync(ChatId(msg.Chat_Id), msg.Message_Id) - with e -> - logger.LogError ($"Failed to delete message {msg.Message_Id} from chat {msg.Chat_Id}", e) - - // delete recorded messages from DB - return! DB.deleteUserMessages fromUserId - } - - // try ban user in all monitored chats - let! banResults = banInAllChats botConfig botClient message.ReplyToMessage.From.Id - let! deletedUserMessages = deletedUserMessagesTask - - // produce aggregated log message - let logMsg = aggregateBanResultInLogMsg logger message deletedUserMessages banResults + // produce aggregated log message + let logMsg = aggregateBanResultInLogMsg logger message deletedUserMessages banResults - // log both to logger and to logs channel - let! _ = botClient.SendTextMessageAsync(ChatId(botConfig.LogsChannelId), logMsg) - logger.LogInformation logMsg - - let! _ = banUserInDb - do! deleteCmdTask - do! deleteReplyTask + // log both to logger and to logs channel + let! _ = botClient.SendTextMessageAsync(ChatId(botConfig.LogsChannelId), logMsg) + logger.LogInformation logMsg + + let! _ = banUserInDb + do! deleteCmdTask + do! deleteReplyTask + } // ping command for testing that bot works and you can talk to it elif isPingCommand message && isMessageFromAdmin botConfig message then diff --git a/src/VahterBanBot/VahterBanBot.fsproj b/src/VahterBanBot/VahterBanBot.fsproj index 1781374..0093d98 100644 --- a/src/VahterBanBot/VahterBanBot.fsproj +++ b/src/VahterBanBot/VahterBanBot.fsproj @@ -3,6 +3,7 @@ Exe net7.0 + true