Hi I'm trying to check if only the specific user has typed yes
or no
then apply the role.
At the moment if a mentionable role is added it will first return a message asking the user whether they want to add this role. However this line causes a conflict say when another user types yes it it applies the original user the role.
This is not the result I expected. So how would I go about adding a check?
Here is the code I'm working with and the line where I'm having the issue.
if msg is None or msg.content.lower().strip() in ("yes", "y"):
await author.add_roles(role)
message = '{} added the role **{}**.'.format(author.display_name, role.name)
embed = discord.Embed(description=message.format(author.display_name, role.name), colour=0x56e011)
await ctx.send(embed=embed)
else:
embed = discord.Embed(description='Okay I won't add the role **{}**.'.format(role.name))
await ctx.send(embed=embed)
return
The full code:
async def add(self, ctx, *, rolename):
author = ctx.message.author
role_dict = {
"blue":556228119565041726,
"green":556228124719710261,
"orange":556228127567904790,
"yellow":556228225320222757}
role_id = role_dict.get(rolename.lower())
if not role_id:
message = 'I cannot find the role **{}**.'
embed = discord.Embed(description=message.format(rolename))
await ctx.send(embed=embed)
return
role = discord.utils.get(ctx.message.guild.roles, id = role_id)
if role in author.roles:
message = 'It looks like you already have the role **{}**.'
embed = discord.Embed(description=message.format(role.name))
await ctx.send(embed=embed)
return
if role.mentionable:
message = '**@mention** notifications are enabled for the role **{}**. If you still want to add this role type **Yes** otherwise **No**.'.format(role.name)
embed = discord.Embed(description=message.format(author.display_name, role.name), colour=0xff8100)
await ctx.send(embed=embed)
try:
msg = await self.bot.wait_for("message", timeout=20.0)
except asyncio.TimeoutError:
await ctx.send('Sorry, you took too long. Try again.')
return
if msg is None or msg.content.lower().strip() in ("yes", "y"):
await author.add_roles(role)
message = '{} added the role **{}**.'.format(author.display_name, role.name)
embed = discord.Embed(description=message.format(author.display_name, role.name), colour=0x56e011)
await ctx.send(embed=embed)
else:
embed = discord.Embed(description='Okay I won't add the role **{}**.'.format(role.name))
await ctx.send(embed=embed)
return
else:
await author.add_roles(role)
message = '{} added the role **{}**.'.format(author.display_name, role.name)
embed = discord.Embed(description=message.format(author.display_name, role.name), colour=0x56e011)
await ctx.send(embed=embed)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…