Skip to content

Commit

Permalink
removed annoying warning about tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Oct 3, 2023
1 parent ae1d9c5 commit 0f304c2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
82 changes: 44 additions & 38 deletions src/VahterBanBot/Bot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/VahterBanBot/VahterBanBot.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<WarningsAsErrors>true</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 0f304c2

Please sign in to comment.