I maked some searchs and didn't find a way or something good to help me...
I wanna make a bot in python that send a DM to the User's ID's that i have. I looked for the Discord api py, but didn't understanded how can make the function to send message.
There are several ways to DM a user in discord.py.
If you want to message the user who ran the command, you can use ctx.author.send:
#client.command()
async def send(ctx):
await ctx.author.send("Hi there")
To message a specific user:
#client.command()
async def send(ctx, user : discord.Member):
await user.send("Hello")
Note: The user parameter can be either an #mention or, in your case, a user ID.
Try this. Just do something like
#bot.command(pass_context=True)
async def send(ctx,user:discord.Member):
await user.send("Hello User"!)
Related
So, I've never used discord.py, this is my first time using it, and I am very confused, I want to make a command so it'll send the help message on DMs, but it does not works, unless you mention yourself.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '$')
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
#client.command()
async def help(ctx, user:discord.Member, *, message=None):
message = 'test'
embed = discord.Embed(title=message)
await user.send(embed=embed)
So, if you do $help, it'll do nothing, but if u do $help #John Doe#0001 it'll DM John Doe or anyone you mention.
I am sorry if this sounds stupid..
If you want the bot to message you, the person who invoked the command, you shouldn't pass the user argument. However, if you still want to mention someone so you can send them your help message, you can make user=None, an optional argument, within the function itself.
Do note that in the code snippet I have provided I made message not an argument, but something already in the function. Do view the revised code below.
#client.command()
async def help(ctx, user:discord.Member=None):
if user == None: # if no user is passed..
user = ctx.author # ..make the command author the user
message = "test"
embed = discord.Embed(title=message)
await user.send(embed=embed)
Helpful Questions:
How to send a Private Message to the message author? - SO
How to DM commands? - SO
ctx - Discord.py Docs
Do you mean sending a help message to the person who used the command but didn't to mention the user?
Here's how:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '$')
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
#client.command()
async def help(ctx):
message = 'test'
embed = discord.Embed(title=message)
await ctx.author.send(embed=embed)
Use ctx.author.send to send a message to user used the command
Here is the code for the help gui to be sent in dms , if u want to send the help prompt in the command's channel as well as dms add await ctx.channel.send(embed=embed)
#client.command()
async def help(ctx):
embed = discord.Embed(color=0xFFFFFF)
embed.set_author(name='HELP MESSAGE' icon_url=ctx.author.avatar_url)
embed.add_field(name="help" , value='sends the help message in dms')
await ctx.author.send(embed=embed)
You can add ur custom embed instead of the embed in the above code .. Hope you understood .
#client.command()
async def help(ctx, user:discord.Member, *, message=None):
embed = discord.Embed(title=f"{message}", description=f"Sent by {ctx.author}")
await user.send(embed=embed)
I would try something like this.
While programming a discord bot, I realized that I needed to read reactions that the bot dmed to different users.
Code:
#client.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
await channel.send('{} has added {} to the message: {}'.format(user.name, reaction.emoji, reaction.message.content))
This reads a message from basically any channel that the bot is in (e.g. in guilds), as opposed to just DM channels. Is there a fix for this?
Thanks in advance
I came to this realization just before so I'll add it as an answer.
You can just use isinstance to check the channel type as dpy has different internal class's for all channels.
#bot.event
async def on_reaction_add(reaction, user):
if not isinstance(reaction.message.channel, discord.DMChannel):
# Not a dm
return
print("Hey this should be a dm")
That code will only ever run in dms.
Alternately, you could remove the not and put your dm code within the if statement
So im making a !suggest command for my discord server and i have it done except i need it to dm me the suggestion the person made. I can not find info on how to dm a specific person by ID or otherwise. Here is my coded command thus far:
async def suggest(server, suggestion):
await server.owner.create_dm()
await dm_channel.send("{} suggested this: '{}'".format(ctx.author.mention, suggestion))
Please help?
You can use client.get_user to get your own User object, which has a send method that will send a DM to you over Discord.
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.command()
async def suggest(ctx, suggestion):
dm_user = client.get_user(1234567890) # Your Discord ID here
await dm_user.send("{} suggested this: '{}'".format(ctx.author.mention, suggestion))
client.run('token')
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!')
Say I want to make a bot with a "poke" feature (aka sends a pm to a user saying "Boop" when someone says "!poke #user#0000"), how would I do this? It works perfectly when I do this:
#bot.command(pass_context=True)
async def poke(ctx, message):
await client.send_message(ctx.message.author, 'boop')
but only if I want to poke the author of the message. I want to poke whoever's being #'d.
I know the discord.py documents say I can use this:
start_private_message(user)
but I don't know what to put in place of user.
It's actually simpler than that
#bot.command(pass_context=True)
async def poke(ctx, member: discord.Member):
await bot.send_message(member, 'boop')
send_message contains logic for private messages, so you don't have to use start_private_message yourself. The : discord.Member is called a converter, and is described in the documentation here
I might be necroing this post a bit, but the recent version of this would look like this:
#bot.command():
async def poke(ctx, user: discord.Member=None):
if user is None:
await ctx.send("Incorrect Syntax:\nUsage: `!poke [user]`")
await user.send("boop")
The if user is None: block is optional, but is useful if you want to have an error message if a command isn't used correctly.