Skip to content

Commit

Permalink
now totally deletes command message
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Nov 29, 2023
1 parent 051edcc commit 67ba7f9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
46 changes: 24 additions & 22 deletions src/VahterBanBot/Bot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ let isMessageFromAdmin (botConfig: BotConfiguration) (message: Message) =

let isBannedPersonAdmin (botConfig: BotConfiguration) (message: Message) =
botConfig.AllowedUsers.ContainsValue message.ReplyToMessage.From.Id

let isKnownCommand (message: Message) =
isPingCommand message ||
isBanOnReplyMessage message

let isBanAuthorized (botConfig: BotConfiguration) (message: Message) (logger: ILogger) =
let fromUserId = message.From.Id
Expand Down Expand Up @@ -126,11 +130,6 @@ let banOnReply
.SetTag("vahterId", message.From.Id)
.SetTag("vahterUsername", message.From.Username)

// delete command message
let deleteCmdTask =
botClient.DeleteMessageAsync(ChatId(message.Chat.Id), message.MessageId)
|> safeTaskAwait (fun e -> logger.LogError ($"Failed to delete command message {message.MessageId} from chat {message.Chat.Id}", e))

// delete message that was replied to
let deleteReplyTask =
botClient.DeleteMessageAsync(ChatId(message.Chat.Id), message.ReplyToMessage.MessageId)
Expand All @@ -148,7 +147,7 @@ let banOnReply
let! allUserMessages = DB.getUserMessages fromUserId

// delete all recorded messages from user in all chats
let! _ =
do!
allUserMessages
|> Seq.map (fun msg -> task {
try
Expand All @@ -157,6 +156,7 @@ let banOnReply
logger.LogError ($"Failed to delete message {msg.Message_Id} from chat {msg.Chat_Id}", e)
})
|> Task.WhenAll
|> taskIgnore

// delete recorded messages from DB
return! DB.deleteUserMessages fromUserId
Expand All @@ -170,11 +170,10 @@ let banOnReply
let logMsg = aggregateBanResultInLogMsg logger message deletedUserMessages banResults

// log both to logger and to logs channel
let! _ = botClient.SendTextMessageAsync(ChatId(botConfig.LogsChannelId), logMsg)
do! botClient.SendTextMessageAsync(ChatId(botConfig.LogsChannelId), logMsg) |> taskIgnore
logger.LogInformation logMsg

let! _ = banUserInDb
do! deleteCmdTask
do! banUserInDb.Ignore()
do! deleteReplyTask
}

Expand All @@ -190,7 +189,7 @@ let onUpdate
else

// upserting user to DB
let! _ =
let upsertUserTask =
DbUser.newUser message.From
|> DB.upsertUser

Expand All @@ -206,25 +205,28 @@ let onUpdate
"[unknown]"
logger.LogInformation $"Deleted message from channel {probablyChannelName}"

// check if message is a known command
// and check that user is allowed to ban others
elif isBanOnReplyMessage message && isBanAuthorized botConfig message logger then
do! banOnReply botClient botConfig message logger

// ping command for testing that bot works and you can talk to it
elif isPingCommand message && isMessageFromAdmin botConfig message then
// check if message is a known command from authorized user
elif isKnownCommand message && isMessageFromAdmin botConfig message then
// delete command message
let deleteCmdTask =
botClient.DeleteMessageAsync(ChatId(message.Chat.Id), message.MessageId)
|> safeTaskAwait (fun e -> logger.LogError ($"Failed to delete ping message {message.MessageId} from chat {message.Chat.Id}", e))
let! _ = botClient.SendTextMessageAsync(ChatId(message.Chat.Id), "pong")

// check that user is allowed to ban others
if isBanOnReplyMessage message && isBanAuthorized botConfig message logger then
do! banOnReply botClient botConfig message logger

// ping command for testing that bot works and you can talk to it
elif isPingCommand message then
do! botClient.SendTextMessageAsync(ChatId(message.Chat.Id), "pong") |> taskIgnore
do! deleteCmdTask
// if message is not a command, just save it ID to DB

// if message is not a command from authorized user, just save it ID to DB
else
let! _ =
do!
message
|> DbMessage.newMessage
|> DB.insertMessage
()
|> taskIgnore
do! taskIgnore upsertUserTask
}
12 changes: 6 additions & 6 deletions src/VahterBanBot/Cleanup.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ type CleanupService(
%sb.AppendLine(string vahterStats)

let msg = sb.ToString()
let! _ = telegramClient.SendTextMessageAsync(
ChatId(botConf.LogsChannelId),
msg
)
do! telegramClient.SendTextMessageAsync(
ChatId(botConf.LogsChannelId),
msg
) |> taskIgnore
logger.LogInformation msg
}

interface IHostedService with
member this.StartAsync(cancellationToken) =
member this.StartAsync _ =
if not botConf.IgnoreSideEffects then
timer <- new Timer(TimerCallback(cleanup >> ignore), null, TimeSpan.Zero, cleanupInterval)
Task.CompletedTask

member this.StopAsync(cancellationToken) =
member this.StopAsync _ =
match timer with
| null -> Task.CompletedTask
| timer -> timer.DisposeAsync().AsTask()
6 changes: 6 additions & 0 deletions src/VahterBanBot/Utils.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module VahterBanBot.Utils

open System
open System.Threading.Tasks

let inline (~%) x = ignore x

Expand Down Expand Up @@ -44,3 +45,8 @@ let timeSpanAsHumanReadable (ts: TimeSpan) =
pluralize ts.TotalHours "hour"
else
pluralize ts.TotalDays "day"

type Task<'x> with
member this.Ignore() = task { let! _ = this in () }

let inline taskIgnore (t: Task<'x>) = t.Ignore()

0 comments on commit 67ba7f9

Please sign in to comment.