furmeet-bot/webproj/bot/tasks.py

44 lines
1.4 KiB
Python

import datetime
from background_task import background
from telegram.bot import Bot
from . import models
@background()
def captcha_kicker():
for pendingCaptchaUser in models.PendingCaptchaUser.objects.all():
expirity = pendingCaptchaUser.created + datetime.timedelta(seconds=pendingCaptchaUser.lifetime)
if expirity < datetime.datetime.now():
if (expirity+datetime.timedelta(seconds=1800)) < datetime.datetime.now():
pendingCaptchaUser.delete()
continue
bot_token = pendingCaptchaUser.bot_token
bot = Bot(token=bot_token)
try:
bot.kick_chat_member(
chat_id=pendingCaptchaUser.group.telegram_id,
user_id=pendingCaptchaUser.user.telegram_id,
)
except:
pass
try:
bot.unban_chat_member(
chat_id=pendingCaptchaUser.group.telegram_id,
user_id=pendingCaptchaUser.user.telegram_id,
)
except:
pass
try:
bot.delete_message(
chat_id=pendingCaptchaUser.group.telegram_id,
message_id=pendingCaptchaUser.captcha_message_id,
)
except:
pass
pendingCaptchaUser.delete()
captcha_kicker(schedule=1, repeat=10, repeat_until=None)