Skip to content

Commit

Permalink
better ban msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Sep 28, 2023
1 parent aa63cea commit d193cff
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/VahterBanBot/Bot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ let aggregateBanResultInLogMsg
let targetUserId = message.ReplyToMessage.From.Id
let targetUsername = message.ReplyToMessage.From.Username
let logMsgBuilder = StringBuilder()
%logMsgBuilder.AppendLine($"Vahter {vahterUsername}({vahterUserId}) banned {targetUsername} ({targetUserId})")
%logMsgBuilder.AppendLine($"Vahter {prependUsername vahterUsername}({vahterUserId}) banned {prependUsername targetUsername} ({targetUserId})")
%logMsgBuilder.AppendLine($"Deleted {deletedUserMessages} messages in chats:")

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

Expand Down
2 changes: 0 additions & 2 deletions src/VahterBanBot/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ let server = app.RunAsync()
let telegramClient = app.Services.GetRequiredService<ITelegramBotClient>()

let startLogMsg =
let prependUsername (s: string) =
if s.StartsWith("@") then s else "@" + s
let sb = System.Text.StringBuilder()
%sb.AppendLine("Bot started with following configuration")
%sb.AppendLine("AllowedUsers:")
Expand Down
7 changes: 7 additions & 0 deletions src/VahterBanBot/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ let getEnv name =
let getEnvOr name defaultValue =
let value = Environment.GetEnvironmentVariable name
if value = null then defaultValue else value

let prependUsername (s: string) =
if isNull s then
null
elif s.StartsWith "@" then
s
else "@" + s

0 comments on commit d193cff

Please sign in to comment.