Skip to content

Commit

Permalink
fix: current user is in update, not in message
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Apr 13, 2024
1 parent ae1d30b commit 27323fb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
16 changes: 10 additions & 6 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import textwrap
import time

from telegram import Chat, Message
from telegram import Chat, Message, Update
from telegram.ext import (
Application,
ApplicationBuilder,
Expand Down Expand Up @@ -112,8 +112,10 @@ async def post_shutdown(application: Application) -> None:
def with_message_limit(func):
"""Refuses to reply if the user has exceeded the message limit."""

async def wrapper(message: Message, context: CallbackContext, question: str) -> None:
username = message.from_user.username
async def wrapper(
update: Update, message: Message, context: CallbackContext, question: str
) -> None:
username = update.effective_user.username
user = UserData(context.user_data)

# check if the message counter exceeds the message limit
Expand All @@ -129,17 +131,19 @@ async def wrapper(message: Message, context: CallbackContext, question: str) ->

# this is a known user or they have not exceeded the message limit,
# so proceed to the actual message handler
await func(message, context, question)
await func(update=update, message=message, context=context, question=question)

# increment the message counter
message_count = user.message_counter.increment()
logger.debug(f"user={message.from_user.username}, n_messages={message_count}")
logger.debug(f"user={username}, n_messages={message_count}")

return wrapper


@with_message_limit
async def reply_to(message: Message, context: CallbackContext, question: str) -> None:
async def reply_to(
update: Update, message: Message, context: CallbackContext, question: str
) -> None:
"""Replies to a specific question."""
await message.chat.send_action(action="typing", message_thread_id=message.message_thread_id)

Expand Down
4 changes: 3 additions & 1 deletion bot/commands/imagine.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ async def __call__(self, update: Update, context: CallbackContext) -> None:
parse_mode=ParseMode.HTML,
)
return
await self.reply_func(update.message, context, question=message.text)
await self.reply_func(
update=update, message=update.message, context=context, question=message.text
)
2 changes: 1 addition & 1 deletion bot/commands/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ async def __call__(self, update: Update, context: CallbackContext) -> None:
# this is not a question to the bot, so ignore it
return

await self.reply_func(message, context, question=question)
await self.reply_func(update=update, message=message, context=context, question=question)
4 changes: 3 additions & 1 deletion bot/commands/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ async def __call__(self, update: Update, context: CallbackContext) -> None:
if not last_message:
await update.message.reply_text("No message to retry 🤷‍♂️")
return
await self.reply_func(update.message, context, question=last_message.question)
await self.reply_func(
update=update, message=update.message, context=context, question=last_message.question
)

0 comments on commit 27323fb

Please sign in to comment.