Skip to content

Commit

Permalink
Removed ConfigMiddleware; pass config to Bot instance directly instead
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterGroosha committed May 18, 2021
1 parent 4eac34e commit d803f22
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 47 deletions.
5 changes: 1 addition & 4 deletions bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from aiogram.types import BotCommand

from bot.config_reader import load_config
from bot.middlewares.config import ConfigMiddleware
from bot.handlers.main_group_admin import register_main_group_admin
from bot.handlers.main_group_user import register_main_group_user
from bot.handlers.main_group_events import register_group_events
Expand Down Expand Up @@ -49,6 +48,7 @@ async def main():
config = load_config()

bot = Bot(token=config.token, parse_mode="HTML")
bot["config"] = config
dp = Dispatcher(bot)

# Register handlers
Expand All @@ -57,9 +57,6 @@ async def main():
register_group_events(dp, main_group_id=config.group.main)
register_callbacks_reports(dp)

# Register middlewares
dp.middleware.setup(ConfigMiddleware(config))

# Register /-commands in UI
await set_bot_commands(bot)

Expand Down
7 changes: 4 additions & 3 deletions bot/handlers/callbacks_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from bot.localization import get_string


async def callbacks_on_report_msg(call: types.CallbackQuery, callback_data: dict, config: Config, lang: str):
async def callbacks_on_report_msg(call: types.CallbackQuery, callback_data: dict):
"""
Handle callback button taps on report message in admin group
Expand All @@ -15,6 +15,7 @@ async def callbacks_on_report_msg(call: types.CallbackQuery, callback_data: dict
:param config: bot config
:param lang: preferred bot language
"""
config = call.bot.get("config")
option = callback_data.get("option", "del")
user_id = callback_data.get("user_id")
message_ids = callback_data.get("message_ids")
Expand All @@ -25,10 +26,10 @@ async def callbacks_on_report_msg(call: types.CallbackQuery, callback_data: dict

if option == "del":
await call.message.edit_text(
call.message.html_text + get_string(lang, "action_deleted"))
call.message.html_text + get_string(config.lang, "action_deleted"))
elif option == "ban":
await call.bot.kick_chat_member(config.group.main, user_id)
await call.message.edit_text(call.message.html_text + get_string(lang, "action_deleted_banned"))
await call.message.edit_text(call.message.html_text + get_string(config.lang, "action_deleted_banned"))
await call.answer()


Expand Down
11 changes: 6 additions & 5 deletions bot/handlers/main_group_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
restriction_time_regex = re.compile(r'(\b[1-9][0-9]*)([mhd]\b)')


async def error_no_reply(message: types.Message, lang: str):
async def error_no_reply(message: types.Message):
lang = message.bot.get("config").get("lang")
await message.reply(get_string(lang, "error_no_reply"))


Expand All @@ -24,13 +25,13 @@ def get_restriction_period(text: str) -> int:
return 0


async def cmd_ro(message: types.Message, lang: str):
async def cmd_ro(message: types.Message):
"""
Handle /ro command in main group
:param message: Telegram message starting with /ro
:param lang: preferred bot language
"""
lang = message.bot.get("config").get("lang")
readonly_to = await message.chat.get_member(message.reply_to_message.from_user.id)
if readonly_to.is_chat_admin():
await message.reply(get_string(lang, "error_restrict_admin"))
Expand All @@ -53,13 +54,13 @@ async def cmd_ro(message: types.Message, lang: str):
)


async def cmd_nomedia(message: types.Message, lang: str):
async def cmd_nomedia(message: types.Message):
"""
Handle /nomedia command in main group
:param message: Telegram message starting with /nomedia
:param lang: preferred bot language
"""
lang = message.bot.get("config").get("lang")
nomedia_to = await message.chat.get_member(message.reply_to_message.from_user.id)
if nomedia_to.is_chat_admin():
await message.reply(get_string(lang, "error_restrict_admin"))
Expand Down
29 changes: 14 additions & 15 deletions bot/handlers/main_group_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@ def get_message_url(chat_id, message_id):
)


async def error_no_reply(message: types.Message, lang: str):
async def error_no_reply(message: types.Message):
lang = message.bot.get("config").get("lang")
await message.reply(get_string(lang, "error_no_reply"))


async def cmd_report(message: types.Message, config: Config, lang: str):
async def cmd_report(message: types.Message):
"""
Handle /report command in main group
:param message: Telegram message starting with /report
:param config: bot config
:param lang: preferred bot language
"""
config = message.bot.get("config")
reported = await message.chat.get_member(message.reply_to_message.from_user.id)
if reported.is_chat_admin():
await message.reply(get_string(lang, "error_report_admin"))
await message.reply(get_string(config.lang, "error_report_admin"))
return

available_options = {
get_string(lang, "action_del_msg"): "del",
get_string(lang, "action_del_and_ban"): "ban"
get_string(config.lang, "action_del_msg"): "del",
get_string(config.lang, "action_del_and_ban"): "ban"
}
parts = message.text.split(maxsplit=1)
report_msg_template = get_string(lang, "report_message")
report_msg_template = get_string(config.lang, "report_message")
if len(parts) == 2:
report_msg_template += get_string(lang, "report_note").format(note=quote_html(parts[1]))
report_msg_template += get_string(config.lang, "report_note").format(note=quote_html(parts[1]))

msg = await message.reply(get_string(lang, "report_sent"))
msg = await message.reply(get_string(config.lang, "report_sent"))

kb = types.InlineKeyboardMarkup()
for button_text, option in available_options.items():
Expand All @@ -58,23 +58,22 @@ async def cmd_report(message: types.Message, config: Config, lang: str):
await message.bot.send_message(
config.group.reports,
report_msg_template.format(
time=message.reply_to_message.date.strftime(get_string(lang, "report_date_format")),
time=message.reply_to_message.date.strftime(get_string(config.lang, "report_date_format")),
msg_url=get_message_url(message.chat.id, message.reply_to_message.message_id),
),
reply_markup=kb
)


async def calling_all_units(message: types.Message, config: Config, lang: str):
async def calling_all_units(message: types.Message):
"""
Notifying all admins about something's going on in main group
:param message: Telegram message starting with @admin
:param config: bot config
:param lang: preferred bot language
"""
config = message.bot.get("config")
msg_url = get_message_url(message.chat.id, message.message_id)
text = get_string(lang, "need_admins_attention").format(msg_url=msg_url)
text = get_string(config.lang, "need_admins_attention").format(msg_url=msg_url)
await message.bot.send_message(config.group.reports, text)


Expand Down
Empty file removed bot/middlewares/__init__.py
Empty file.
20 changes: 0 additions & 20 deletions bot/middlewares/config.py

This file was deleted.

0 comments on commit d803f22

Please sign in to comment.