Skip to content

Commit

Permalink
less verbose happy path
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Oct 3, 2023
1 parent 0f304c2 commit d3d9ee6
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/VahterBanBot/Bot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,35 @@ let aggregateBanResultInLogMsg

let vahterUserId = message.From.Id
let vahterUsername = message.From.Username
let chatName = message.Chat.Username
let chatId = message.Chat.Id

let targetUserId = message.ReplyToMessage.From.Id
let targetUsername = message.ReplyToMessage.From.Username
let logMsgBuilder = StringBuilder()
%logMsgBuilder.AppendLine($"Vahter {prependUsername vahterUsername}({vahterUserId}) banned {prependUsername targetUsername} ({targetUserId})")
%logMsgBuilder.AppendLine($"Deleted {deletedUserMessages} messages in chats:")
%logMsgBuilder.Append($"Vahter {prependUsername vahterUsername}({vahterUserId}) banned {prependUsername targetUsername} ({targetUserId}) in chat ${chatName}({chatId})")

// we don't want to spam logs channel if all is good
let allChatsOk = banResults |> Array.forall Result.isOk
if allChatsOk then
%logMsgBuilder.AppendLine " in all chats"
logMsgBuilder.AppendLine $"Deleted {deletedUserMessages} messages"
|> string
else

%logMsgBuilder.AppendLine ""
%logMsgBuilder.AppendLine $"Deleted {deletedUserMessages} messages in chats:"

(logMsgBuilder, banResults)
||> Array.fold (fun (sb: StringBuilder) result ->
match result with
| Ok (chatUsername, chatId) ->
sb.AppendLine($"{prependUsername chatUsername} ({chatId}) - OK")
| Error (chatUsername, chatId, e) ->
logger.LogError($"Failed to ban user {prependUsername targetUsername} ({targetUserId}) in chat {prependUsername chatUsername} ({chatId})", e)
sb.AppendLine($"{prependUsername chatUsername} ({chatId}) - FAILED. {e.Message}")
)
|> string
(logMsgBuilder, banResults)
||> Array.fold (fun (sb: StringBuilder) result ->
match result with
| Ok (chatUsername, chatId) ->
sb.AppendLine($"{prependUsername chatUsername} ({chatId}) - OK")
| Error (chatUsername, chatId, e) ->
logger.LogError($"Failed to ban user {prependUsername targetUsername} ({targetUserId}) in chat {prependUsername chatUsername} ({chatId})", e)
sb.AppendLine($"{prependUsername chatUsername} ({chatId}) - FAILED. {e.Message}")
)
|> string

let onUpdate
(botClient: ITelegramBotClient)
Expand Down

0 comments on commit d3d9ee6

Please sign in to comment.