-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
local admins in chats won't trigger ML deletion
- Loading branch information
Showing
10 changed files
with
150 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module VahterBanBot.UpdateChatAdmins | ||
|
||
open System.Collections.Generic | ||
open System.Text | ||
open System.Threading.Tasks | ||
open Microsoft.Extensions.Logging | ||
open Telegram.Bot | ||
open Telegram.Bot.Types | ||
open VahterBanBot.Types | ||
open VahterBanBot.Utils | ||
open System | ||
open System.Threading | ||
open Microsoft.Extensions.Hosting | ||
|
||
type UpdateChatAdmins( | ||
logger: ILogger<UpdateChatAdmins>, | ||
telegramClient: ITelegramBotClient, | ||
botConf: BotConfiguration | ||
) = | ||
let mutable timer: Timer = null | ||
static let mutable localAdmins: ISet<int64> = HashSet<int64>() | ||
|
||
let updateChatAdmins _ = task { | ||
let sb = StringBuilder() | ||
%sb.AppendLine("New chat admins:") | ||
let result = HashSet<int64>() | ||
for chatId in botConf.ChatsToMonitor.Values do | ||
let! admins = telegramClient.GetChatAdministratorsAsync(ChatId chatId) | ||
|
||
// wait a bit so we don't get rate limited | ||
do! Task.Delay 100 | ||
|
||
for admin in admins do | ||
%result.Add admin.User.Id | ||
%sb.AppendJoin(",", $"{prependUsername admin.User.Username} ({admin.User.Id})") | ||
localAdmins <- result | ||
logger.LogInformation (sb.ToString()) | ||
} | ||
|
||
static member Admins = localAdmins | ||
|
||
interface IHostedService with | ||
member this.StartAsync _ = | ||
if not botConf.IgnoreSideEffects && botConf.UpdateChatAdmins then | ||
if botConf.UpdateChatAdminsInterval.IsSome then | ||
// recurring | ||
timer <- new Timer(TimerCallback(updateChatAdmins >> ignore), null, TimeSpan.Zero, botConf.UpdateChatAdminsInterval.Value) | ||
Task.CompletedTask | ||
else | ||
// once | ||
updateChatAdmins() | ||
else | ||
Task.CompletedTask | ||
|
||
member this.StopAsync _ = | ||
match timer with | ||
| null -> Task.CompletedTask | ||
| timer -> timer.DisposeAsync().AsTask() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters