AttributeError: 'Client' 对象没有属性 'send_message' (Discord Bot)

新手上路,请多包涵

出于某种原因,send_message 在我的 Discord 机器人上无法正常工作,而且我无法找到修复它的方法。

 import asyncio
import discord

client = discord.Client()

@client.async_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 client.send_message(message.channel, 'Hi %s, i heard you.' % author)
client.run("key")

 on_message !test
in test function
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\indit\AppData\Roaming\Python\Python36\site-packages\discord\client.py", line 223, in _run_event
    yield from coro(*args, **kwargs)
  File "bot.py", line 15, in on_message
    await test(author, message)
  File "bot.py", line 21, in test
    await client.send_message(message.channel, 'Hi %s, i heard you.' % author)
AttributeError: 'Client' object has no attribute 'send_message'

原文由 cute 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
1 个回答

您可能正在运行 discord.py 的重写版本,因为 discord.Client 对象没有 send_message 方法。

要解决您的问题,您可以将其作为:

 async def test(author, message):
    await message.channel.send('I heard you! {0.name}'.format(author))

但是对于我看到你所做的,我建议使用 命令扩展

这使得为机器人创建机器人和命令变得更加简单,例如,这里有一些代码与您的代码完全相同

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command()
async def test(ctx):
    await ctx.send('I heard you! {0}'.format(ctx.author))

bot.run('token')

原文由 mental 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题