Unable to send message on member join (discord.py)

Viewed 671
async def on_member_join(member):
    await bot.send_message(member, "Welcome !")

This is my code. And when I a member join the server, I obtain this :

Ignoring exception in on_member_join
Traceback (most recent call last):
  File "C:\Users\pauls\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/pauls/OneDrive/Bureau/Discord UHC/PandaBot/main.py", line 19, in on_member_join
    await bot.send_message(member, "Welcome !")
AttributeError: 'Bot' object has no attribute 'send_message'

Can someone help me pls ?

3 Answers

I think this will help you, if you want to send direct message to connected user:

async def on_member_join(member):
    await member.send("Welcome!")

here is a simple code which sends a custom message on "system message channel" when someone joins the server! OR if there is no system message channel you need to place the specific channel id in channel_id_here

@client.event
async def on_member_join(member):
    try:
        channel = member.guild.system_channel
        await channel.send("welcome {} to {} ".format(str(member.mention),str(member.guild)))
    except:
        channel = member.guild.get_channel(channel_id_here)
        await channel.send("welcome {} to {} ".format(str(member.mention),str(member.guild)))

That code has a lot of errors. First, (member, "Welcome !") should be (member + "Welcome !"). Second, bot.send_message is invalid; it should be bot.send if you are using the rewrite version, but even then it wouldn’t work, because it won’t know what channel to send it in.

mguild = bot.get_guild(<id>)
if member.guild is not None and member.guild == mguild:
    channel = mguild.get_channel(<id>)
    if channel is not None:
        await channel.send(member + "Welcome!")

This would work, just replace the <id> placeholder with the IDs you want. Cheers!

Related