Python - DM a User Discord Bot

35,122

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:

  1. Loop through your channels and grab the handle based on some criteria
  2. 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?.

Share:
35,122

Related videos on Youtube

Rubayet Python
Author by

Rubayet Python

Updated on July 05, 2021

Comments

  • Rubayet Python
    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
    Pieter Helsen almost 4 years
    No longer useful since the migration to v1.0 as suggested by @Torxed
  • Patrick Haugh
    Patrick Haugh almost 4 years
    You're correct about send, but this doesn't really answer the question.
  • Torxed
    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 a on_message request.
  • Patrick Haugh
    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."