'How to send a DM to a member who leaves a Discord Server
So, I am making a Basic Discord bot and I have added Welcome Message and Welcome DM to it. I am working on the Leave message and Leave DM. I am done with the Leave Message, but when I try to DM the person who just left, It just throws an error. Here's my code
import discord, random, os
from datetime import datetime
now = datetime.now()
current_time = now.strftime('%H:%M')
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
TOKEN = #bot token
@client.event
async def on_ready():
print('{0.user} is online!'.format(client))
@client.event
async def on_member_join(member):
await member.send(f'Welcome to the server {member}. Hope you have a great time here!')
guild = client.get_guild(#server id)
channel = guild.get_channel(#server id)
await channel.send(f'Thanks for joining the server {member.mention}! Hope you have a great time here.')
sadMsg = discord.Embed(color=0xFFAFA, title='**What happened..?**')
sadMsg.add_field(name='**You can tell me why you left the server!**', value='If anyone made you uncomfortable or if you want to tell us why you left the server then:\nClick this link to get a form!\nFill it up and we will get your feedback and will take actions on that user!\nIf you change your mind and want to join again\nThen you are always welcome!\nClick this link to join again\nhttps://discord.gg/u2FpSwMgcD\nClick this link to give feedback\nhttps://forms.gle/aRBZuAfSDcsFxWP86\nIf you got banned,\n**Here is the link for ban appeal**\nhttps://forms.gle/CUEygMSNbyFHQkYv8')
@client.event
async def on_member_remove(member):
guild = client.get_guild(#server id)
channel = guild.get_channel(#server id)
await channel.send(f'{member.mention} just left the server. :cry:')
@client.event
async def on_message(message):
embed = discord.Embed(color=0xFFFAFA, title=f"Hello there!")
embed.add_field(name='Hope you are having a great day.', value='Hi, my name is **The Marauders**. I am a discord bot created by Ignis#3040 with Discord.py v1.7.3.\nJoin this server to get notified about bot updates\nhttps://discord.gg/u2FpSwMgcD', inline=False)
currentTime = discord.Embed(color=0xFFAFA, title='**Time**')
currentTime.add_field(name='**Current Time**', value=f'The current time is **{current_time}**')
if message.content.startswith('!time'):
await message.channel.send(embed=currentTime)
if message.content.startswith('!hello') or message.content.startswith('/hello'):
await message.channel.send(embed=embed)
client.run(TOKEN)
In this code when I write the below line of code in the on_member_remove function
await member.send(sadMsg)
It throws and error that it can't message the user. Can anyone help?
Solution 1:[1]
The bot needs to share a mutual guild with the user to message them. So, when the user leaves the server the bot presumably loses the only shared guild and the message fails.
Solution 2:[2]
Unless your bot is in a mutual server with that user, they wont receive a message. Otherwise, just send a message to the user in on_member_remove(). Send that message to a channel with the ID of the user
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | leftomash |
| Solution 2 | Trent112232 |
