one if these permissions is required discord py - discord.py

I want the bot to check if the command auther has the check or the permission
#commands.check(mefle7)
#commands.has_permissions(manage_messages=True)

Just use #commands.has_permissions(manage_messages=True)
Example:
#bot.command()
#commands.has_permissions(manage_messages=True)
async def clear(ctx, amount):
await ctx.channel.purge(limit=amount)

Related

How to make a bot respond exactly what you say in discord.py

im trying to get my Discord bot to say exactly what i want with the command of !say excluding the !say part but im new to coding and have no idea where to start. I know the basics of making a bot say/react/delete/DM messages but thats all
Try this:
#commands.command()
async def say(self, ctx, *, arg):
await ctx.send(arg)
The full code would look something like this:
from discord.ext import commands
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
#bot.event
async def on_ready():
print("bot is ready")
#commands.command()
async def say(self, ctx, *, arg):
await ctx.send(arg)
bot.run("YOURTOKEN")

Python command only work in direct messages?

So I want to be able to have my command only work in direct messages, can anyone help?
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('bb!help'):
await message.channel.send('Huge long commands list')
Use isinstance().
isinstance checks whether if an object has the specified type. For example, isinstance(3, int) checks if 3 is an integer (which it is and therefore returns True). In your case you could check if the message.channel is a DMChannel. The correct code would be:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('bb!help') and isinstance(message.channel, discord.DMChannel):
await message.channel.send('Huge long commands list')
If you're using commands.Bot
#client.command()
#commands.dm_only()
async def command(ctx):
...
If you're using on_message
#client.event
async def on_message(message):
if isinstance(message.channel, discord.DMChannel):
# The message is sent in the dm's of the bot

async def on_ready(): SyntaxError: invalid syntax

I was making an discord bot and im new to python and i have looked up but didn't find any answer on how to fix this but i got error from this code
Code:
import discord
from discord.ext import commands
client = command.Bot(command_prefix="<")
#client.event
async def on_ready():
print("Bot is online")
#client.command()
synd def Test(ctx):
await ctx.send("Test successful")
client.run(Token)
line with error:
#client.event
async def on_ready():
print("Bot is online")
Error:
async def on_ready():
^
SyntaxError: invalid syntax
Does anyone know how to fix this? i am using Python version: pip 19.2.3 (python 3.8)
EDITED
The print function needs to be intended another 4 spaces to be like:
async def on_ready():
print("Bot is online")
you can try use command "python3 file_name.py"

How do I check if arguments are given?

I want to check if the user has given two arguments, the first being a user to ban (mentioned) and a reason.
If a user is not mentioned, it will show a message saying "You must mention a user you'd like to ban" and if a reason isn't given, it will ban the user with the message "No Reason Given".
I'd also like to make it so you can't ban yourself, if not already implemented by discord.py
I've tried using an if args == None: statement, but it doesn't work.
#commands.has_permissions(ban_members=True)
#client.command()
async def ban(ctx, member: discord.Member, *, arg):
embed = discord.Embed(title="Punishments", color=0x3b55e8)
embed.add_field(name=f"{member} has been banned for:", value=arg)
embed.set_footer(text=strftime("%Y-%m-%d %H:%M:%S", gmtime()))
await ctx.send(embed=embed)
await member.ban()
#ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
embed = discord.Embed(title="Error", color=0x3b55e8)
embed.add_field(name="You do not have permission to use this command", value="If you think this is incorrect, please contact a server administrator")
embed.set_footer(text=strftime("%Y-%m-%d %H:%M:%S", gmtime()))
await ctx.send(embed=embed)
print(f"{ctx.author} has attempted to ban a player, but does not have the correct permissions")
I don't receive any errors.
You can set a default value for your reason, and add an error handler to handle the failure when a required argument is not passed:
#commands.has_permissions(ban_members=True)
#client.command()
async def ban(ctx, member: discord.Member, *, arg="No Reason Given"):
embed = discord.Embed(title="Punishments", color=0x3b55e8)
embed.add_field(name=f"{member} has been banned for:", value=arg)
embed.set_footer(text=strftime("%Y-%m-%d %H:%M:%S", gmtime()))
await ctx.send(embed=embed)
await member.ban()
#ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
embed = discord.Embed(title="Error", color=0x3b55e8)
embed.add_field(name="You do not have permission to use this command", value="If you think this is incorrect, please contact a server administrator")
embed.set_footer(text=strftime("%Y-%m-%d %H:%M:%S", gmtime()))
await ctx.send(embed=embed)
print(f"{ctx.author} has attempted to ban a player, but does not have the correct permissions")
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send("You must mention a user you'd like to ban")
else:
raise error
Patrick Haugh has already answered the question but best you create an error.py and insert all the global errors there.
Link to the documentation (CommandErrors)
This can look like this:
import discord
from discord.ext import commands
class Error(commands.Cog):
def __init__(self, bot):
self.bot = bot
#commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f"You don't have the permission to execute this bot command!"))
if isinstance(error, commands.CommandNotFound):
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f"The bot command doesn't exist!"))
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f"The command is incomplete, missing one or more parameters!"))
if isinstance(error, commands.BadArgument):
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f"The command was entered incorrectly, one or more parameters are wrong or in the wrong place!"))
def setup(bot):
bot.add_cog(Error(bot))

on_message stops all of my bot.commands from working

I have a flip command that returns either heads or tails and it was working fine until I added the on_message function. I did some research on SO and read the documentation and here it says to include await bot.process_commands(message) in the on_message function but that did not solve the issue and the flip command still doesn't work. If I remove the on_message function everything works as expected.
bot.py
import discord
from discord.ext import commands
import random
# from app bot user
# click to reveal
TOKEN = 'token_here'
client = commands.Bot(command_prefix = '!')
#client.event
async def on_ready():
print('Bot ready')
#client.command()
async def flip():
flip = random.choice(['heads', 'tails'])
await client.say(flip)
#client.event
async def on_message(message):
if message.content.upper().startswith('N****'):
await client.send_message(message.channel, "Please do not use that word here.")
client.process_commands(message)
#client.event
async def on_member_join(member):
await client.change_presence(game=discord.Game(name='Hi %s' % (member)))
await client.send_message(member, "Hi %s, Welcome to Carson's Discord Server! This server is fairly NSFW at times; you've been warned! Enjoy your stay :)" % (member))
#client.event
async def on_member_remove(member):
await client.change_presence(game=discord.Game(name='Bye %s' % (member)))
client.run(TOKEN)
When I run the script, everything else works normally with no errors. It's just the commands that don't work. Any insight would be greatly appreciated.
You need to await the last line in on_message as process_commands is a coroutine.
await client.process_commands(message)
If you send !flip in discord, you should be able to receive the appropriate response (heads or tails).

Resources