Just want a reply when u enter a command wrong - discord.py

I'm not the most knowledgeable with discord.py, I have been doing research on coding following what other people do and say, but it comes to the point where i now need to ask for help.
As i said, I don't know much, all i know is what I've written, all i want to know is when u enter a command wrong, it replies with "what is that". what i have entered doesn't work,
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Please pass in all requirements :rolling_eyes:.')
if isinstance(error, commands.MissingPermissions):
await ctx.send("You dont have all the requirements :angry:")
if isinstance(error, commands.NotFound):
await ctx.semd("what is this :face_vomiting:")
just wondering if anyone can help, because i am new with this.

This should work :
if isinstance(error, commands.CommandNotFound):
await ctx.send("what is this :face_vomiting:")

Related

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....

I am making a discord.py rewrite help command. But when i try to send the embed i get a weird response

I'm trying to make a help command for my bot like the below:
#client.command()
async def help(ctx):
embed=discord.Embed(title="Help Command", description="The discord bot help command", color=0xe8d9d9)
embed.add_field(name="Slowmode", value="To add on slowmode do .delay (your amount of seconds for slowmode here)", inline=False)
embed.add_field(name="Ban Members", value="To ban members do .ban #usernamehere reasonhere", inline=False)
embed.add_field(name="Kick Members", value="To ban members do .kick #usernamehere reasonhere", inline=False)
embed.add_field(name="8Ball", value="To use the 8Ball do .8ball questionhere then the bot will answer you", inline=True)
embed.add_field(name="Bot Latency", value="To get the ping do .ping and it will show you the ping in ms", inline=True)
await ctx.send(embed)
When I use it, however, the bot responds in the channel with <discord.embeds.Embed object at 0x000001D29A1588B0> and not the embed
As mentioned in by Lukas Thaler, you made mistake in sending the embed. It should be await ctx.send(embed=embed).
Hope this helps!

How can I mention whoever was mentioned in the command trigger?

I'm trying to have my discord.py bot mention a member in a suspension from the server, then give them the suspended role. However, I don't know what to put in the embed and even if I just put something in there, it won't send the message. I also am not sure if it will role. Here's my code:
#bot.command(pass_ctx=True)
#commands.has_permission(administrator=True)
async def suspend(ctx, *, self, member = discord.Member):
embed = discord.Embed(
colour = discord.Colour.red(title='Successful Suspension'))
[
embed.add_field(name='Details', value='')]
await ctx.send(embed=embed)
await member.add_roles('Suspended')
else:
await ctx.send('Insufficient Permissions.')
I also get a syntax error on the else: function.
First of all I'm going to answer your question. You can use discord.Member.mention (In your case you can simply write member.mention) to mention someone.
Secondly, I'm going to help with your embed:
The arguments in the function are messy.
Why did you put a title inside discord.Colour.red()? Thats a colour, and it's used for changing your embed sidebar color (Also you can't change your text color if that's what you were trying to do).
In embed fields you cannot have nothing in the value. value=''
You have a small typo, it's has_permissions()
I don't like spoonfeeding, but your code should look something like this:
#bot.command(pass_ctx=True)
#commands.has_permissions(administrator=True)
async def suspend(self, ctx, *, member = discord.Member):
embed = discord.Embed(title='Succesful Suspension', colour=discord.Colour.red())
embed.add_field(name='Details', value='A')
await ctx.send(embed=embed)
await member.add_roles('Suspended')
I alsoremoved the last part of your code because It didn't make sense. See error handling.
I think this is all, hope that this worked and I recommend you to have a look at the docs.
If someone finds anything wrong, tell me in the comments.

How to implement Patreon-only commands for a discord bot?

So i have seen another post with almost the exact same question however, mine is different.
I am fully aware there is a Patreon bot, however to my knowledge this is only valid on servers.
So lets say someone invites my bot, and tries a command that requires them to be a patron. How could my bot written in python do a check to see if they have become a patron for my product? And then set a role for them accordingly. Which i can then do the check on to allow them access to the command or not.
So essentially, it should do what the Patreon bot does, however would work on its own. Examples are such as the Dank Memer bot: which can be invited to any server and if one becomes a patron can use specific commands, otherwise you can't
I've looked around this topic for a while now and haven't been able to find any info on how to check if the user has become a patron or not.
Many thanks in advance!
If you know the patreons. just give them a role [patreons] or any name!
after giving them the role. copy the role id and paste it in patreons_role_id
ok, what this command does is, just it check for the [patreons] role in the member roles! if the [patreons] role is present! it will execute the #your code else it sends a custom message!
#client.event()
async def on_message(message):
if message.content == '!test':
is_patreon = False
for user_roles in message.author.roles:
if user_roles.id == patreons_role_id:
is_patreon = True
break
if is_patreon == True:#your code
else: await message.channel.send('THIS COMMAND IS ONLY AVAILABLE FOR PATREON!')
Let's assume you have a role for your patreons.
#client.command()
#commands.has_roles("PatreonRole")
async def commandname(ctx, args):
#do stuff
There is a way out, but its probably not the right one, so, If you know who your patreons are and you know their discord IDs as well, you can declare an if statement in the command similar to this -
#client.command()
async def your_command_name():
if member.id == #Your 1st Patreon's Discord ID:
#Your Code
elif member.id == #Your 2nd Patreon's Discord ID:
#Your code
else:
await ctx.send("You cannot use that command as you are not a patreon!")
But again this is probably not right way if you have a lot of patreons or you dont know their Discord IDs, but anyways this way was the only way I could come up with.
I hope that helped. :)

How to check if bot is connected to a channel? | discord.py

I've decided to try making my discord bot play music, but I've gotten stuck already. Mainly due to the fact I can't find any sources to help with the current version, I've been winging everything from the docs. However, I can't figure out how to check if the bot is connected to a voice channel.
I have tried if not Client.is_connected():, however that didn't work. If there are any updated sources to help me get the basics of discord.py's voice features, please give me a link :) Here is my code so far:
# ----- ATTEMPT AT VOICE COMMANDS ------
#discord.opus.load_opus() - what goes in bracket???
#client.command(name="join", pass_ctx=True)
async def join(ctx):
#if not is_connected(): - Client.is_connected() not working
user = ctx.message.author
vc = user.voice.channel
await vc.connect()
await ctx.send(f"Joined **{vc}**")
#else:
# await ctx.send("I'm already connected!")
#client.command(name="disconnect", pass_ctx=True)
async def disconnect(ctx):
# if not is_connected(): - once again can't work it out
vc = ctx.message.guild.voice_client # i don't even know how this worked :D
await vc.disconnect()
#else:
# await ctx.send("I'm not connected to any channels")
#client.command(name="play", pass_ctx=True)
async def play(ctx, songurl=None):
if not songurl: # this works at least
await ctx.send("Please specify a song")
return
if not is_connected(): # once again, how to check if bot is connected?
vc = ctx.message.author.voice.channel
if not vc: # i think this should work
await ctx.send("You're not in a voice channel!")
await vc.connect()
# haven't even worked out anything past this point and it's broken
ps: sorry for just dumping my whole vc section but i don't understand a lot
Really all that matters here is the play command, but I included the others just because (as you can see from my comments) I don't understand LOTS of what is going on. How should I go about this? Are there any good sources for the current version? Thanks in advance.
A bot can be connected to voice in multiple guilds at the same time, so you need to get the VoiceClient for the appropriate guild from Client.voice_clients and then check VoiceClient.is_connected:
def is_connected(ctx):
voice_client = get(ctx.bot.voice_clients, guild=ctx.guild)
return voice_client and voice_client.is_connected()
you could also do
client.command()
async def join(ctx):
user = ctx.message.author
vc = user.voice.channel
voice = discord.utils.get(client.voice_clients, guild=ctx.guild) # This allows for more functionality with voice channels
if voice == None: # None being the default value if the bot isnt in a channel (which is why the is_connected() is returning errors)
await vc.connect()
await ctx.send(f"Joined **{vc}**")
else:
await ctx.send("I'm already connected!")
After a bit of experimentation with my bot, I found that something similar to this may work for you. It's also pretty quick and easy.
if ctx.guild.voice_client in bot.voice_clients:
# Do something here
I used this with the on_message event, but it could likely work in your use case as well by doing:
if not ctx.guild.voice_client in bot.voice_clients:
# Do something else here

Resources