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

Discord机器人报错:'Client'对象无'send_message'属性,求解决

解决Discord机器人send_message报错的问题

你遇到的AttributeError: 'Client' object has no attribute 'send_message'是因为discord.py v1.0及以后的版本已经彻底移除了Client.send_message()方法。这个方法只存在于v0.16.x这类旧版本中,新版本把发送消息的功能直接放到了频道对象上。

给你两个修复方案,更推荐第一个(适配新版本的写法):

方案一:适配新版本discord.py

只需要改两个地方就能搞定:

  1. 把@client.async_event改成@client.event——新版本里注册事件不再需要async_前缀
  2. 把client.send_message(message.channel, ...)替换成message.channel.send(...)

修改后的完整代码:

import asyncio
import discord
client = discord.Client()

@client.event
async def on_message(message):
    author = message.author
    if message.content.startswith('!test'):
        print('on_message !test')
        await test(author, message)

async def test(author, message):
    print('in test function')
    await message.channel.send('Hi %s, i heard you.' % author)

client.run("your_bot_token_here")

方案二:回退到旧版本discord.py(不推荐)

如果你因为特殊需求必须沿用旧写法,可以安装v0.16.12版本(最后一个支持send_message的版本):

pip install discord.py==0.16.12

不过还是建议用新版本,旧版本已经停止维护,不会再修复bug或添加新功能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:10:17