Skip to content

Commit

Permalink
fix: ignore expired message counter value
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Apr 15, 2023
1 parent 378b6c0 commit 69ca7c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async def wrapper(message: Message, context: CallbackContext, question: str) ->
if (
not filters.is_known_user(username)
and user.message_counter.value >= config.conversation.message_limit.count > 0
and not user.message_counter.is_expired()
):
# this is a group user and they have exceeded the message limit
wait_for = models.format_timedelta(user.message_counter.expires_after())
Expand Down
18 changes: 17 additions & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ async def test_no_mention(self):
self.assertEqual(self.bot.text, "")


class UserMessageLimitTest(unittest.IsolatedAsyncioTestCase, Helper):
class MessageLimitTest(unittest.IsolatedAsyncioTestCase, Helper):
def setUp(self):
self.ai = FakeGPT()
askers.TextAsker.model = self.ai
Expand Down Expand Up @@ -388,6 +388,22 @@ async def test_unknown_user(self):
await self.command(update, self.context)
self.assertTrue(self.bot.text.startswith("Please wait"))

async def test_expired(self):
config.conversation.message_limit.count = 3

user = User(id=2, first_name="Bob", is_bot=False, username="bob")
# the counter has reached the limit, but the value has expired
user_data = {
"message_counter": {"value": 3, "timestamp": dt.datetime.now() - dt.timedelta(hours=1)}
}
self.application.user_data[user.id] = user_data
context = CallbackContext(self.application, chat_id=1, user_id=user.id)

update = self._create_update(11, text="What is your name?", user=user)
await self.command(update, context)
self.assertEqual(self.bot.text, "What is your name?")
self.assertEqual(user_data["message_counter"]["value"], 1)

async def test_unlimited(self):
config.conversation.message_limit.count = 0
other_user = User(id=2, first_name="Bob", is_bot=False, username="bob")
Expand Down

0 comments on commit 69ca7c2

Please sign in to comment.