How to make a DM command in discord.py - python

I want to create a command that only works if a user is DMing their command. How do I do that? I am using discord.py 1.5.1, and I'm fairly new to discord.py.
Here is my code so far:
from discord.ext import commands, tasks
import discord
intents = discord.Intents.all
bot = commands.Bot(command_prefix='$', description='- shows this message', intents=intents)
---- snip ----
#commands.command(brief='a DM command')
async def dm_command(ctx):
# do stuff here
bot.run('TOKEN')
When I try DMing the command, the bot doesn't pick up the DM I sent. Should I specify the command in on_message()?

Here is my code for your query:
#bot.command()
async def dm_command(ctx):
if isinstance(ctx.channel, discord.channel.DMChannel):
#do stuff here
First of all, the decorator you use is not what I was taught when i first learned discord.py, so I changed the decorator from #commands.command(brief='a DM command') to #bot.command() (Feel free to change back if it works for you). Then, the rest is fairly simple. I just checked if the channel was a DM channel, and thats it! If you have any questions about my code or if you have unforseen errors, follow up!

#client.command()
async def dm(ctx):
await ctx.author.send('Hi im a discord bot!')#this is the message that will pop up in ur dms when u input the cmd
client.run('Token')

Related

Discord Python bot commands not responding

basically i have no idea why this code is not working, ive tried using on_message and that works so long as i dont include and if statement to filter for the messages with the prefix at the start. so i scrapped that, this code worked a few months ago with a different bot i made and ive ended up scrapping all the other stuff i was doing and bringing it down to the bare basics because i cant figure out why the bot doesnt recognise messages starting with the prefix.
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
TOKEN = "Token_here"
#bot.command(name='tst')
async def test(ctx):
await ctx.send('testt')
#bot.event
async def on_ready():
print ('Successful login as {0.user}'.format(bot))
bot.run(TOKEN)
ive tried print('Test') debugging aswell see below
#bot.command(name='tst')
async def test(ctx):
print('Test")
then in discord typing the !test command still does nothing and the terminal also remains empty other than the on_ready result of
Successful login as botname#0001
i have no idea whats going on honestly
There are a couple of things that could be causing your problems. I fixed the issue by enabling intents for the bot. This also must be done on the developer portal. https://discord.com/developers/applications
intents = discord.Intents().all()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
you also don't need the name parameter in the command decorator. It can just be written as :
#bot.command()
async def test(ctx):
await ctx.send('testt')
Try these changes:
bot= commands.Bot(command_prefix='!!', intents=discord.Intents.all())
#bot.command()
async def test():
await ctx.channel.send("Test successful")

discord.py kick command it not working somehow please help me T-T

I don't have much knowledge on Python, and still in the process of learning it.
I have been significantly modifying an open-source Discord bot coded in Python 2.7.
I would like to add a feature that allows me to kick a user based on a command.
Something like [commandprefix]kickuser [userid] But I have no idea how to make the bot grab the userid from the message I sent, and when I try to make it ultra-specific to kick my second account as a test, it doesn't work either...
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions
import os
bot = commands.Bot(command_prefix='=')
#bot.command(pass_context = True)
#has_permissions(administrator=True, ban_members=True)
async def kick(ctx, userName: discord.User):
await bot.kick(userName)
#kick.error
async def kick_error(error, ctx):
if isinstance(error, MissingPermissions):
await ctx.send(f"Sorry you don't have permission to do that")
bot.run("token")```
**no error** *no work*

Making a say command in discord.py

I want to make a simple say command in discord.py (ex.!say something - the bot says "something" and deletes the command message) but every code I found doesn't work for me. I'm new to python and discord.py and would really appreciate some help.
Thanks in advance.
You could also try something much easier like this:
#client.command()
async def say(ctx, message):
if message != None
ctx.channel.send(message)
elif message == None:
ctx.channel.send('Give me something to say')
This is the easiest way I've been able to do this, hope it helps!
#client.command()
async def say(ctx, *, text):
await ctx.message.delete()
await ctx.send(f"{text}")
You can find a lot of useful information on what you can make your discord bot do in the discord.py API reference. This is an example which should do what you want it to:
import discord
from discord.ext import commands
Set your bot's intents and command prefix as well as your bot token:
intents = discord.Intents().default()
bot = commands.Bot(command_prefix='!', intents=intents)
token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
Define a command '!say' with a parameter 'msg', which the bot should reply:
#bot.command(name='say')
async def audit(ctx, msg=None):
if msg is not None:
await ctx.send(msg)
Now you can delete the message which invoked the command (needs permissions!):
await ctx.message.delete()
Finally, run the bot:
bot.run(token)

Figuring out how to make a Discord Bot send a DM in Python

I'm trying to add a command to my Discord bot that sends confidential information through DM's. This is what I currently have:
import discord
from discord.ext import commands
#bot.command(name='password', help='DM's you your password')
async def on_message(message):
await member.create.dm()
await member.dm_channel.send('password')
The problem I keep getting is "name 'member' is not defined". I have tried replacing member with Member, user, and User, but I have not gotten through. I have even tried to make the bot just send a DM with this:
import discord
from discord.ext import commands
#bot.command(pass_context=True)
async def DM(ctx, user: discord.member, *, message=None):
message = message or "This Message is sent via DM"
await bot.send_message(user, message)
but it still raises the same error. What am I doing wrong?
CTX has a property of message which has a property of author which can be used to send a message directly.
So the person who is triggering the command, will be ctx.message.author.
Also I don't think that pass_context=True is necessary, but I could be mistaken.
#bot.command()
async def DM(ctx):
return await ctx.message.author.send("Henlo Werld!")
You shouldn't use send_message, it's very old version of discord.py.
So, first of all - update your discord library - pip install -U discord.py==1.4.2.
Your question has answer at Frequently Asked Questions
For future help, I recommend joining the Discord support server
Happy coding!

discord.py bot doesnt respond to Bot.commands()

Hello I am creating a discord bot i was trying to add single command but bot doesn't respond to any commands
something like
Bot = commands.Bot(command_prefix="!")
#Bot.commands()
async def ping():
print("Pong!")
this thing should respond when I type !ping in to discord client it should print pong in to terminal
but nothing nothing at all
I have tried Bot.add_command(ping) but it says command it's aleady registered i have no idea..
From what I've read from the discord.py docs you should code it like this:
Bot = commands.Bot(command_prefix="!")
#Bot.commands()
async def ping(ctx):
ctx.send("Pong!")
When you use 'print' you print the answer on your idle terminal. 'ctx.send' print the answer on discord chat. And every function on discord.py needs and argument.

Categories