python - discord.py:如何在命令中返回?
问题描述
我有一个命令,在这个命令中机器人等待消息。在这种情况下,消息内容为“否”或“是”。但是如果用户写了别的东西,机器人会发送一条消息,这不起作用,用户应该再试一次。所以在这种情况下,如果用户发送“否”或“是”,机器人应该再次检查。但是我该怎么做呢?
我的代码:
@client.command()
async def setup(ctx, choice = None):
if choice == 'welcome':
def check4(messagetitletags):
return messagetitletags.channel.id == ctx.message.channel.id and messagetitletags.author == ctx.message.author and not messagetitletags.author == ctx.message.author.bot
messagetitletagscheck = await client.wait_for('message', check=check4, timeout=None)
if messagetitletagscheck.content in ['Yes', 'y', 'Y', 'yes']:
with open(r'./welcome.json', 'r') as f:
welcomemessage = json.load(f)
if f'{ctx.guild.id}' in welcomemessage.keys():
try:
welcomemessage[f"{ctx.guild.id}"][f'TitleTags'] = {}
welcomemessage[f'{ctx.guild.id}'][f"TitleTags"] = f"Yes"
except KeyError:
welcomemessage[f"{ctx.guild.id}"][f'TitleTags'] = {}
welcomemessage[f"{ctx.guild.id}"][f"TitleTags"] = f"Yes"
else:
welcomemessage[f"{ctx.guild.id}"] = {}
welcomemessage[f"{ctx.guild.id}"][f"TitleTags"] = [f"Yes"]
with open(r'./welcome.json', 'w+')as f:
json.dump(welcomemessage, f, sort_keys=True, indent=4)
elif messagetitletagscheck.content in ['No', 'n', 'N', 'no']:
with open(r'./welcome.json', 'r') as f:
welcomemessage = json.load(f)
if f'{ctx.guild.id}' in welcomemessage.keys():
try:
welcomemessage[f"{ctx.guild.id}"][f'TitleTags'] = {}
welcomemessage[f'{ctx.guild.id}'][f"TitleTags"] = f"No"
except KeyError:
welcomemessage[f"{ctx.guild.id}"][f'TitleTags'] = {}
welcomemessage[f"{ctx.guild.id}"][f"TitleTags"] = f"No"
else:
welcomemessage[f"{ctx.guild.id}"] = {}
welcomemessage[f"{ctx.guild.id}"][f"TitleTags"] = [f"No"]
with open(r'./welcome.json', 'w+')as f:
json.dump(welcomemessage, f, sort_keys=True, indent=4)
else:
await ctx.send('<:error:713187214586282054> Invalid input. Please try again.')
return check4
解决方案
您可以创建一个循环,直到结果成功。
例如,如果您想等待有效的是/否答案,您可以在正确输出时使语句为假。
例子:
def your_code():
if output == "right":
# some code
return True
elif output == "wrong":
# some code
return True
else:
# some code
return False
answer_is_valid = False
while not answer_is_valid: # Loops till answer_is_valid is True
answer_is_valid = your_code() # This puts the return value in answer_is_valid.
#If the return value was False it will loop again.
如果用户不断返回不正确的输入,我不建议它永远循环。所以我会在 x 尝试后停止该函数的函数中进行另一次检查。这看起来像这样:
def your_code():
if output == "right":
# some code
return True
elif output == "wrong":
# some code
return True
else:
# some code
return False
answer_is_valid = False
incorrect_tries = 0
max_incorrect_tries = 5
# the loops exits if the answer is valid. Or max incorrect tries has been reached
while not answer_is_valid and incorrect_tries <= max_incorrect_tries:
answer_is_valid = your_code() # This repeats the function until the function returns True
incorrect_tries += 1
您可以用您的函数替换 your_code() 函数,如果输出正确/不正确,请确保返回 True/False。
要实现它:
@client.command()
async def setup(ctx, choice = None):
if choice == "welcome":
# some code
应转换为:
def welcome():
# some code that returns True when the input was valid, False if not
@client.command()
async def setup(ctx, choice = None):
if choice == "welcome":
# the new code with while loop explained earlier using your welcome function
推荐阅读
- google-api - 在 Google Analytics API for Postman 中获取令牌
- rest - 通过 REST API for Azure DevOps 获取所有组织
- angular - angular 6 如何获取相对于字符串的 url
- javascript - 将 JSON 存储在 Redis 中,其中几个数据项是可选的
- lua - luasocket + copas 错误 - 尝试跨越 C 调用边界
- clickhouse - 缺少 clickhouse-server 的 Pidfile
- shell - 检查 drop table 语句是否删除 hive 表
- 3d - STL 文件转换为可读的文本文件
- c# - 如何在 linq 中添加条件连接?
- dart - 如何在继续颤动之前等待未来的对象?