【问题标题】:'NoneType' object has no attribute 'send' [closed]“NoneType”对象没有“发送”属性[关闭]
【发布时间】:2021-12-10 03:27:27
【问题描述】:

这是我的代码。当用户加入服务器时,它应该向频道发送消息。

@client.event
async def on_member_join(member):
    print('+') #this works perfectly
    ch = client.get_channel(84319995256905728)
    await ch.send(f"{member.name} has joined")

但是发生了错误。这是输出:

Ignoring exception in on_member_join
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/nextcord/client.py", line 351, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 30, in on_member_join
    await ch.send(f"{member.name} has joined")
AttributeError: 'NoneType' object has no attribute 'send'

我在开发门户中启用了服务器成员意图。

intents = nextcord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='=',intents=intents)  

我可以知道如何解决它吗?

【问题讨论】:

  • client.get_channel() 的调用失败并返回None。所以ch 不是你想的那样。
  • 我认为您应该能够从成员对象的公会属性中获取加入的频道。参考这个example
  • R U 确定存在具有此类 ID (84319995256905728) 的频道吗?
  • @Alex 是的,频道不存在。感谢您的评论
  • @BoarGules 是的,频道不存在。感谢您的评论

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


【解决方案1】:

确保您的机器人能够在该频道中发送消息,否则它会说它无法发送它,如果它确实确保该频道 ID 存在。 例如:

@client.event
async def on_member_join(member):
  channel = client.get_channel(900247143351210004)
    await channel.send(f"{member.mention} has arrived!, check out our announcments channel for server and bot announcements!")

【讨论】:

  • 如果代码有效,请务必勾选它,以便人们知道他们回答了@Iaeden Tai 的问题
【解决方案2】:

感谢大家的投入。以上问题已处理。问题是 ID 为 84319995256905728 的频道不存在,我相应地对其进行了修复。

【讨论】:

    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 2015-09-24
    • 2021-08-11
    • 2021-08-19
    • 2019-12-12
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多