Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

'NoneType' object has no attribute 'send'?

The code is below

@client.event
async def on_member_join(Member: discord.member):
    # Welcome message I guess
    channel = discord.utils.get(Member.guild.channels, name='☭│chat')
    embed = discord.Embed(
        title="A new member has joined the server :grin:",
        description=f"Thanks for joining {Member.mention} \n Hope you brought pizza!!",
    )
    embed.set_image(url=Member.avatar_url)
    try:
        await channel.send(embed=embed)
    finally:
        await channel.send(":skull:")

The error is described in the title, the welcome message really never seems to work because the channel is apparently a "None" object, is the issue the channel declaration?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I am assuming you are using the discordpy module.
According to its documentation, the discord.utils.get() function will return None if "nothing is found that matches the attributes passed".

This is occouring in your code, and so the later channel.send() fails. To solve this, consider adding an except AttributeError as e clause to your (conveniently placed) try-finally statement, and within it check if the type of channel is None.

try:
    await channel.send(embed=embed)
except AttributeError as e:
    if channel is None: # None is a singleton (unique) object, so all none instances satisfy this 'is' expression.
        ... you can now know that no channel could be attained

@Barmar correctly said that the finally statement will be executed even after the exception is caught (and so an error will still occour). This can be circumvented by encapsulating the contents of the finally cause in another try-except clause pair [though this is somewhat unattractive].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading