Dom
Dom

Reputation: 138

How do you use ctx in discord.py welcome messages?

so I'm trying to build a discord Welcome message with an image, but when I use the following code:

@bot.event
async def on_member_join(member, ctx, user: discord.Member = None):
    with open('users.json', 'r') as f:
        users = json.load(f)

    await update_data(users, member)

    with open('users.json', 'w') as f:
        json.dump(users, f)

    if user == None:
        user = ctx.author

    asset = user.avatar_url_as(size=128)
    data = BytesIO(await asset.read())
    im = Image.open(data)

    im = im.resize((244, 244));
    bigsize = (im.size[0] * 3, im.size[1] * 3)
    mask = Image.new('L', bigsize, 0)
    draw = ImageDraw.Draw(mask) 
    draw.ellipse((0, 0) + bigsize, fill=255)
    mask = mask.resize(im.size, Image.ANTIALIAS)
    im.putalpha(mask)

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

    background = Image.open('welcome.png')
    background.paste(im, (418, 68), im)
    background.save('overlap.png')

    await ctx.send(f'Welcome {user.mention}!')
    await ctx.send(file=discord.File("overlap.png")) 

...I get the error:

 await coro(*args, **kwargs)
TypeError: on_member_join() missing 1 required positional argument: 'ctx'

I'm not sure why this is happening as I have defined ctx in the event. Any help would be appreciated.

Upvotes: 0

Views: 817

Answers (1)

Nurqm
Nurqm

Reputation: 4743

You can't use any other parameter than member in on_member_join event. If you want to send a message to a channel, you can use channel.send. For getting the channel, you can use discord.utils.get or guild.get_channel(). Here is a simple example:

@bot.event
async def on_member_join(member):
    channel = member.guild.get_channel(channel id)
    await channel.send(f'Welcome {user.mention}!')
    await channel.send(file=discord.File("overlap.png")) 

Upvotes: 1

Related Questions