Sending a DM to a new member - discord.py

I'm working on a basic bot with discord.py and I need to figure out how to send a DM when a new member joins. When a new user joins, it should trigger an event which will get the member and send them a DM welcoming them to the server. I've tried quite a few variations of code trying to get it to work but nothing happens when I join with an alt.
Here's the basic code I have:
async def on_member_join(member):
await member.create_dm()
await member.send(
f'Hi {member.name}! Welcome to the server!'
)
I've also tried without the member.create_dm() because the docs said that it should do that on its own. I'm using Python 3.7.3 and discord.py 1.5.1 so old answers from a couple years ago don't work. I've tried everything that I can find.

You need to enable intents.members, also I saw a handful of questions similar like these so you didn't do a good job when researching.
intents = discord.Intents.default()
intents.members = True
# If you're using commands.Bot
bot = commands.Bot(..., intents=intents)
# If you're using discord.Client
client = discord.Client(intents=intents)
Also make sure to enable them in the developer portal

Starting with discord.py 1.5, in order to receive the member join event, you must enable the members gateway intent, which you can read about here. You must first enable to members intent on the developer portal, then when constructing your client/bot instance, you must pass your intents to it.
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(**other_options, intents=intents)
# Or if using the clinet
client = discord.Client(**other_options, intents=intents)
Finally, you should also be sure to handle the Forbidden exception that may be raised if the member has their DMs turned off
try:
await member.send('Some text')
except discord.Forbidden:
# The user had their DMs disabled, do whatever you need to
# nothing in this case
pass

Related

API calls forced even with all intents enabled

I've been having an issue recently with getter methods from different objects in discordpy, in this example I'm using discord.Guild
guild = await bot.fetch_guild(key) # API call used because getter returns nothing
categories = guild.categories # returns empty list
Intents are all enabled, I used discord.Intents.all().
Privileged gateway intents are also all enabled in Developer Portal.
Can someone help me with this issue? Thanks.

How to make my discord.py bot only allow #everyone and #here from only admins and mods and not from normal members

import discord
from discord.ext import commands
bot = discord.Bot(command_prefix='!')
#bot.event
#commands.has_permissions(administrator=False)
async def delmessage(message):
await message.delete()
As explained by stijndcl in comment, the best solution is to go directly in your server settings to allow only admins and moderators to use #everyone and #here.
If you still you want to use a bot, something can easily be done using the on_message handler and then checking the roles of the message author to delete or keep the message.
The problem with this method is that the filtering process is done after the message publication, so deleting an unauthorized ping will create ghost pings and therefore a poor experience for your server members (and again why bother when a setting exists for this exact purpose).

How do I get my bot to send a message when it is added to a guild? - discord.py

I want my bot to send a message to a specific guild channel whenever it is added or removed from any guild. How do I make it in Discord.py? I am aware of client.event but I am unsure how to use it.
My bots code (sends to channel at the top of server where it has permissions)
#client.event
async def on_guild_join(guild):
for channel in guild.text_channels:
if channel.permissions_for(guild.me).send_messages:
embedHi = discord.Embed(
title="Thanks for adding me!",
description=
f"<:impostor:774673531786625024>I am the Impostor - a bot created by Baz!<:impostor:774673531786625024>\n\n<:noice:751384305464377375>You can join my support server by running $help and you can view all of my commands here as well!<:noice:751384305464377375>\n\n<:patreon:839897502925062165> Feel free to go to https://www.patreon.com/theimpostor to gain access to cool premium commands! <:patreon:839897502925062165>\nIf you join the <:purple:839879572631453696> Hacker Plan <:purple:839879572631453696>, then you will recieve all premium commands, a special role, early access to commands and even work in progress updates!\n:partying_face:Have fun!:partying_face:\n\n\n<:ping:757276110252670986>When you added this bot, it was in version {__version__}<:ping:757276110252670986>",
url="https://www.patreon.com/theimpostor",
colour=discord.Colour.red())
embedHi.set_thumbnail(
url=
"image url"
)
embedHi.set_image(url="image url")
embedHi.set_footer(
text="© Baz - The Impostor - Among Us bot for Discord")
await channel.send(embed=embedHi)
break
#bot.event
async def on_guild_join(guild):
# send your message
Relevant docs

Is there anything to make on_member_join in discord.py work?

I'm currently programming a bot and my first goal is to make the bot welcome and say goodbye to members that join or leave, but the bot sends nothing, not even showing any error.
#client.event
async def on_member_join(member):
print(" System: A member has joined the server!")
def sprint(str):
for c in str + '\n':
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(3./90)
sprint(" Bot: Oh hey, looks like "+member.name+" has joined..")
#send message to a specific channel
channel = client.get_channel(channel id from the welcome channel)
await channel.send("Hey! Welcome to the server, "+member.mention+"! I hope you read the #rules before doing anything, otherwise have fun and enjoy your stay!")
I did it last time before I recreated the script, but the on_member_join thing doesn't work, not even on_member_leave! (I'm doing the on_member_join first to check if it's working)
My alternate account rejoined my server, but the bot (except MEE6) didn't send anything.. Is there anything you guys can help me?
You'll need to enable the member intent by creating an instance of discord.Intents.default, setting Intents.members to True, and passing the instance into your commands.Bot or discord.Client constructor, like:
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(intents=intents)
You'll also need to enable the members intent in the Discord dev portal (https://discord.com/developers). Go to that url, click on your application, go to the Bot tab, scroll down to the 'privileged gateway intents' section, and enable the members intent.
Then, restart your bot, and member events should work.
(for more info, look at https://discordpy.readthedocs.io/en/latest/intents.html)

DM Admin members on member join discord.py?

So I am playing around with coding a bot, but am a complete newb to coding. I want the bot to DM all members of the Admin role when someone joins the server. I have looked everywhere and cannot get my head around it. I have tried multiple ways with no success. Has anyone get any ideas?
You'll have to loop through everyone on the server and check if they are an administrator
import discord
client = discord.Client()
#client.event
async def on_member_join(member):
server = member.server
for user in server.members:
if user.server_permissions.administrator:
await client.send_message(user, "A new member has joined")
client.run('token')

Resources