How to send private message to member in on_member_join() discord.py?

Question:

This is what I have:

@client.command(pass_context=True)
@client.event
async def on_member_join(ctx, member):
    print(f'{member} has joined a server.')
    await ctx.send(f"Hello {member}!")
    await ctx.member.send(f"Welcome to the server!")

I need the bot to send a private message containing rules and commands list when he joins.

Please help!

Asked By: MicasiO

||

Answers:

The event on_member_join() only accepts member as a valid parameter (see doc). Thus what you try to do: on_member_join(ctx, member) ,wont work. You need to use this instead: on_member_join(member).

If you used the event as follows:

@client.event
async def on_member_join(member):
    await member.send('Private message')

You can send messages directly to members who joined the server. Because you get an member object using this event.

Answered By: Deru

I don’t know what happened, from one day to the next the bot stopped sending welcome messages to new members. But I was finally able to solve it.
I just had to add these two lines of code. intents = discord.Intents() intents.members = True Read

import discord
from discord.ext import commands

#try add this 
intents=discord.Intents.all()

#if the above don't work, try with this
#intents = discord.Intents()
#intents.members = True

TOKEN = 'your token'
bot=commands.Bot(command_prefix='!',intents=intents)

#Events
@bot.event
async def on_member_join(member):
    await member.send('Private message')

@bot.event
async def on_ready():
    print('My bot is ready')

bot.run(TOKEN)
Answered By: ivansaul
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.