【问题标题】:AttributeError: 'Client' object has no attribute 'send_message' (Discord Bot)AttributeError: 'Client' 对象没有属性 'send_message' (Discord Bot)
【发布时间】:2018-06-15 11:00:55
【问题描述】:

由于某种原因,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'

【问题讨论】:

  • 你是在异步还是重写分支?
  • 在纠正错误的缩进并使用我自己的令牌后,它在我的 Linux 机器上运行良好。

标签: python python-3.x discord discord.py


【解决方案1】:

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

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

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

但对于我看到你在做什么,我建议使用 commands extension

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

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')

【讨论】:

  • 出于某种原因,如果您想安装重写版本,请使用此 python3 -m pip install -U github.com/Rapptz/discord.py/archive/rewrite.zip
  • 其实用这个命令更好pip install -U git+https://github.com/Rapptz/discord.py@rewrite#egg=discord.py[voice]
  • 现在 rewrite (v1.0.0) 已经正式发布了这个版本应该现在通过一个简单的pip install discord.py安装
【解决方案2】:

根据discord.py 文档,迁移到v1.0 带来了API 的许多重大变化,包括很多功能discord.Client 移出并放入各自的模型中。

例如在您的具体情况下: Client.send_message(abc.Messageable) --> abc.Messageable.send()

因此您的代码将从此重新设置样式:

await client.send_message(message.channel, 'I heard you! {0}'.format(message.author))

到这里:

await message.channel.send('I heard you! {0}'.format(message.author))

这使得代码在描述实际的不和谐实体方面更加一致和准确,即不和谐Message 被发送到Channelabc.Messageable 的子类)而不是Client 是直观的自己。

从这个意义上说,迁移列表非常多;你可以找到他们hereModels are Stateful 部分下。

【讨论】:

    【解决方案3】:

    我认为 discord.py 不能再用于send_message() 函数了。但这并不意味着我们不能发送消息。确实存在另一种向频道发送消息的方式。

    你可以使用:

    await message.channel.send("")
    

    为此目的。

    现在让我们将其应用到您的代码中。

    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 message.channel.send(F'Hi {author}, I heard you.')
    
    client.run("key")
    

    现在这将解决您的问题,您将能够使用您的机器人轻松发送消息。

    谢谢! :D

    【讨论】:

      猜你喜欢
      • 2019-12-20
      • 2019-03-07
      • 2020-12-17
      • 1970-01-01
      • 2021-07-10
      • 2019-10-15
      • 2018-03-20
      • 2022-07-23
      相关资源
      最近更新 更多