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

如何让Discord机器人报错时仅发送用户消息而非冗余信息?

解决方案

问题根源在于你使用了str(ctx.message),这会输出整个Message对象的完整结构信息,包含大量冗余字段。要获取用户实际发送的消息内容,应该使用ctx.message.content属性。

修改后的错误处理代码如下:

@client.event
async def on_command_error(ctx, error):
    channel = client.get_channel(xxxxxxxx)  # 替换为你的日志频道ID
    # 仅发送用户输入的消息内容,而非整个Message对象
    await channel.send(f'error found by: {ctx.author}, in: {ctx.channel.name},  the message was: {ctx.message.content}, the error: {str(error)}')
    await ctx.send(f"An error occured: {str(error)} ")

关键修改点:

  • 将str(ctx.message)替换为ctx.message.content,直接获取用户发送的文本内容
  • 可选优化:将str(ctx.channel)替换为ctx.channel.name,只显示频道名称而非完整的Channel对象信息,进一步减少冗余

修改后,日志消息里的the message was:部分就只会显示用户实际输入的内容,不会再出现ID、频道属性等冗余信息。

内容的提问来源于stack exchange,提问作者nawafksa090

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 19:40:31