60 lines
1.5 KiB
V
60 lines
1.5 KiB
V
module bot_manager
|
||
|
||
import repository
|
||
import tg_handle
|
||
import models
|
||
|
||
pub struct BotManager {
|
||
pub mut:
|
||
active_bots map[i64]&tg_handle.TgBot
|
||
repo repository.Repo
|
||
}
|
||
|
||
pub fn new_bot_manager(repo repository.Repo) BotManager {
|
||
return BotManager{
|
||
active_bots: map[i64]&tg_handle.TgBot{}
|
||
repo: repo
|
||
}
|
||
}
|
||
|
||
pub fn (mut bm BotManager) manage_bots(new_bots map[i64]models.TelegramIntegration) ! {
|
||
// перебираем переданную мапку, сраавниваем то что работает с тем что пришло, дублей быть не должно
|
||
for bot_id, bot_data in new_bots {
|
||
if bot_id !in bm.active_bots {
|
||
bm.start_bot(bot_data)!
|
||
}
|
||
}
|
||
}
|
||
|
||
fn (mut bm BotManager) start_bot(bot models.TelegramIntegration) ! {
|
||
// доп проверка ну а вдруг
|
||
if bot.id in bm.active_bots {
|
||
eprintln('already started: $bot.id')
|
||
return
|
||
}
|
||
tg_bot := tg_handle.new_tg_bot( bm.repo,bot)
|
||
bm.active_bots[bot.id] = tg_bot
|
||
println('started: $bot.id')
|
||
}
|
||
|
||
pub fn (mut bm BotManager)deleted_bot(bots []models.TelegramIntegration)!{
|
||
for bot in bots {
|
||
if bot.id in bm.active_bots {
|
||
handle := bm.active_bots[bot.id]or {
|
||
println('Error get bot handle for: $bot.id')
|
||
continue
|
||
}
|
||
handle.stop_ch <- 1
|
||
bm.active_bots.delete(bot.id)
|
||
println('stopped and deleted: $bot.id')
|
||
} else {
|
||
println('bot not found: $bot.id')
|
||
}
|
||
}
|
||
}
|
||
|
||
pub fn (mut bm BotManager) stop_all_bots() {
|
||
for _,bot in bm.active_bots{
|
||
bot.stop_ch <-1
|
||
}
|
||
} |