hugim_bot/main.py

114 lines
3.1 KiB
Python
Raw Permalink Normal View History

2024-12-11 12:32:06 +02:00
import asyncio
import logging
import sys
from os import getenv
from aiogram import Bot, Dispatcher, html
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart, Command
from aiogram.types import Message
import sqlite3
from dotenv import load_dotenv
load_dotenv()
# Bot token can be obtained via https://t.me/BotFather
TOKEN = getenv("BOT_TOKEN")
# All handlers should be attached to the Router (or Dispatcher)
dp = Dispatcher()
@dp.message(CommandStart())
async def command_start_handler(message: Message) -> None:
"""
This handler receives messages with `/start` command
"""
# Most event objects have aliases for API methods that can be called in events' context
# For example if you want to answer to incoming message you can use `message.answer(...)` alias
# and the target chat will be passed to :ref:`aiogram.methods.send_message.SendMessage`
# method automatically or call API method directly via
# Bot instance: `bot.send_message(chat_id=message.chat.id, ...)`
await message.answer(f"Hello, {html.bold(message.from_user.full_name)}!")
@dp.message(Command("add_admin"))
async def command_add_admin_handler(message: Message) -> None:
"""
This handles adding new admins
# TODO: add a state
"""
con = sqlite3.connect("admins.db")
id = message.from_user.id
username = message.from_user.first_name
try:
con.execute("""
INSERT INTO admins VALUES
(?, ?);
""", (id, username))
con.commit()
except Exception as err:
print(err)
print("addmint added")
await message.answer(f"yoooo, added an admin lmao")
@dp.message(Command("am_i_admin"))
async def command_check_admin_handler(message: Message) -> None:
con = sqlite3.connect("admins.db")
user_id = message.from_user.id
check_id = con.execute("SELECT id FROM admins where id=?", (user_id,))
if check_id.fetchone() != None:
await message.answer(f"yes you are, nigga")
else:
await message.answer(f"no, lolz")
@dp.message()
async def echo_handler(message: Message) -> None:
"""
Handler will forward receive a message back to the sender
By default, message handler will handle all message types (like a text, photo, sticker etc.)
"""
try:
# Send a copy of the received message
await message.send_copy(chat_id=message.chat.id)
except TypeError:
# But not all the types is supported to be copied so need to handle it
await message.answer("Nice try!")
async def main() -> None:
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
con = sqlite3.connect("admins.db")
con.execute("""CREATE TABLE IF NOT EXISTS admins (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);""")
con.commit()
# And the run events dispatching
await dp.start_polling(bot)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())