eudaimonia
eudaimonia

Reputation: 3

object NoneType can't be used in 'await' expression

I am pretty new to programming discord bots and I had little knowledge about python. I have been trying to program a bot to send a welcome image to new members. I used Pillow for image manipulation and it seemed to work just fine in the test file I created. However when I added it to the bot and tried inviting someone, the error

Ignoring exception in on_member_join Traceback (most recent call last): File "C:\Users\nam\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "C:\Users\nam\PyCharmProjects\lian_bot\bot.py", line 44, in on_member_join await client.send_file(channel, 'welcome.png') TypeError: object NoneType can't be used in 'await' expression

kept showing up. Im a complete begginner in this area.

This is the code:

@client.event
async def on_member_join(member):
    channel = client.get_channel("866789191269220382")

    url = requests.get(member.avatar_url)
    avatar = Image.open(BytesIO(url.content))
    avatar = avatar.resize((300, 300));
    bigsize = (avatar.size[0] * 3, avatar.size[1] * 3)
    mask = Image.new('L', bigsize, 0)
    draw = ImageDraw.Draw(mask)
    draw.ellipse((0, 0) + bigsize, fill=255)
    mask = mask.resize(avatar.size, Image.ANTIALIAS)
    avatar.putalpha(mask)

    output = ImageOps.fit(avatar, mask.size, centering=(0.5, 0.5))
    output.putalpha(mask)
    output.save('avatar.png')

    background = Image.open('bg.png')
    background.paste(avatar, (150, 150), avatar)
    background.save('welcome.png')

    await client.send_file(channel, 'welcome.png')

Upvotes: 0

Views: 5060

Answers (1)

colin oneill
colin oneill

Reputation: 108

Your error is on the very first line of your function, when getting a channel you pass the id of the channel, the id of this channel is an int/number. Do you see what the error is now? If not, that's ok it's that it's a string and not an int as ids are supposed to be. The reason I put so much emphasis on this is that if you learn it now it will save you a lot of headaches in the future for programming in discord.py, I Hope this helps, Best of luck to you on learning discord.py.

channel = client.get_channel(866789191269220382)

EDIT discord.py has another way of uploading a file that may be a better way to go about this.

await channel.send(file=discord.File('welcome.png'))

Upvotes: 3

Related Questions