discord.py,在没有 on_message() 事件的情况下通过频道 ID 发送消息?

时间:2021-04-29 16:33:09

标签: python discord.py

我为我的机器人配置和代码中的一般内容创建了一个类,我很整洁 所以,客户端是 discord.Client() 和 bot。是我的班级 在我的机器人类中,我存储了一些频道 ID,然后我注意到 client.send_message(channelid, message) 方式不再支持。但是,我将频道 ID 变量编辑为

channel1=client.get_channel(channelid)

client=discord.Client() 结束了我的课程,它的定义。

我用这个来发送消息

await bot.channel1.send("hello world")

我期待在那个频道有消息但现实:

Task exception was never retrieved
future: <Task finished name='Task-2' coro=<bot2() done, defined at /path/to/file/bot.py:46> exception=AttributeError("'NoneType' object has no attribute 'send'")>
Traceback (most recent call last):
  File "/path/to/file/bot.py", line 53, in bot2
    await bot.channel1.send(fc)
AttributeError: 'NoneType' object has no attribute 'send'

简而言之,我的 channel1 定义是 nonetype.. 为什么?

是的,我在代码中使用了另一个异步定义并运行它,因为此代码应该在 on_message() 之外运行并且应该异步运行

我用

在文件的末尾调用了我的异步函数
client.loop.create_task(bot())
client.loop.create_task(bot2())
client.loop.create_task(dead())
client.run(bot.token)

1 个答案:

答案 0 :(得分:0)

channel1 是非类型对象,因为它试图在机器人准备好之前定义

  await client.wait_until_ready()
  channel1=client.get_channel(id)