1
0
Fork 0
This commit is contained in:
Trueold89 2024-09-04 22:57:58 +03:00
parent 94ccb3f376
commit 03ac89574a
5 changed files with 20 additions and 15 deletions

View File

@ -6,6 +6,8 @@ from aiogram.filters import Command
from aiogram.types import Message
from u232ping.db import Sqlite
from u232ping.controller import Controller
import logging
import sys
TOKEN = environ["BOT_TOKEN"]
dp = Dispatcher()
@ -14,19 +16,23 @@ c = Controller(Sqlite())
@dp.message(Command("all"))
async def all(message: Message) -> None:
await c.all(message)
group = await c.all()
await message.answer(str(group))
@dp.message(Command("1"))
async def first(message: Message) -> None:
await c.first(message)
group = await c.first()
await message.answer(str(group))
@dp.message(Command("2"))
async def second(message: Message) -> None:
await c.second(message)
group = await c.second()
await message.answer(str(group))
async def main() -> None:
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
await dp.start_polling(bot)

View File

@ -1,6 +1,5 @@
from u232ping.abc.db import DataBase
from u232ping.types import Group
from aiogram.types import Message
class Controller(object):
@ -9,19 +8,19 @@ class Controller(object):
def __init__(self, db_implementation: DataBase) -> None:
self.db = db_implementation
async def _ping(self, msg: Message, group: int | None = None) -> None:
async def _ping(self, group: int | None = None) -> Group:
gtp: Group
if group is None:
gtp = await self.db.get_all()
else:
gtp = await self.db.get_group(group)
msg.answer(str(gtp))
return gtp
async def all(self, msg: Message) -> None:
await self._ping(msg)
async def all(self) -> Group:
return await self._ping()
async def first(self, msg: Message) -> None:
await self._ping(msg, 1)
async def first(self) -> Group:
return await self._ping(1)
async def second(self, msg: Message) -> None:
await self._ping(msg, 2)
async def second(self) -> Group:
return await self._ping(2)

View File

@ -13,12 +13,12 @@ class Sqlite(DataBase):
async def _get(self, group: int | None = None) -> Group:
req = "SELECT telegram FROM u232"
if group is not None:
where = f" WHERE group = {group}"
req += where
req += f" WHERE sub = '{group}'"
async with sq(self.path) as db:
cursor = await db.cursor()
await cursor.execute(req)
fetch = await cursor.fetchall()
fetch = tuple(map(lambda i: i[0], fetch))
return Group(fetch)
async def get_group(self, group_idx: int):

View File

@ -9,4 +9,4 @@ class Group(object):
def __str__(self) -> str:
lst = tuple(map(lambda member: f"@{member}", self.members))
return "/n".join(lst)
return "\n".join(lst)