2

I'm trying to use lower() so the role names are not case sensitive. So if a user types lol instead of LoL it won't go through the if statement if not role_id:

This is how I'm doing it:

@commands.command()
@commands.check(lambda ctx: ctx.channel.id in [555844758778544160])
async def add(self, ctx, *, rolename):
    author = ctx.message.author
    role_dict = {
        "Members":557212810468392970,
        "PS4":568761643916328960,
        "LoL":559792606364565505}
    role_id = role_dict.get(rolename.lower())
    if not role_id:
        await ctx.send("I cannot find the role {}.".format(rolename))
        return
    role = discord.utils.get(ctx.message.guild.roles, id = role_id)
    message = '{} added the role **{}**'.format(author.display_name, role.name)
    embed = discord.Embed(description=message.format(author.display_name, role.name), colour=0xff0000)
    await author.add_roles(role)
    await ctx.send("Role Added")

This line here role_id = role_dict.get(rolename.lower()) is the culprit when I add the role !add lol rather than LoL this is what I'm getting:

Help much appreciated.

James
  • 168
  • 12
  • you may want to look at https://stackoverflow.com/questions/3296499/case-insensitive-dictionary-search to make keys case insensitive – dee cue Apr 20 '19 at 13:41

1 Answers1

3

The problem is that you're comparing a lowercase rolename with dictionary keys that are not in lowercase. For a case-insensitive check, both the rolename and dictionary keys should be lowercase.

Either manually change the dictionary keys to lowercase:

role_dict = {
        "members":557212810468392970,
        "ps4":568761643916328960,
        "lol":559792606364565505}

Or create it programmatically with a dict comprehension, and check if rolename.lower() is in the lowercase dict:

role_dict = {
        "Members":557212810468392970,
        "PS4":568761643916328960,
        "LoL":559792606364565505}
lowercase_dict = {k.lower():v for k,v in role_dict.items()}
role_id = lowercase_dict.get(rolename.lower())
if not role_id:
    await ctx.send("I cannot find the role {}.".format(rolename))
    return
glhr
  • 4,439
  • 1
  • 15
  • 26