【问题标题】:AttributeError: 'NoneType' object has no attribute 'send'AttributeError:“NoneType”对象没有“发送”属性
【发布时间】:2018-08-16 06:37:28
【问题描述】:

我想向特定频道发送消息,我按照不和谐的步骤操作,我正确地按照他们的步骤操作,但它不起作用。 这是我遵循的步骤http://discordpy.readthedocs.io/en/rewrite/faq.html#how-do-i-send-a-message-to-a-specific-channel

这是我的代码:

@bot.event
async def on_member_join(member):
    print("A member just joined and his name is" + member.name)
    general = bot.get_channel(466675715333554179)
    mid = member.id
    await general.send('Welcome <@%s> to OnePlix server, I am Cooler!' % (mid))

【问题讨论】:

  • 完整的错误是什么
  • 忽略 on_member_join Traceback 中的异常(最近一次调用最后一次):文件“C:\Users\ezter\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py ",第 307 行,在 _run_event 中从 getattr(self, event)(*args, **kwargs) 文件“C:\Users\ezter\Desktop\Oneplix bot\Bot.py”,第 45 行,在 on_member_join 等待一般。 send('欢迎 到 OnePlix 服务器,我很酷!' % (mid)) AttributeError: 'NoneType' object has no attribute 'send'
  • 错误告诉你generalNone,但你的代码假设它不是None
  • 如果你运行import discord; print(discord.__version__),它会打印什么?
  • 您可能正在运行 discord.py 的当前稳定/公共版本,其中所有 ID 都是字符串。将该频道 ID 用引号括起来,看看它是否有效。

标签: python discord discord.py


【解决方案1】:

这是一个愚蠢的错误。我输入了错误 discord.py 版本的常见问题解答。

解决办法是这样的

await client.send_message(discord.Object(id='12324234183172'), 'hello')

【讨论】:

  • 你可能想在那里使用第二种方法:await client.send_message(client.get_channel('12324234183172'), 'hello')Object 类根据它们的 id 解析项目是有效的,但是很容易忘记该对象的类型应该是什么,尤其是当你的机器人开始变大时。
猜你喜欢
  • 2021-08-19
  • 2021-11-12
  • 2019-01-01
  • 2021-12-26
  • 2019-07-23
  • 2018-05-13
  • 2020-09-07
  • 2017-05-03
  • 2023-03-16
相关资源
最近更新 更多