使用discord.py开发Discord机器人时]kill命令未标记成员如何发错误提示
实现方案
调整思路:
- 给
person参数设置默认值None,将必填参数改为可选参数 - 函数开头先判断
person是否为空,为空就直接返回目标提示文本,不为空再执行原有逻辑
修改后的完整代码如下:
# 你原有代码里的bot初始化、killgifs列表定义等内容保持不变即可 @bot.command(name='kill') async def kill(ctx, person: discord.Member = None): # 无标记成员时返回提示 if person is None: await ctx.send("> Please Tag Someone") return # 原有功能逻辑不变 author = ctx.author embed = discord.Embed(color=discord.Color.red()) embed.set_author(name=f'{author} kills {person.display_name}') embed.set_image(url = random.choice(killgifs)) await ctx.send(embed=embed)
内容的提问来源于stack exchange,提问作者Rovindu Thamuditha
相关产品推荐
相关产品推荐

