Discord BOT - DM someone that leaves server - python

I know that it is possible to DM someone with the bot if you have their ID, but how can I get the users ID's when they enter the server?
I was thinking:
#client.event
async def on_member_leave(member):
or
async def on_guild_remove(guild):
user = await client.fetch_user(**the id**)
await user.send("Your message")

There's no need to use fetch_user, you can simply use message.send -
#client.event
async def on_member_leave(member):
await member.send("Your message")
Also, if the member has no server common with the client or if the member has disabled direct messages, The message will not be sent.

Related

The bot does not send a message to the dm

I want to make that when a user join to the server where this bot is located, the bot should send him a greeting message in the dm. But when connecting, the bot does not send a message. No errors are displayed. How can I fix this? My code:
#client.event
async def on_member_join():
await member.send("Welcome!")
#client.eevent
async def on_member_join(member):
await member.send("Welcome!")
you need to have an arguement in the function!
Also you need to enable intents in your bot
client = discord.Client(command_prefix="!", intents=discord.Intents.all(),case_insensitive=True)
you also need to enable all intents in the discord Developer portal

How to make my bot forward DMs sent to it to a channel

So, I've got a bot that can send a user a DM via a very simple command. All I do is "!DM #user message" and it sends them the message. However, people sometimes try responding to the bot in DMs with their questions, but the bot does nothing with that, and I cannot see their replies. Would there be a way for me to make it so that if a user sends a DM to the bot, it will forward their message to a certain channel in my server (Channel ID: 756830076544483339).
If possible, please can someone tell me what code I would have to use to make it forward on all DMs sent to it to my channel 756830076544483339.
Here is my current code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
activity = discord.Game(name="DM's being sent", type=3)
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name="Pokemon Go Discord Server!"))
print("Bot is ready!")
#bot.command()
async def DM(ctx, user: discord.User, *, message=None):
message = message or "This Message is sent via DM"
await user.send(message)
await ctx.send('DM Sent Succesfully.')
Thank you!
Note: I'm still quite new to discord.py coding, so please could you actually write out the code I would need to add to my script, rather than just telling me bits of it, as I often get confused about that. Thank you!
What you're looking for is the on_message event
#bot.event
async def on_message(message):
# Checking if its a dm channel
if isinstance(message.channel, discord.DMChannel):
# Getting the channel
channel = bot.get_channel(some_id)
await channel.send(f"{message.author} sent:\n```{message.content}```")
# Processing the message so commands will work
await bot.process_commands(message)
Reference

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

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!
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.
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)

Discord.Py On Join commands

I have a discord bot, and I want it to do something like
once a member joins
DM member (message)
if member replies with key
give them this role
Thanks
You need to use the function on_member_join().
#client.event
async def on_member_join(member):
pass
Then put the code message sending/receiving code in there. With your example you would do:
#client.event
async def on_member_join(member):
await client.send_message(member, 'Prompt.')
m = await client.wait_for_message(author=member, channel=member)
if m.content == 'key':
# give the user the role
await client.send_message(member, 'Role added')
else:
await client.send_message(member, 'Incorrect key')
To find out how to give a user a role from a dm to a server, read this question: How To Assign A User A Role In A Server From A Direct Message - Discord.py
#client.event
async def on_member_join(member):
await member.send("hello")
This code sends hello to the user when he/she joins the server.
Hope this is helpful for everybody

Python - DM a User Discord Bot

I'm working on a User Discord Bot in Python .If the bot owner types !DM #user then the bot will DM the user that was mentioned by the owner.
#client.event
async def on_message(message):
if message.content.startswith('!DM'):
msg = 'This Message is send in DM'
await client.send_message(message.author, msg)
The easiest way to do this is with the discord.ext.commands extension. Here we use a converter to get the target user, and a keyword-only argument as an optional message to send them:
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='!')
#bot.command(pass_context=True)
async def DM(ctx, user: discord.User, *, message=None):
message = message or "This Message is sent via DM"
await bot.send_message(user, message)
bot.run("TOKEN")
For the newer 1.0+ versions of discord.py, you should use send instead of send_message
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='!')
#bot.command()
async def DM(ctx, user: discord.User, *, message=None):
message = message or "This Message is sent via DM"
await user.send(message)
bot.run("TOKEN")
Since the big migration to v1.0, send_message no longer exists.
Instead, they've migrated to .send() on each respective endpoint (members, guilds etc).
An example for v1.0 would be:
async def on_message(self, message):
if message.content == '!verify':
await message.author.send("Your message goes here")
Which would DM the sender of !verify. Like wise, you could do:
for guild in client.guilds:
for channel in guild.channels:
channel.send("Hey yall!")
If you wanted to send a "hi yall" message to all your servers and all the channels that the bot is in.
Since it might not have been entirely clear (judging by a comment), the tricky part might get the users identity handle from the client/session. If you need to send a message to a user that hasn't sent a message, and there for is outside of the on_message event. You will have to either:
Loop through your channels and grab the handle based on some criteria
Store user handles/entities and access them with a internal identifier
But the only way to send to a user, is through the client identity handle which, in on_message resides in message.author, or in a channel that's in guild.channels[index].members[index]. To better understand this, i recommend reading the official docs on how to send a DM?.
I used this command in the past and it in my opinion it works the best for me:
#bot.command(pass_context=True)
async def mall(ctx, *, message):
await ctx.message.delete()
for user in ctx.guild.members:
try:
await user.send(message)
print(f"Successfully DMed users!")
except:
print(f"Unsuccessfully DMed users, try again later.")
#bot.command()
async def dm(ctx, user: discord.User, *, message=None):
if message == None:
message = "Hi!"
embed = make_embed(title=f"Sent by {user}", desc=message)
await user.send(embed=embed)
await ctx.send("Message sent!")```
I have noticed that each code I put into my code lines they don't work completely, so I added my own bit to them and bam it works! When adding this to your bot's code, don't forget to add the bot's name where it says bot name here. It will only DM the person who sent it, but you can change what it says each day for a surprise for everyone that used the command. It works every time for me.
#client.command()
async def botdm(ctx):
await ctx.message.author.send('hi my name is *bot name here* and i am a bot!')

Categories

Resources