diff options
author | 2021-01-21 12:53:07 +0100 | |
---|---|---|
committer | 2021-01-21 12:53:07 +0100 | |
commit | 1d83eff5feb1c4abb201ce6567c9524a29cd93a2 (patch) | |
tree | 9862c4636a3a28774e25ade36b151e9ce81a24fa | |
parent | Changed slowmode reset tests (diff) | |
parent | Merge PR #1202 - fuzzy match the role command (diff) |
Merge branch 'master' into mbaruh/sm-stats
-rw-r--r-- | bot/exts/info/information.py | 23 | ||||
-rw-r--r-- | tests/bot/exts/info/test_information.py | 2 |
2 files changed, 16 insertions, 9 deletions
diff --git a/bot/exts/info/information.py b/bot/exts/info/information.py index 38e760ee3..9fb875925 100644 --- a/bot/exts/info/information.py +++ b/bot/exts/info/information.py @@ -6,7 +6,8 @@ from collections import Counter, defaultdict from string import Template from typing import Any, Mapping, Optional, Tuple, Union -from discord import ChannelType, Colour, Embed, Guild, Message, Role, Status, utils +import fuzzywuzzy +from discord import ChannelType, Colour, Embed, Guild, Message, Role, Status from discord.abc import GuildChannel from discord.ext.commands import BucketType, Cog, Context, Paginator, command, group, has_any_role @@ -106,22 +107,28 @@ class Information(Cog): To specify multiple roles just add to the arguments, delimit roles with spaces in them using quotation marks. """ - parsed_roles = [] - failed_roles = [] + parsed_roles = set() + failed_roles = set() + all_roles = {role.id: role.name for role in ctx.guild.roles} for role_name in roles: if isinstance(role_name, Role): # Role conversion has already succeeded - parsed_roles.append(role_name) + parsed_roles.add(role_name) continue - role = utils.find(lambda r: r.name.lower() == role_name.lower(), ctx.guild.roles) + match = fuzzywuzzy.process.extractOne( + role_name, all_roles, score_cutoff=80, + scorer=fuzzywuzzy.fuzz.ratio + ) - if not role: - failed_roles.append(role_name) + if not match: + failed_roles.add(role_name) continue - parsed_roles.append(role) + # `match` is a (role name, score, role id) tuple + role = ctx.guild.get_role(match[2]) + parsed_roles.add(role) if failed_roles: await ctx.send(f":x: Could not retrieve the following roles: {', '.join(failed_roles)}") diff --git a/tests/bot/exts/info/test_information.py b/tests/bot/exts/info/test_information.py index d077be960..80731c9f0 100644 --- a/tests/bot/exts/info/test_information.py +++ b/tests/bot/exts/info/test_information.py @@ -65,7 +65,7 @@ class InformationCogTests(unittest.IsolatedAsyncioTestCase): permissions=discord.Permissions(0), ) - self.ctx.guild.roles.append([dummy_role, admin_role]) + self.ctx.guild.roles.extend([dummy_role, admin_role]) self.cog.role_info.can_run = unittest.mock.AsyncMock() self.cog.role_info.can_run.return_value = True |