Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

python - Discord.py bot doesn't dm new members

I'm using the event on_member_join to attempt to dm new members, but when I tested with my alt, it didn't send a message.

@bot.event
async def on_member_join(member):
  embed = discord.Embed(title="Welcome to my server!", description=None, color = discord.Color.magenta())
  embed.add_field(name="To get started:", value="?Invite some friends!
?Check out some of the channels and get engaged with the community!
?Have fun!", inline=True)
  channel = bot.get_channel(803703488499810326)
  userDM = member.create_dm()
  await userDM.send(embed=embed)
question from:https://stackoverflow.com/questions/65908435/discord-py-bot-doesnt-dm-new-members

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to do member.create_dm() because when you DM a user it's not needed. You also don't need to define description to None, you simply don't even need to specify a description.

In order for the bot to even find the members, you need members intent turned on in your bot. You can do that using the image below.

enter image description here

Now, you just need to provide the code in the bot so that the bot can use the intents. Here is the code that you need to add to your main bot file.

intents = discord.Intents.default() # Gets the default intents from discord.
intents.members = True # enables members intents on the bot.

bot = commands.Bot(command_prefix=prefix, intents=intents)

After all of that here is your fixed code.

@bot.event
async def on_member_join(member):
  embed = discord.Embed(title="Welcome to my server!", description=None, color = discord.Color.magenta())
  embed.add_field(name="To get started:", value="?Invite some friends!
?Check out some of the channels and get engaged with the community!
?Have fun!", inline=True)
  await member.send(embed=embed)

I hope this helped, Have a nice day, Best of luck on your bot. Here is a Discord Server for Beginners.

?? Discord.py For Beginners : https://discord.gg/C8zFM3D2rn


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...