Purge Command Can Be Used By Anyone - discord.py

I have created a "clear" function and it has ctx.channel.purge() but it seems like anyone can use it with out any permission.
Any idea on how can I prevent it?
I only want that only members that have the permission manage channeles to be able to use it

Use the #commands.has_permissions() decorator:
#bot.command() # or #commands.command()
#commands.has_permissions(manage_channels=True)
async def clear(ctx: commands.Context):
ctx.channel.purge()

You can use #commands.has_role() or #commands.has_permissions() to allow a specific role or permission to use that command.
With commands.has_role:
#commands.has_role('Role name') # or you can use role id
#client.command()
async def test(ctx):
With commands.has_permissions:
#commands.has_permissions(ban_members=True)
#client.command()
async def test(ctx):
Or you can use commands.has_any_role(). If member has any of these roles that you input as an parameter, they are allowed to execute this command.
#commands.has_any_role('Role1', 'Role2')
#client.command()
async def test(ctx):

You can do #commands.has_role() or #commands.has_permissions(). Like it says, #commands.has_role() checks if the user has a role, and #commands.has_permissions() checks if the user has a permission, like read messages, send messages, manage server, etc.

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

Blacklist user id from using discord command discord.py

Ok so i am making a bot for my friends server PBC and i wanted to blacklist a user with there
user id from using ONE COMMAND and they can use every other one so if you have anything that can help please help me thank you.
There is a very simple way of doing this with just an if statement
if ctx.author.id != [put the users id here]:
await ctx.respond("You do not have permission to use this command")
return
Place this at the very top of the command to block the user from using the command.
just use #commands.check()
like this
def in_blacklist(ctx):
return not str(ctx.message.author.id) in ['id1', 'id2'......]
#bot.command() #or #commands.command()
#commdnds.check(in_blacklist)
async def your_command(ctx):
#do something....

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)

How to send DM to command author?

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.

Resources