From 00d13437a7c27cfb7131ad43fca14a4133a1eec1 Mon Sep 17 00:00:00 2001 From: dgw Date: Thu, 30 Jan 2025 19:04:34 -0600 Subject: [PATCH] bot: round rate limit `time_left` value up, eliminate microseconds This makes rate limit feedback from the bot more readable. We could go full-on custom format, but honestly the built-in string representation of a timedelta is perfectly serviceable if you get rid of the microseconds portion. (How you get there is pretty ugly, but it's not AS bad as a whole new helper function in `tools.time`.) --- sopel/bot.py | 8 +++++++- test/test_bot.py | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/sopel/bot.py b/sopel/bot.py index d52e1707c..07bf78025 100644 --- a/sopel/bot.py +++ b/sopel/bot.py @@ -8,9 +8,11 @@ from __future__ import annotations from ast import literal_eval +from datetime import timedelta import inspect import itertools import logging +import math import re import threading import time @@ -631,7 +633,11 @@ def rate_limit_info( return False, None next_time = metrics.last_time + rate_limit - time_left = next_time - at_time + time_left = timedelta( + seconds=math.ceil( + (next_time - at_time).total_seconds() + ) + ) message: Optional[str] = None diff --git a/test/test_bot.py b/test/test_bot.py index 73904e810..63f68c3b3 100644 --- a/test/test_bot.py +++ b/test/test_bot.py @@ -1174,8 +1174,8 @@ def testrule(bot, trigger): # assert there is now a NOTICE which contains templated time left information assert mockbot.backend.message_sent patt = (br"NOTICE Test :" - br"time_left=\d+:\d+:\d+(\.\d+)? " - br"time_left_sec=\d+(\.\d+)?") + br"time_left=\d+:\d+:\d+ " + br"time_left_sec=\d+") assert re.match(patt, mockbot.backend.message_sent[0])