Added task scheduling

This commit is contained in:
hok7z 2023-01-04 22:14:32 +02:00
parent 97b851db72
commit ecc781eadb
8 changed files with 58 additions and 47 deletions

View File

@ -1,12 +1,14 @@
bot_token = "" bot_token = ""
limit_of_warns = 5
api_id = "" api_id = ""
api_hash = "" api_hash = ""
group_id = "" group_id = ""
second_group_id = "" second_group_id = ""
db_url = "sqlite://db.db"
telegram_api_server = "127.0.0.1:5326" telegram_api_server = "127.0.0.1:5326"
db_url = "sqlite://db.db"
limit_of_warns = 5
update_interval = 3600

14
app.py
View File

@ -3,7 +3,8 @@ import logging
from aiogram import executor from aiogram import executor
from database import db, Member, Restriction from database import db, Member, Restriction
from load import dp, bot from load import dp, bot, scheduler
import filters import filters
dp.filters_factory.bind(filters.AvaibleRolesFilter) dp.filters_factory.bind(filters.AvaibleRolesFilter)
@ -39,9 +40,14 @@ async def on_startup(dp):
from utils.default_commands import set_default_commands from utils.default_commands import set_default_commands
await set_default_commands(dp) await set_default_commands(dp)
# Reloading users data
from utils import reload_users_data
scheduler.add_job(reload_users_data,"interval", seconds=config.update_interval)
scheduler.start()
from load import tgc from load import tgc
await tgc.client.start() await tgc.client.start()
await bot.set_webhook(WEBHOOK_URL) await bot.set_webhook(WEBHOOK_URL)
async def on_shutdown(dp): async def on_shutdown(dp):
@ -61,11 +67,11 @@ def main() -> None:
on_shutdown=on_shutdown, on_shutdown=on_shutdown,
skip_updates=True, skip_updates=True,
host=WEBAPP_HOST, host=WEBAPP_HOST,
port=WEBAPP_PORT, port=WEBAPP_PORT
) )
else: else:
executor.start_polling(dp,skip_updates=True) executor.start_polling(dp,skip_updates=True)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -18,6 +18,9 @@ limit_of_warns = 5
api_id = env.int("api_id") api_id = env.int("api_id")
api_hash = env.str("api_hash") api_hash = env.str("api_hash")
# Data update interval
update_interval = env.int("update_interval")
group_permissions = { group_permissions = {
"can_send_messages":True, "can_send_messages":True,
"can_send_media_messages":False, "can_send_media_messages":False,

View File

@ -124,8 +124,6 @@ async def kick_user(message: types.Message):
if (not status1 and status2) and (not command.is_silent): if (not status1 and status2) and (not command.is_silent):
await message.answer(f"[{from_user.first_name}](tg://user?id={from_user.user_id}) has kicked [{to_user.first_name}](tg://user?id={to_user.user_id})",parse_mode="Markdown") await message.answer(f"[{from_user.first_name}](tg://user?id={from_user.user_id}) has kicked [{to_user.first_name}](tg://user?id={to_user.user_id})",parse_mode="Markdown")
Restriction.create( Restriction.create(
from_user=from_user, from_user=from_user,
to_user=to_user, to_user=to_user,
@ -167,7 +165,6 @@ async def mute_user(message:types.Message):
if status and (not command.is_silent): if status and (not command.is_silent):
await message.answer(f"[{from_user.first_name}](tg://user?id={from_user.user_id}) has muted [{to_user.first_name}](tg://user?id={to_user.user_id})",parse_mode="Markdown") await message.answer(f"[{from_user.first_name}](tg://user?id={from_user.user_id}) has muted [{to_user.first_name}](tg://user?id={to_user.user_id})",parse_mode="Markdown")
Restriction.create( Restriction.create(
from_user=from_user, from_user=from_user,
to_user=to_user, to_user=to_user,
@ -290,7 +287,6 @@ async def warn_user(message: types.Message):
await message.answer(f"[{to_user.first_name}](tg://user?id={to_user.user_id}) has been banned!",parse_mode="Markdown") await message.answer(f"[{to_user.first_name}](tg://user?id={to_user.user_id}) has been banned!",parse_mode="Markdown")
await bot.kick_chat_member(chat_id=message.chat.id, user_id=to_user.user_id, until_date=None) await bot.kick_chat_member(chat_id=message.chat.id, user_id=to_user.user_id, until_date=None)
Restriction.create( Restriction.create(
from_user=from_user, from_user=from_user,
to_user=to_user, to_user=to_user,
@ -304,44 +300,11 @@ async def warn_user(message: types.Message):
commands_prefix="!" commands_prefix="!"
) )
async def reload(message: types.Message): async def reload(message: types.Message):
from load import tgc from utils import reload_users_data
await reload_users_data()
owner_exists = Member.get_or_none(Member.role == "owner")
if (not owner_exists):
Member.create(
user_id = message.from_user.id,
first_name = message.from_user.first_name,
username = message.from_user.username,
role = "owner",
)
# TODO: do this every 1 hours
members = await tgc.members_list(config.group_id)
for member in members:
user = Member.get_or_none(Member.user_id == member["id"])
if (not user):
Member.create(
user_id = member["id"],
first_name = member["first_name"],
username = member["username"],
)
else:
user.first_name = member["first_name"]
user.username = member["username"]
user.save()
group = await bot.get_chat(message.chat.id)
group_permissions = dict(group["permissions"])
for permission in group_permissions.keys():
config.group_permissions[permission] = group_permissions[permission]
await message.answer("Reloaded!") await message.answer("Reloaded!")
@dp.message_handler( @dp.message_handler(
commands=["setrole"], commands=["setrole"],
commands_prefix="!", commands_prefix="!",

View File

@ -2,15 +2,19 @@ from aiogram import Bot, Dispatcher
from aiogram import types from aiogram import types
from aiogram.bot.api import TelegramAPIServer from aiogram.bot.api import TelegramAPIServer
from aiogram.contrib.fsm_storage.memory import MemoryStorage from aiogram.contrib.fsm_storage.memory import MemoryStorage
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import config import config
import utils import utils
storage = MemoryStorage() storage = MemoryStorage()
# Create client connection # Create client connection
tgc = utils.TelegramClient(config.api_id, config.api_hash, config.token) tgc = utils.TelegramClient(config.api_id, config.api_hash, config.token)
scheduler = AsyncIOScheduler()
bot = Bot( bot = Bot(
token=config.token, token=config.token,
server=TelegramAPIServer.from_base(config.telegram_api_server), server=TelegramAPIServer.from_base(config.telegram_api_server),

View File

@ -1,5 +1,6 @@
aiogram==2.23.1 aiogram==2.24
Pyrogram==2.0.62 Pyrogram==2.0.62
peewee==3.15.4 peewee==3.15.4
environs==9.5.0 environs==9.5.0
TgCrypto==1.2.5 TgCrypto==1.2.5
APScheduler==3.9.1.post1

View File

@ -2,6 +2,7 @@ from .notify_start import notify_started_bot
from .default_commands import set_default_commands from .default_commands import set_default_commands
from .telegram_client import TelegramClient from .telegram_client import TelegramClient
from .reload_users import reload_users_data
from .parse_timedelta import parse_timedelta from .parse_timedelta import parse_timedelta
from .command_parser import get_argument, get_command_args from .command_parser import get_argument, get_command_args

31
utils/reload_users.py Normal file
View File

@ -0,0 +1,31 @@
async def reload_users_data():
import config
from database import Member
from load import tgc, bot
members = await tgc.members_list(config.group_id)
for member in members:
user = Member.get_or_none(Member.user_id == member["id"])
first_name = member["first_name"]
if (not first_name):
first_name = "NULL"
if (not user):
Member.create(
user_id = member["id"],
first_name = first_name,
username = member["username"],
)
else:
user.first_name = first_name
user.username = member["username"]
user.save()
group = await bot.get_chat(config.group_id)
group_permissions = dict(group["permissions"])
for permission in group_permissions.keys():
config.group_permissions[permission] = group_permissions[permission]