You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 01:45:03