From df238e5b8f940ce308d687ad937ffa905a23ffdf Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 6 Oct 2026 18:06:45 +0100 Subject: [PATCH] Ignore punctuation when matching a word in the zen command --- bot/exts/utils/utils.py | 5 +++-- tests/bot/exts/utils/test_utils.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/bot/exts/utils/utils.py b/bot/exts/utils/utils.py index 2f4fc0166e..c0a39d285f 100644 --- a/bot/exts/utils/utils.py +++ b/bot/exts/utils/utils.py @@ -164,8 +164,9 @@ async def zen( # Try to handle first exact word due difflib.SequenceMatched may use some other similar word instead # exact word. for i, line in enumerate(zen_lines): - for word in line.split(): - if word.lower() == search_value.lower(): + # Only compare the word itself, so "ugly" matches "ugly." + for word in re.findall(r"[\w']+", line.lower()): + if word == search_value.lower(): embed.title += f" (line {i}):" embed.description = line await ctx.send(embed=embed) diff --git a/tests/bot/exts/utils/test_utils.py b/tests/bot/exts/utils/test_utils.py index 9b8ea4adee..f2be465ed0 100644 --- a/tests/bot/exts/utils/test_utils.py +++ b/tests/bot/exts/utils/test_utils.py @@ -85,6 +85,19 @@ async def test_zen_with_valid_slices(self): await self.cog.zen.callback(self.cog, ctx, search_value=input_slice) ctx.send.assert_called_once_with(embed = self.template_embed) + async def test_zen_with_exact_word_ignores_punctuation(self): + """ Tests if the `!zen` command matches an exact word that has punctuation next to it. """ + expected_results = {"ugly": 0, "silenced": 10, "obvious": 12, "RIGHT": 15} + + for word, line in expected_results.items(): + self.template_embed.title = f"The Zen of Python (line {line}):" + self.template_embed.description = self.zen_list[line] + + ctx = MockContext() + with self.subTest(word=word, line=line): + await self.cog.zen.callback(self.cog, ctx, search_value=word) + ctx.send.assert_called_once_with(embed=self.template_embed) + async def test_zen_with_invalid_slices(self): """ Tests if the `!zen` command reacts properly to invalid slices for indexing as an argument. """ slices= ["19:18", "10:9", "-1:-2", "0:-100", "::0", "1:2:-1", "-5:-4:-1"]