Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions bot/exts/info/information.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
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

Expand Down Expand Up @@ -106,22 +107,28 @@ async def role_info(self, ctx: Context, *roles: Union[Role, str]) -> None:

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)
Comment thread
kosayoda marked this conversation as resolved.

if failed_roles:
await ctx.send(f":x: Could not retrieve the following roles: {', '.join(failed_roles)}")
Expand Down
2 changes: 1 addition & 1 deletion tests/bot/exts/info/test_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def test_role_info_command(self):
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
Expand Down