You need to enable JavaScript to run this app. Discord Bot 私信发送Embed帮助消息报send_message/whisper属性不存在错误
优惠活动
大模型
产品
解决方案
定价
更多

Discord Bot 私信发送Embed帮助消息报send_message/whisper属性不存在错误

Discord .helpDM私信功能报错排查与实现

报错根因

触发AttributeError: 'Bot' object has no attribute 'send_message'、'Bot' object has no attribute 'whisper'的核心原因是Discord.py 2.0及以上版本做了API破坏性变更,旧版本中挂载在全局Bot实例上的send_message()、whisper()方法已被完全移除,所有消息发送逻辑统一收敛到渠道对象的自有方法上,不存在全局Bot实例的通用消息发送入口。

原两种实现方案的失效逻辑拆解

  • 基于@client.event修饰的on_message监听方案:旧代码直接调用client.send_message(用户对象, 消息内容)触发发送的写法,在新版Bot实例上不存在对应方法属性,直接抛出属性不存在的报错
  • 基于@client.command修饰的helpDMM命令方案:旧代码尝试调用client.whisper()发送私信的写法,本质是旧版discord.py的已废弃封装方法,新版已完全下线,同样触发属性不存在的报错

可正常运行的标准实现

新版API的私信发送标准流程为,先获取触发指令的用户对象,通过用户对象创建/拉取对应的私信渠道,调用渠道对象的send()方法完成Embed消息下发,完全不需要依赖全局Bot实例的发送方法。

推荐写法:基于命令装饰器实现(避免与系统命令逻辑冲突)

import discord
from discord.ext import commands

# 初始化Bot配置
intents = discord.Intents.default()
intents.message_content = True  # 必须开启消息内容读取意图
bot = commands.Bot(command_prefix=".", intents=intents)

@bot.command(name="helpDM")
async def send_help_to_dm(ctx):
    # 构建需求指定的Embed对象
    help_embed = discord.Embed(
        title="Hello",
        color=discord.Color.orange()
    )
    # 批量添加指定的15项功能字段
    feature_names = [
        "Bomb", "Luck", "Tof", "Hitme", "Monkey",
        "Whois", "coin", "Dice", "Randomnumber", "Meme",
        "Say", "Kick", "Ban", "Unban", "Prefix"
    ]
    for feature in feature_names:
        help_embed.add_field(name=feature, value="对应功能的详细说明文本", inline=False)
    
    # 核心逻辑:获取触发用户的私信渠道并发送Embed
    dm_channel = await ctx.author.create_dm()
    await dm_channel.send(embed=help_embed)
    # 可选反馈:在指令触发频道提示用户已发送私信,避免用户反复发指令
    await ctx.send("帮助说明已发送至你的私信,请查收。", delete_after=10)

备选写法:基于消息事件监听实现(非推荐,易出现逻辑冲突)

@bot.event
async def on_message(message):
    # 过滤机器人自身发送的消息,避免触发死循环
    if message.author.bot:
        return
    # 匹配.helpDM指令
    if message.content.strip() == ".helpDM":
        help_embed = discord.Embed(
            title="Hello",
            color=discord.Color.orange()
        )
        feature_names = [
            "Bomb", "Luck", "Tof", "Hitme", "Monkey",
            "Whois", "coin", "Dice", "Randomnumber", "Meme",
            "Say", "Kick", "Ban", "Unban", "Prefix"
        ]
        for feature in feature_names:
            help_embed.add_field(name=feature, value="对应功能的详细说明文本", inline=False)
        
        dm_channel = await message.author.create_dm()
        await dm_channel.send(embed=help_embed)
    # 必须手动调用命令处理逻辑,否则会导致其他所有Bot内置命令失效
    await bot.process_commands(message)

常见踩坑提示

  • 初始化Bot时必须开启message_content意图,否则机器人无法读取用户发送的消息内容,会出现指令无响应的问题
  • 所有消息发送操作不要在新版Bot实例上寻找全局发送方法,统一通过目标渠道对象调用send()方法即可
  • 如果出现私信发送权限报错,需检查Discord开发者后台对应机器人的私信权限、成员意图配置是否正常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.16 13:54:04