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

为何discord.py的Client无send_message属性?Slash命令DM用户报错

问题解决:Discord机器人Slash命令发送私信报错

错误原因

  1. 同时初始化了interactions.Client和discord.Client两个客户端,属于冗余操作,且discord.Client早已移除了send_message方法,这是报错的直接原因。
  2. 调用发送私信的方式错误,在interactions库中,无需额外调用其他客户端,直接通过命令上下文的用户对象即可发送私信。
  3. 原代码中print和await语句没有缩进,不符合Python语法规范。

修正后的代码

import interactions

# 只初始化interactions客户端,无需discord.Client
bot = interactions.Client(token="你的TOKEN")

@bot.command(
    name="gen",
    description="Generate a username and password.",
)
async def gen(ctx: interactions.CommandContext):
    print("test")
    # 通过ctx.user获取发起命令的用户,调用send方法发送私信
    await ctx.user.send("Hello!")
    # 可选:回复命令触发的频道,告知用户已发送私信
    await ctx.send("已向你发送私信,请查看!")

bot.start()

关键说明

  • 移除了不必要的discord库导入和discord.Client实例,全程使用interactions库的客户端即可实现需求。
  • ctx.user是发起Slash命令的用户对象,调用其send方法即可直接发送私信。
  • 确保代码缩进正确,Python对缩进要求严格,原代码中函数内的语句没有缩进会导致运行错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 14:12:08