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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.