discord.py 8ball embed命令启动失败 报await outside function语法错误
Discord 机器人8ball命令开发报错说明
开发初始代码
重做Discord机器人的8ball命令时,编写的实现逻辑如下:
@client.command(aliases=['8ball']) async def _8ball(ctx, *, question): responses = ['It is certain.', 'It is decidedly so.', 'Without a doubt.', 'Yes - definitely.', 'You may rely on it.', 'As I see it, yes.', 'Most likely.', 'Outlook good.', 'Yes.', 'Signs point to yes.', 'Reply hazy, try again.', 'Ask again later.', 'No.', 'Better not tell you now.', 'Cannot predict now.', 'Concentrate and ask again.', "Don't count on it.", 'My reply is no.', 'My sources say no.', 'Outlook not so good.', 'Very doubtful.'] responses = random.choice(responses) embed=discord.Embed(title="8ball", description="Ask the 8ball!", color=discord.Color.dark_purple()) embed.add_field(name="Q: {question}", value="A: {responses}", inline=False) await ctx.send(embed=embed)
运行报错信息
启动Python文件时无法正常运行,返回语法错误:
File "C:\Users\r00t_technologies\Documents\bot\enigmatic-peak-21114\bot_development.py", line 135 await ctx.send(embed=embed) ^ SyntaxError: 'await' outside function
排查结果
该问题已自行解决,原因为看错了代码对应的行号位置,不存在await脱离异步函数范围的实际代码错误。因相关开发领域经验尚浅,初期排查时对行号对应关系的判断出现偏差,特此说明。
- 后续开发可留意现有代码的显示问题:当前代码中
add_field方法传入的字符串未添加f前缀,{question}和{responses}不会被实际变量值替换,会原样显示占位符文本,给对应字符串加上f前缀即可正常展示提问和8ball的回答内容。
内容的提问来源于stack exchange,提问作者g2781
相关产品推荐
相关产品推荐

