Format code to PEP8 & other small changes

This commit is contained in:
hok7z 2023-07-22 14:07:43 +03:00
parent e1d362d759
commit e316279f38
10 changed files with 56 additions and 50 deletions

View File

@ -6,8 +6,8 @@ api_hash = ""
group_id = ""
second_group_id = ""
telegram_api_server = "127.0.0.1:5326"
db_url = "sqlite://db.db"
telegram_api_server = "http://127.0.0.1:5326"
db_uri = "sqlite://db.db"
limit_of_warns = 5

View File

@ -23,7 +23,7 @@ This bot is designed to simplify the moderation and management of Telegram group
| `group_id` | group id |
| `second_group_id` | seconds group for admins |
| `telegram_bot_api_server` | telegram bot api server |
| `db_url` | connection info to database |
| `db_uri` | connection info to database |
| `limit_of_warns` | limit user warnings |
| `update_interval` | interval for update of user data |

2
app.py
View File

@ -22,7 +22,7 @@ WEBAPP_PORT = 3001
# Don`t touch anything!
WEBHOOK_HOST = f'http://{WEBAPP_HOST}:{WEBAPP_PORT}'
WEBHOOK_PATH = f'/bot{config.token}/'
WEBHOOK_PATH = f'/bot{config.bot_token}/'
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"

View File

@ -8,7 +8,7 @@ env.read_env()
USE_WEBHOOK = True
# bot token
token = env.str("bot_token")
bot_token = env.str("bot_token")
group_id = env.int("group_id")
second_group_id = env.int("second_group_id")
@ -33,7 +33,6 @@ group_permissions = {
"can_pin_messages": False
}
db_url = env.str("db_url")
db_uri = env.str("db_uri")
telegram_api_server = env.str("telegram_api_server").split(":")
telegram_api_server = f"http://{telegram_api_server[0]}:{telegram_api_server[1]}"
telegram_api_server = env.str("telegram_api_server")

View File

@ -1,20 +1,22 @@
from peewee import Field, Model, BigIntegerField, CharField, DateField, DateTimeField, ForeignKeyField
from peewee import CharField, BigIntegerField, DateField, DateTimeField, ForeignKeyField
from enum import Enum
import config
from playhouse.db_url import connect
from datetime import datetime, date
from peewee import Model
db = connect(config.db_uri)
db = connect(config.db_url)
from enum import Enum
class MemberRoles(Enum):
OWNER = "owner"
ADMIN = "admin"
HELPER = "helper"
MEMBER = "member"
class Member(Model):
user_id = BigIntegerField()
first_name = CharField()
@ -26,11 +28,11 @@ class Member(Model):
joined = DateField(default=date.today())
class Meta:
db_table = "members"
database = db
class Restriction(Model):
from_user = ForeignKeyField(Member, lazy_load=True)
to_user = ForeignKeyField(Member, lazy_load=True)

View File

@ -13,7 +13,7 @@ import utils
storage = MemoryStorage()
# Create client connection
tgc = utils.TelegramClient(config.api_id, config.api_hash, config.token)
tgc = utils.TelegramClient(config.api_id, config.api_hash, config.bot_token)
scheduler = AsyncIOScheduler()
@ -22,7 +22,7 @@ session = AiohttpSession(
)
bot = Bot(
token=config.token,
token=config.bot_token,
session=session,
parse_mode="Markdown"
)

View File

@ -1,11 +1,19 @@
import typing
from typing import Optional
from dataclasses import dataclass
from database import Member
from load import types
def get_argument(arguments:list,index:int=0) -> typing.Optional[str]:
@dataclass
class CommandArguments:
to_user: Optional[Member]
from_user: Optional[Member]
arguments: list
is_silent: bool
def get_argument(arguments: list, index: int = 0) -> Optional[str]:
""" Get element from a list.If element not exist return None """
if not (arguments):
return None
@ -15,12 +23,7 @@ def get_argument(arguments:list,index:int=0) -> typing.Optional[str]:
else:
return None
@dataclass
class CommandArguments:
to_user:Member | None
from_user:Member | None
arguments:list
is_silent:bool
async def get_command_args(message: types.Message) -> CommandArguments:
"""Describe user data and arguments from message"""

View File

@ -2,6 +2,7 @@ import config
from database import Member, Restriction
from database import db
def database_is_empty():
if not db.get_columns("members"):
db.create_tables([Member, Restriction])
@ -12,5 +13,6 @@ def database_is_empty():
return False
async def notify_started_bot(bot):
await bot.send_message(config.second_group_id, "Bot successfully launched!")

View File

@ -1,11 +1,11 @@
import re
import typing
from typing import Optional
import datetime
from load import types
def parse_timedelta(value: str) -> typing.Optional[datetime.timedelta]:
def parse_timedelta(value: str) -> Optional[datetime.timedelta]:
regex = r'(?:(\d+)(?:d|д))?(?:(\d+)(?:h|ч))?(?:(\d+)(?:m|м))?(?:(\d+)(?:s|с))?'
specification = value.strip().replace(' ', '')
match = re.fullmatch(regex, specification)
@ -19,7 +19,7 @@ def parse_timedelta(value: str) -> typing.Optional[datetime.timedelta]:
)
def parse_timedelta_from_message(message: types.Message) -> typing.Optional[datetime.timedelta]:
def parse_timedelta_from_message(message: types.Message) -> Optional[datetime.timedelta]:
_, *args = message.text.split()
if args: