简体   繁体   中英

Discord.py - AttributeError: 'NoneType' object has no attribute 'send'

I was casually programming my Discord bot, and when I was done, I went and tested my code (which sends a welcoming message when someone joins the server) by using my alternative account to join the test server. But when the alternative joined, I got this error:

Ignoring exception in on_member_join
Traceback (most recent call last):
  File "C:\Users\quinn\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "E:\GrindersValley\ValleysPy\cogs\Hellogoodbye.py", line 36, in on_member_join
    await client.get_channel(844229450928488458).send(embed=embed)
AttributeError: 'NoneType' object has no attribute 'send'\

I have no clue what's wrong. My code: https://pastebin.com/VntL0y5d

First, your issue is that your declaration of client and bot is inconsistent, so change all instances of client to bot .

Second, all calls upon bot inside the cog should be self.bot because you declared that in _init_ as a class variable, so ex: bot.get_channel() should be self.bot.get_channel()

Third, your cache may not be populated so add:

await self.bot.wait_until_ready()

to the beginning of your listeners to ensure your client's cache is fully populated before trying to call on it

TextChannel wasn't found. If the bot can't fetch the channel from its cache then it returns None, which is what's triggering your error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM