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

cut*_*ute 9 python python-3.x discord discord.py

由于某些原因,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")
Run Code Online (Sandbox Code Playgroud)
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'
Run Code Online (Sandbox Code Playgroud)

men*_*tal 10

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

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

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

但是对于我看到的操作,我建议使用命令扩展名

这使得创建机器人和针对该机器人的命令变得更加简单,例如,以下代码与您的代码完全相同

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')
Run Code Online (Sandbox Code Playgroud)

  • 实际上,最好使用以下命令`pip install -U git + https://github.com/Rapptz/discord.py@rewrite#egg=discord.py [voice]` (3认同)