添加检查问题
Adding a check issue
您好,我正在尝试检查是否只有特定用户输入了 yes
或 no
然后应用该角色。
目前,如果添加一个可提及的角色,它会首先 return 一条消息,询问用户是否要添加此角色。然而,这一行会导致冲突,比如当另一个用户键入 yes 时,它会为原始用户应用角色。
这不是我期望的结果。那么我该如何添加支票呢?
这是我正在使用的代码以及我遇到问题的行。
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
完整代码:
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)
我有一些代码用于为 wait_for
生成检查函数。下面是我用来等待消息的
from collections.abc import Sequence
def make_sequence(seq):
if seq is None:
return ()
if isinstance(seq, Sequence) and not isinstance(seq, str):
return seq
else:
return (seq,)
def message_check(channel=None, author=None, content=None, ignore_bot=True, lower=True):
channel = make_sequence(channel)
author = make_sequence(author)
content = make_sequence(content)
if lower:
content = tuple(c.lower() for c in content)
def check(message):
if ignore_bot and message.author.bot:
return False
if channel and message.channel not in channel:
return False
if author and message.author not in author:
return False
actual_content = message.content.lower() if lower else message.content
if content and actual_content not in content:
return False
return True
return check
然后您可以轻松地将您想要接收的消息的要求传递给wait_for
check = message_check(author=ctx.author, channel=ctx.channel, content=('y', 'yes'))
msg = await self.bot.wait_for("message", timeout=20.0, check=check)
您好,我正在尝试检查是否只有特定用户输入了 yes
或 no
然后应用该角色。
目前,如果添加一个可提及的角色,它会首先 return 一条消息,询问用户是否要添加此角色。然而,这一行会导致冲突,比如当另一个用户键入 yes 时,它会为原始用户应用角色。
这不是我期望的结果。那么我该如何添加支票呢?
这是我正在使用的代码以及我遇到问题的行。
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
完整代码:
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)
我有一些代码用于为 wait_for
生成检查函数。下面是我用来等待消息的
from collections.abc import Sequence
def make_sequence(seq):
if seq is None:
return ()
if isinstance(seq, Sequence) and not isinstance(seq, str):
return seq
else:
return (seq,)
def message_check(channel=None, author=None, content=None, ignore_bot=True, lower=True):
channel = make_sequence(channel)
author = make_sequence(author)
content = make_sequence(content)
if lower:
content = tuple(c.lower() for c in content)
def check(message):
if ignore_bot and message.author.bot:
return False
if channel and message.channel not in channel:
return False
if author and message.author not in author:
return False
actual_content = message.content.lower() if lower else message.content
if content and actual_content not in content:
return False
return True
return check
然后您可以轻松地将您想要接收的消息的要求传递给wait_for
check = message_check(author=ctx.author, channel=ctx.channel, content=('y', 'yes'))
msg = await self.bot.wait_for("message", timeout=20.0, check=check)