python - 当命令在 discord.py 中缺少必需参数时如何显示消息
问题描述
我正在尝试使用随机答案生成器制作 8ball 命令,但我想添加一个事件,如果您只说“&8ball”,它会说“8ball 需要一个问题”
这是我使用的代码:
@client.command(aliases=['8b','8B','8Ball','b','B','8ball','Ball'])
@cooldown(1, 7, BucketType.user)
async def ball(ctx, *, question):
embed = discord.Embed(title="shaking the magic 8ball", colour=ctx.author.colour)
responses = [line.strip() for line in open('8ball.txt')]
choices = random.choice(responses)
embed2 = discord.Embed(title="the magic 8ball says " + choices, colour=ctx.author.colour)
message = await ctx.send(embed=embed)
await asyncio.sleep(5)
await message.edit(embed=embed2)
这是我要添加的内容:
embed3 = discord.Embed(title="The magic 8ball requires a question", colour=ctx.author.colour)
embed.add_field(text="Brody Foxx")
await ctx.send(embed=embed3)
我也尝试使用if
andelse
语句
if question in message.content:
类似的东西,试着在谷歌和 YouTube 上找遍了,我要么找不到任何东西,要么它们都在 python 中重写,所以我(再一次)求助于堆栈溢出。感谢您的帮助,如果您无论如何都帮助了我,谢谢。
解决方案
None
如果要求不能通过,您可以简单地添加,它可以像下面这样使用:
@client.command(aliases=['8b','8B','8Ball','b','B','8ball','Ball'])
@cooldown(1, 7, BucketType.user)
async def ball(ctx, *, question=None):
if question == None:
embed3 = discord.Embed(title="The magic 8ball requires a question", colour=ctx.author.colour)
embed.add_field(text="Brody Foxx")
await ctx.send(embed=embed3)
return
embed = discord.Embed(title="shaking the magic 8ball", colour=ctx.author.colour)
responses = [line.strip() for line in open('8ball.txt')]
choices = random.choice(responses)
embed2 = discord.Embed(title="the magic 8ball says " + choices, colour=ctx.author.colour)
message = await ctx.send(embed=embed)
await asyncio.sleep(5)
await message.edit(embed=embed2)
推荐阅读
- node.js - findByIdAndDelete 带有 mongodb 和 mongoose 的子文档,( nodejs )
- bash - 用于自动将文件从多个目录移动到具有相同后缀(不是文件扩展名)的新创建目录的脚本
- assembly - ascii char 到 char 转换程序集 x86?
- javascript - MVC5 @Html.EditorFor:第二个属性问题
- node.js - 如何确定当前操作系统是否为带有 Node.js 的 Window 7
- excel - 匹配 Excel 中的值并计算公式
- javascript - 在 node.js 中排队承诺的数组
- java - Firestore 更新 2 个不同的文档
- ruby-on-rails - 更新到 Rails 5.2 后 Postgres 创建不起作用
- angular - Angular:ngClass条件停止工作