Use greedy converter, no more commas

pull/198/merge
bobloy 3 years ago
parent a6e0646b85
commit 955624ad1a

@ -3,6 +3,7 @@ import logging
import re import re
import discord import discord
from discord.ext.commands import RoleConverter, Greedy, CommandError, ArgumentParsingError
from discord.ext.commands.view import StringView from discord.ext.commands.view import StringView
from redbot.core import Config, checks, commands from redbot.core import Config, checks, commands
from redbot.core.bot import Red from redbot.core.bot import Red
@ -13,15 +14,38 @@ log = logging.getLogger("red.fox_v3.ccrole")
async def _get_roles_from_content(ctx, content): async def _get_roles_from_content(ctx, content):
content_list = content.split(",") # greedy = Greedy[RoleConverter]
try: view = StringView(content)
role_list = [ rc = RoleConverter()
discord.utils.get(ctx.guild.roles, name=role.strip(" ")).id for role in content_list
] # "Borrowed" from discord.ext.commands.Command._transform_greedy_pos
except (discord.HTTPException, AttributeError): # None.id is attribute error result = []
return None while not view.eof:
else: # for use with a manual undo
return role_list previous = view.index
view.skip_ws()
try:
argument = view.get_quoted_word()
value = await rc.convert(ctx, argument)
except (CommandError, ArgumentParsingError):
view.index = previous
break
else:
result.append(value)
return [r.id for r in result]
# Old method
# content_list = content.split(",")
# try:
# role_list = [
# discord.utils.get(ctx.guild.roles, name=role.strip(" ")).id for role in content_list
# ]
# except (discord.HTTPException, AttributeError): # None.id is attribute error
# return None
# else:
# return role_list
class CCRole(commands.Cog): class CCRole(commands.Cog):
@ -84,7 +108,7 @@ class CCRole(commands.Cog):
# Roles to add # Roles to add
await ctx.send( await ctx.send(
"What roles should it add? (Must be **comma separated**)\n" "What roles should it add?\n"
"Say `None` to skip adding roles" "Say `None` to skip adding roles"
) )
@ -106,7 +130,7 @@ class CCRole(commands.Cog):
# Roles to remove # Roles to remove
await ctx.send( await ctx.send(
"What roles should it remove? (Must be comma separated)\n" "What roles should it remove?\n"
"Say `None` to skip removing roles" "Say `None` to skip removing roles"
) )
try: try:
@ -124,7 +148,7 @@ class CCRole(commands.Cog):
# Roles to use # Roles to use
await ctx.send( await ctx.send(
"What roles are allowed to use this command? (Must be comma separated)\n" "What roles are allowed to use this command?\n"
"Say `None` to allow all roles" "Say `None` to allow all roles"
) )

Loading…
Cancel
Save