Skip to content

Commit f1c8541

Browse files
committed
Pixl - fuzzy threshold
1 parent 1636e82 commit f1c8541

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

pixl/pixl.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Pixl(commands.Cog):
4848
"""
4949

5050
__author__ = "[vertyco](https://github.com/vertyco/vrt-cogs)"
51-
__version__ = "0.3.10"
51+
__version__ = "0.4.0"
5252

5353
def __init__(self, bot: Red, *args, **kwargs):
5454
super().__init__(*args, **kwargs)
@@ -65,6 +65,7 @@ def __init__(self, bot: Red, *args, **kwargs):
6565
"show_answer": True, # Show the answer after game over
6666
"use_global": True, # Use global images added by bot owner
6767
"use_default": True, # Use default images
68+
"fuzzy_threshold": 92, # Fuzzy matching threshold (0-100)
6869
}
6970
default_global = {
7071
"images": [], # Images added by bot owner
@@ -283,7 +284,9 @@ async def start_game(self, ctx: commands.Context):
283284
invalid = "\n".join(cant_get)
284285
await ctx.send(f"Some images failed during prep\n{box(invalid)}")
285286

286-
game = PixlGrids(ctx, game_image, correct, conf["blocks_to_reveal"], conf["time_limit"])
287+
game = PixlGrids(
288+
ctx, game_image, correct, conf["blocks_to_reveal"], conf["time_limit"], conf["fuzzy_threshold"]
289+
)
287290
msg = None
288291
embed = discord.Embed(
289292
title="Pixl Guess",
@@ -393,6 +396,7 @@ async def view_settings(self, ctx: commands.Context):
393396
f"`Participants: `{conf['min_participants']} minimum\n"
394397
f"`Currency Ratio: `{conf['currency_ratio']}x\n"
395398
f"`Show Answer: `{conf['show_answer']}\n"
399+
f"`Fuzzy Threshold:`{conf['fuzzy_threshold']}%\n"
396400
f"Delay between blocks is {await self.config.delay()} seconds"
397401
)
398402
# If the timeout is faster than the amount of blocks could be revealed at that delay, calculate how many blocks would be left at game end
@@ -524,6 +528,29 @@ async def reset_scoreboard(self, ctx: commands.Context, user: Optional[discord.M
524528
await self.config.clear_all_members(ctx.guild)
525529
await ctx.send("Scoreboard has been reset for all users in this server")
526530

531+
@pixlset.command(name="fuzzy")
532+
async def set_fuzzy_threshold(self, ctx: commands.Context, threshold: float):
533+
"""
534+
Set the fuzzy matching threshold for answer checking (0.0 to 1.0)
535+
536+
A lower value means more lenient matching (more answers accepted)
537+
A higher value requires more accurate spelling
538+
539+
Examples:
540+
- 0.92 (default) accepts answers that are 92% similar
541+
- 0.7 would accept answers that are roughly 70% similar
542+
- 1.0 would require exact matching
543+
- 0 would disable fuzzy matching entirely
544+
"""
545+
if threshold < 0 or threshold > 1:
546+
return await ctx.send("The threshold must be between 0 and 1")
547+
548+
# Convert from 0-1 range to 0-100 range for fuzz.ratio
549+
threshold_int = int(threshold * 100)
550+
551+
await self.config.guild(ctx.guild).fuzzy_threshold.set(threshold_int)
552+
await ctx.send(f"The fuzzy matching threshold has been set to {threshold} ({threshold_int}%)")
553+
527554
@pixlset.group(name="image")
528555
async def image(self, ctx: commands.Context):
529556
"""Add/Remove images"""

pixl/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,14 @@ def __init__(
132132
answers: list,
133133
amount_to_reveal: int,
134134
time_limit: int,
135+
fuzzy_threshold: int = 92,
135136
):
136137
self.ctx = ctx
137138
self.image = image
138139
self.answers = answers
139140
self.amount_to_reveal = amount_to_reveal
140141
self.time_limit = time_limit
142+
self.fuzzy_threshold = fuzzy_threshold
141143
# Game stuff
142144
self.start = datetime.now()
143145
self.time_left = f"<t:{round(self.start.timestamp() + self.time_limit)}:R>"
@@ -205,7 +207,11 @@ def have_winner(self) -> bool:
205207
responses = sorted(self.data["responses"].copy(), key=lambda x: x[2], reverse=False)
206208
self.data["responses"].clear()
207209
for author, answer, _ in responses:
208-
if any([fuzz.ratio(answer, a) > 92 for a in self.answers]):
210+
if not self.fuzzy_threshold:
211+
if answer in self.answers:
212+
self.winner = author
213+
return True
214+
elif any([fuzz.ratio(answer.lower(), a.lower()) > self.fuzzy_threshold for a in self.answers]):
209215
self.winner = author
210216
return True
211217
else:

0 commit comments

Comments
 (0)