1
0
Fork 0

Avoid unknown id error and some little optimisation

This commit is contained in:
Ethanell 2019-12-25 22:10:30 +01:00
parent 1e3fcfd8a0
commit 6f6ac44170

81
bot.py
View file

@ -12,7 +12,8 @@ from os.path import isdir, isfile
from threading import RLock from threading import RLock
from aiogram import Bot, Dispatcher, executor, types from aiogram import Bot, Dispatcher, executor, types
from aiogram.types import InlineQuery, InputTextMessageContent, InlineKeyboardMarkup, InlineKeyboardButton, InlineQueryResultArticle, ParseMode, reply_keyboard, ContentType from aiogram.types import InlineQuery, InputTextMessageContent, InlineKeyboardMarkup, InlineKeyboardButton,\
InlineQueryResultArticle, ParseMode, reply_keyboard, ContentType
from aiogram.utils import markdown from aiogram.utils import markdown
from aiogram.utils.callback_data import CallbackData from aiogram.utils.callback_data import CallbackData
from aiogram.utils.exceptions import MessageIsTooLong from aiogram.utils.exceptions import MessageIsTooLong
@ -59,20 +60,39 @@ if tables:
dbL = RLock() dbL = RLock()
re_url = re.compile(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")
key = reply_keyboard.ReplyKeyboardMarkup()
for k in ["Edt", "Kfet", "Setkfet", "Setedt", "Notif", "Settomuss"]:
key.add(reply_keyboard.KeyboardButton(k))
def get_now(): def get_now():
return datetime.datetime.now(datetime.timezone.utc).astimezone(tz=None) return datetime.datetime.now(datetime.timezone.utc).astimezone(tz=None)
def check_id(user: types.User):
with dbL:
if user.id not in session.query(User.id).all():
logger.info(f"{user.username} add to the db")
if user.locale and user.locale.language:
lg = user.locale.language
else:
lg = ""
session.add(User(id=user.id, language=lg))
session.commit()
def have_await_cmd(msg: types.Message): def have_await_cmd(msg: types.Message):
with dbL: with dbL:
return session.query(User).filter_by(id=msg.from_user.id).first().await_cmd return session.query(User).filter_by(id=msg.from_user.id).first().await_cmd
def edt_key(): def edt_key():
key = InlineKeyboardMarkup() keys = InlineKeyboardMarkup()
for i, n in enumerate(["Day", "Next", "Week", "Next week"]): for i, n in enumerate(["Day", "Next", "Week", "Next week"]):
key.add(InlineKeyboardButton(n, callback_data=posts_cb.new(id=i, action=n.lower()))) keys.add(InlineKeyboardButton(n, callback_data=posts_cb.new(id=i, action=n.lower())))
return key return keys
def calendar(time: str, user_id: int): def calendar(time: str, user_id: int):
@ -96,8 +116,8 @@ async def notif():
nt = u.get_notif() nt = u.get_notif()
kf = u.get_kfet() kf = u.get_kfet()
tm = u.get_tomuss() tm = u.get_tomuss()
except: except Exception as e:
pass logger.error(e)
if nt: if nt:
await bot.send_message(u.id, lang(u, "notif_event")+str(nt), parse_mode=ParseMode.MARKDOWN) await bot.send_message(u.id, lang(u, "notif_event")+str(nt), parse_mode=ParseMode.MARKDOWN)
@ -125,6 +145,7 @@ async def notif():
@dp.inline_handler() @dp.inline_handler()
async def inline_edt(inline_query: InlineQuery): async def inline_edt(inline_query: InlineQuery):
check_id(inline_query.from_user)
text = inline_query.query.lower() if inline_query.query.lower() in TIMES else "invalid" text = inline_query.query.lower() if inline_query.query.lower() in TIMES else "invalid"
res = calendar(text, inline_query.from_user.id) res = calendar(text, inline_query.from_user.id)
input_content = InputTextMessageContent(res, parse_mode=ParseMode.MARKDOWN) input_content = InputTextMessageContent(res, parse_mode=ParseMode.MARKDOWN)
@ -139,31 +160,17 @@ async def inline_edt(inline_query: InlineQuery):
@dp.message_handler(commands="start") @dp.message_handler(commands="start")
async def start(message: types.Message): async def start(message: types.Message):
user_id = message.from_user.id check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logger.info(f"{message.from_user.username} start") logger.info(f"{message.from_user.username} start")
with dbL: with dbL:
if user_id not in session.query(User.id).all(): user = session.query(User).filter_by(id=message.from_user.id).first()
logger.info(f"{message.from_user.username} add to the db")
if message.from_user.locale and message.from_user.locale.language:
lg = message.from_user.locale.language
else:
lg = ""
session.add(User(id=user_id, language=lg))
session.commit()
user = session.query(User).filter_by(id=user_id).first()
key = reply_keyboard.ReplyKeyboardMarkup()
key.add(reply_keyboard.KeyboardButton("Edt"))
key.add(reply_keyboard.KeyboardButton("Kfet"))
key.add(reply_keyboard.KeyboardButton("Setkfet"))
key.add(reply_keyboard.KeyboardButton("Setedt"))
key.add(reply_keyboard.KeyboardButton("Notif"))
key.add(reply_keyboard.KeyboardButton("Settomuss"))
await message.reply(lang(user, "welcome"), parse_mode=ParseMode.MARKDOWN, reply_markup=key) await message.reply(lang(user, "welcome"), parse_mode=ParseMode.MARKDOWN, reply_markup=key)
@dp.message_handler(commands="help") @dp.message_handler(commands="help")
async def help_cmd(message: types.Message): async def help_cmd(message: types.Message):
check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logger.info(f"{message.from_user.username} do help command") logger.info(f"{message.from_user.username} do help command")
with dbL: with dbL:
@ -173,6 +180,7 @@ async def help_cmd(message: types.Message):
@dp.message_handler(lambda msg: msg.text.lower() == "edt") @dp.message_handler(lambda msg: msg.text.lower() == "edt")
async def edt_cmd(message: types.Message): async def edt_cmd(message: types.Message):
check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logger.info(f"{message.from_user.username} do edt") logger.info(f"{message.from_user.username} do edt")
await message.reply(calendar("day", message.from_user.id), parse_mode=ParseMode.MARKDOWN, reply_markup=edt_key()) await message.reply(calendar("day", message.from_user.id), parse_mode=ParseMode.MARKDOWN, reply_markup=edt_key())
@ -180,13 +188,16 @@ async def edt_cmd(message: types.Message):
@dp.callback_query_handler(posts_cb.filter(action=["day", "next", "week", "next week"])) @dp.callback_query_handler(posts_cb.filter(action=["day", "next", "week", "next week"]))
async def edt_query(query: types.CallbackQuery, callback_data: dict): async def edt_query(query: types.CallbackQuery, callback_data: dict):
check_id(query.message.from_user)
await query.message.chat.do(types.ChatActions.TYPING) await query.message.chat.do(types.ChatActions.TYPING)
logger.info(f"{query.message.from_user.username} do edt query") logger.info(f"{query.message.from_user.username} do edt query")
await query.message.reply(calendar(callback_data["action"], query.from_user.id), parse_mode=ParseMode.MARKDOWN, reply_markup=edt_key()) await query.message.reply(calendar(callback_data["action"], query.from_user.id), parse_mode=ParseMode.MARKDOWN,
reply_markup=edt_key())
@dp.message_handler(lambda msg: msg.text.lower() == "kfet") @dp.message_handler(lambda msg: msg.text.lower() == "kfet")
async def kfet(message: types.Message): async def kfet(message: types.Message):
check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logger.info(f"{message.from_user.username} do kfet") logger.info(f"{message.from_user.username} do kfet")
with dbL: with dbL:
@ -204,6 +215,7 @@ async def kfet(message: types.Message):
@dp.message_handler(lambda msg: msg.text.lower() == "setkfet") @dp.message_handler(lambda msg: msg.text.lower() == "setkfet")
async def kfet_set(message: types.Message): async def kfet_set(message: types.Message):
check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logger.info(f"{message.from_user.username} do setkfet") logger.info(f"{message.from_user.username} do setkfet")
with dbL: with dbL:
@ -220,6 +232,7 @@ async def kfet_set(message: types.Message):
@dp.message_handler(lambda msg: msg.text.lower() == "setedt") @dp.message_handler(lambda msg: msg.text.lower() == "setedt")
async def edt_await(message: types.Message): async def edt_await(message: types.Message):
check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logger.info(f"{message.from_user.username} do setedt") logger.info(f"{message.from_user.username} do setedt")
with dbL: with dbL:
@ -232,7 +245,7 @@ async def edt_await(message: types.Message):
@dp.message_handler(lambda msg: msg.text.lower() == "settomuss") @dp.message_handler(lambda msg: msg.text.lower() == "settomuss")
async def edt_await(message: types.Message): async def edt_await(message: types.Message):
user_id = str(message.from_user.id) check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logger.info(f"{message.from_user.username} do settomuss") logger.info(f"{message.from_user.username} do settomuss")
with dbL: with dbL:
@ -245,7 +258,7 @@ async def edt_await(message: types.Message):
@dp.message_handler(commands="getedt") @dp.message_handler(commands="getedt")
async def edt_geturl(message: types.Message): async def edt_geturl(message: types.Message):
user_id = str(message.from_user.id) check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logger.info(f"{message.from_user.username} do getedt command") logger.info(f"{message.from_user.username} do getedt command")
with dbL: with dbL:
@ -258,19 +271,21 @@ async def edt_geturl(message: types.Message):
@dp.message_handler(lambda msg: msg.text.lower() == "notif") @dp.message_handler(lambda msg: msg.text.lower() == "notif")
async def notif_cmd(message: types.Message): async def notif_cmd(message: types.Message):
check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logger.info(f"{message.from_user.username} do notif") logger.info(f"{message.from_user.username} do notif")
key = InlineKeyboardMarkup() keys = InlineKeyboardMarkup()
for i, n in enumerate(["Toggle", "Time", "Cooldown"]): for i, n in enumerate(["Toggle", "Time", "Cooldown"]):
key.add(InlineKeyboardButton(n, callback_data=posts_cb.new(id=i, action=n.lower()))) keys.add(InlineKeyboardButton(n, callback_data=posts_cb.new(id=i, action=n.lower())))
with dbL: with dbL:
user = session.query(User).filter_by(id=message.from_user.id).first() user = session.query(User).filter_by(id=message.from_user.id).first()
msg = lang(user, "notif_info").format(user.nt, user.nt_time, user.nt_cooldown) msg = lang(user, "notif_info").format(user.nt, user.nt_time, user.nt_cooldown)
await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=keys)
@dp.callback_query_handler(posts_cb.filter(action=["toggle", "time", "cooldown"])) @dp.callback_query_handler(posts_cb.filter(action=["toggle", "time", "cooldown"]))
async def notif_query(query: types.CallbackQuery, callback_data: dict): async def notif_query(query: types.CallbackQuery, callback_data: dict):
check_id(query.message.from_user)
await query.message.chat.do(types.ChatActions.TYPING) await query.message.chat.do(types.ChatActions.TYPING)
logger.info(f"{query.message.from_user.username} do notif query") logger.info(f"{query.message.from_user.username} do notif query")
with dbL: with dbL:
@ -294,6 +309,7 @@ async def notif_query(query: types.CallbackQuery, callback_data: dict):
@dp.message_handler(lambda msg: have_await_cmd(msg), content_types=[ContentType.TEXT, ContentType.PHOTO]) @dp.message_handler(lambda msg: have_await_cmd(msg), content_types=[ContentType.TEXT, ContentType.PHOTO])
async def await_cmd(message: types.message): async def await_cmd(message: types.message):
check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
msg = None msg = None
with dbL: with dbL:
@ -308,8 +324,7 @@ async def await_cmd(message: types.message):
if qr: if qr:
url = str(qr[0].data) url = str(qr[0].data)
elif message.text: elif message.text:
msg_url = re.findall( msg_url = re_url.findall(message.text)
"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", message.text)
if msg_url: if msg_url:
url = msg_url[0] url = msg_url[0]
@ -365,6 +380,7 @@ async def await_cmd(message: types.message):
@dp.message_handler(commands="getid") @dp.message_handler(commands="getid")
async def get_id(message: types.Message): async def get_id(message: types.Message):
check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logger.info(f"{message.from_user.username} do getid command") logger.info(f"{message.from_user.username} do getid command")
await message.reply(message.from_user.id) await message.reply(message.from_user.id)
@ -372,6 +388,7 @@ async def get_id(message: types.Message):
@dp.message_handler(commands="getlogs") @dp.message_handler(commands="getlogs")
async def get_logs(message: types.Message): async def get_logs(message: types.Message):
check_id(message.from_user)
logger.info(f"{message.from_user.username} do getlog command") logger.info(f"{message.from_user.username} do getlog command")
if message.from_user.id == ADMIN_ID: if message.from_user.id == ADMIN_ID:
try: try:
@ -398,6 +415,7 @@ async def get_logs(message: types.Message):
@dp.message_handler(commands="getdb") @dp.message_handler(commands="getdb")
async def get_db(message: types.Message): async def get_db(message: types.Message):
check_id(message.from_user)
logger.info(f"{message.from_user.username} do getdb command") logger.info(f"{message.from_user.username} do getdb command")
if message.from_user.id == ADMIN_ID: if message.from_user.id == ADMIN_ID:
with dbL: with dbL:
@ -414,6 +432,7 @@ async def get_db(message: types.Message):
@dp.message_handler(commands="eval") @dp.message_handler(commands="eval")
async def eval_cmd(message: types.Message): async def eval_cmd(message: types.Message):
check_id(message.from_user)
logger.info(f"{message.from_user.username} do eval command") logger.info(f"{message.from_user.username} do eval command")
if message.from_user.id == ADMIN_ID: if message.from_user.id == ADMIN_ID:
msg = markdown.text( msg = markdown.text(