How to send DM to command author? - discord.py

I am trying to make a bot that sends the user who entered the command a DM, but I don't get an error code in PyCharm or a DM in Discord when I enter the command. Am I doing something wrong?
#client.command(aliases=['manage'])
async def oversee(message):
await message.author.send('Hello World!')

According to the discord.py documentation, when you create a command using the commands framework, your first argument must be ctx:
#client.command(aliases=['manage'])
async def oversee(ctx):
await ctx.author.send('Hello World!')
Your function would work as intended if it was a on_message event:
#client.event
async def on_message(message):
if message.content.startswith("!manage") or message.content.startswith("!oversee"):
await message.author.send('Hello World!')
However, I don't recommand using on_message events to create commands, using the commands framework will be way easier and more efficient.

Related

Discord.PY Custom Commands - can you define multiple commands/functions in one file?

Silly question, but in Discord PY are you able to define multiple custom commands or functions in one file? I am working on a few custom commands and just noticed that only the one defined first works, the others say command not found. They all work individually, just not when there are multiple commands defined. Is this format just wrong? Not getting any syntax errors and again it was just working until I added the Add/Remove Roles commands, or even just a Test command that does nothing..
Thanks!
My code looks something like this:
imports
define HelperFunctions:
APIInfo, etc
#start bot
bot = commands.Bot(command_prefix='$', intents=intents)
#bot.command()
#define bot commands
async def TestCommand(params):
do stuff
async def AddRoles(params):
do stuff
async def RemoveRoles(params):
do stuff
async def AddToList(params):
do stuff
bot.run()
bot.command() decorator should be used separately for every command.
https://github.com/Rapptz/discord.py/blob/master/examples/basic_bot.py
#define bot commands
#bot.command()
async def TestCommand(params):
do stuff
#bot.command()
async def AddRoles(params):
do stuff
#bot.command()
async def RemoveRoles(params):
do stuff
#bot.command()
async def AddToList(params):
do stuff

discord.py dm User when using the bot?

I want to send to the user a change log message when someone uses the bot!
Is there something like on_command when someone uses the bot they get a dm?
Yes, you can use either before_invoke or after_invoke depending on when you want your log message to run.
#client.before_invoke
async def before_invoke(ctx):
await ctx.send('before invoke')
#client.after_invoke
async def after_invoke(ctx):
await ctx.send('after invoke')
await ctx.author.send('Command completed!')

Discord banall command not working | discord.py

Hello I am creating a banall command for discord on a bot but it doesn't work for some reason please correct the code or give me a working command for ban all or mass ban
The Code:
for users in ctx.guild.user:
try:
await user.ban(reason=reason)
print(f"[-] BAN : Banned {user}")
except:
pass
The guild object doesn't have a user attribute, so it will raise an AttributeError. This is supressed because it is inside a try-except block. You should loop through ctx.guild.members instead.
so your code should look like this:
#client.command()
async def ban_everyone(ctx):
for member in ctx.guild.members:
await ctx.ban(member)

I am trying to make my bot be able to respond to a command that starts with a certain set of characters instead of having an exact command

I am trying to make my bot be able to respond to a command that starts with a certain set of characters instead of having an exact command. I figured out how to use it with client.event but I can't figure out how to do it using client.command.
As an example let's say I want it to send a message that says "Hello!" when the command ".hi" is sent. I want it to work even if a user sends ".hii" or maybe ".hi!". I can get this to work by using the startswith command. Below is my working client.event code and my broken client.command code:
client.event code:
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.content.lower().startswith('.hi'):
await message.channel.send('Hello!')
client.command code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.', case_insensitive=True)
#client.command()
async def hi(message):
if message.content.startswith('hi'):
print('Hello!')
If you are using the command, you could specify the aliases of the command like this:
#client.command(aliases=['hii', 'hi!'])
async def hi(ctx):
print('Hello!')
https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=aliases#discord.ext.commands.Command.aliases
Also your command is broken because you need to pass in ctx as a parameter for a command, not message.
Your #client.command() won't work because you never passed in ctx, by the way, to send a message you might want to use await ctx.send("Hello!").
The "full command" would look something like this, though its not really what you want as you would have to type .hi hi
#client.command()
async def hi(ctx, message):
if message.startswith('hi'):
await ctx.send('Hello!')
If you actually want to do that you should try using your #client.event (with on_message, basically the one you already have) as I dont think that you can do that unless you use aliases (Which is not a good option though):
#client.command(aliases=['hi!', 'hiiiiii', 'hello'])
async def hi(ctx):
await ctx.send('Hello!')
In conclusion, if you really want to do something like this, use your exsistent #client.event with on_message
(Just as #moinierer3000 said)

Trying to make a suggestion but channel id gives an error for the command

I've got an issue but I am getting no errors and from the code I just wrote as shown below
#client.commands
async def hello():
channel = int(797915093954199565)
await channel.send('Hey what are you doing?')
I am trying to make a command where the user can talk to the bot, and it responds back with something, its just a starting command but I'm having trouble with these small things, the rest of the bot works but its just this issue i'm having please help!
So assuming the rest of your bot and code works, your "hello" command isn't working because you have
#client.commands #its client.command in this case, as it seems you are not using any cogs, and you must call the client with () these double parentheses
async def hello(): #Here you have not passed ctx
channel = int(793744805615632394) #It seems what you would like to do here is send it to a specific channel, however, in the code below, I have set it so it just sends to the channel it was used in. The correct use is client.get_channel(793744805615632394)
await channel.send('Hey what are you doing?')
Here is the command but fixed:
So assuming the rest of your bot and code works, your "hello" command isn't working because you have
#client.command()
async def hello(ctx):
await ctx.send('Hey what are you doing?')
You should also refer to the py docs to learn more about it: https://discordpy.readthedocs.io/en/latest/

Resources