Search code examples
pythondiscordpycord

how works direct message in py-cord on_member_join()


Unfortunately I can't find on the internet how the direct message at on_member_join works. Could someone please help me

Here is my code.

@bot.event
async def on_member_join(member: discord.Member):

    guild = member.guild
    if (
            guild.system_channel is not None
    ):
        await guild.system_channel.send(f"Servus {member.mention} herzlich wilkomma auf {guild.name}!")
        'Here should be the code for the direct message '

I have tried older code which unfortunately no longer works.


Solution

  • The pycord documentation is a good place to look. The discord.Member object has a send method which you can use to send a direct message to.

    So,

    @bot.event
    async def on_member_join(member: discord.Member):
    
        guild = member.guild
        if (
                guild.system_channel is not None
        ):
            await guild.system_channel.send(f"Servus {member.mention} herzlich wilkomma auf {guild.name}!")
            try:
                await member.send(
                    "Check out the pycord docs here: https://docs.pycord.dev/en/stable/api/index.html"
                )
            except discord.Forbidden:
                # can't message the user directly - nothing we can do about it
                # they might have random DMs turned off
                pass