Python - DM a User Discord Bot
Solution 1
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")
Solution 2
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?.
Related videos on Youtube
Rubayet Python
Updated on July 05, 2021Comments
-
Rubayet Python almost 3 years
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)
-
Pieter Helsen almost 4 yearsNo longer useful since the migration to v1.0 as suggested by @Torxed
-
Patrick Haugh almost 4 yearsYou're correct about
send
, but this doesn't really answer the question. -
Torxed almost 4 years@PatrickHaugh in what way doesn't it answer how to send a message to a user? That's exactly what my first code snippet does. You have to find the user handle object and call the
.send()
on that entity. Either by looping a channel or via aon_message
request. -
Patrick Haugh almost 4 years".If the bot owner types
!DM @user
then the bot will DM the user that was mentioned by the owner."