【问题标题】:discord.py bot rewrite AttributeError: 'Bot' object has no attribute 'send_message'discord.py bot rewrite AttributeError: 'Bot' object has no attribute 'send_message'
【发布时间】:2019-05-28 17:07:20
【问题描述】:

我为 discord 制作了一个机器人,当我在本地启动它时它运行良好。 我在 heroku 上构建它,它似乎也可以正常工作(感谢 Tristo)。 但在日志中我收到以下消息:

2019-01-01T23:06:50.131982+00:00 app[worker.1]: Ignoring exception in on_message
2019-01-01T23:06:50.132550+00:00 app[worker.1]: Traceback (most recent call last):
2019-01-01T23:06:50.132589+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/client.py", line 227, in _run_event
2019-01-01T23:06:50.132590+00:00 app[worker.1]:     await coro(*args, **kwargs)
2019-01-01T23:06:50.132593+00:00 app[worker.1]:   File "run.py", line 14, in on_message
2019-01-01T23:06:50.132594+00:00 app[worker.1]:     await client.send_message(message.channel, newMessage)
2019-01-01T23:06:50.132616+00:00 app[worker.1]: AttributeError: 'Bot' object has no attribute 'send_message'

我的程序是:

from discord.ext.commands import Bot
import os

BOT_PREFIX = ("?")
access_token= os.environ["ACCESS_TOKEN"]

client = Bot(command_prefix=BOT_PREFIX)

@client.event
async def on_message(message):
     if message.content.startswith("?"):
         newMessage = 'text' + str(message.content)[1:].upper() + '.png'
         await client.send_message(message.channel, newMessage)

client.run(access_token)

我的 requirements.txt 只包含“git+https://github.com/Rapptz/discord.py@rewrite#egg=discord.py[voice]

我尝试使用“send”而不是“send_message”(类似帖子中的答案),但没有任何变化。

尽管属性消息错误,我的机器人似乎仍然可以工作。 你能帮我理解发生了什么吗?

【问题讨论】:

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


    【解决方案1】:

    您已经安装了 discord.py 的重写分支,它以不同的方式执行 sending messages

    # before
    await client.send_message(channel, 'Hello')
    
    # after
    await channel.send('Hello')
    

    【讨论】:

    • 再次感谢 Tristo。我将其更改为“await message.channel.send(newMessage)”。我没有收到任何错误,但我现在收到了两条不和谐的消息。为什么 ?改成普通版本的不和谐会更容易吗?
    • 您是否有多个机器人实例同时运行?根据您想要完成的任何一个版本都可以,但当前支持的版本是重写
    【解决方案2】:

    await message.channel.send('Hello') 是你所需要的!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-03
      • 2015-07-06
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 2020-11-21
      • 1970-01-01
      相关资源
      最近更新 更多