errors in discord py

Solutions on MaxInterview for errors in discord py by the best coders in the world

showing results for - "errors in discord py"
Breck
23 Aug 2019
1# This will be a quick example of how to handle errors in discord.py 
2import discord
3from discord.ext import commands
4# make sure to import these ^
5
6# Let's say we have a kick command
7@client.command()
8@commands.has_any_role('Admin') 
9async def kick(ctx, member : discord.Member, *, reason=None):
10    await member.kick(reason=reason)
11    await ctx.send('''
12Kicked user: {}
13Reason: {}'''.format(member, reason))
14    
15# Okay, so it works. But what if someone does !kick without the arguments
16# We will get an error. So let's handle that.
17
18# I will show 2 ways 
19
20# Here is the first one. 
21# This way will handle EVERY missing arguments error
22@client.event
23async def on_command_error(ctx, error):
24  
25    if isinstance(error, commands.MissingRequiredArgument):
26        await ctx.send('Make sure you put the required arguments.')
27        # Now, if someone uses any command and doesn't put the
28        # Required arguments, then this event will occur 
29        
30# Here is the second one
31# This way will handle one command's error 
32@kick.error
33async def kick_error(ctx, error):
34  if isinstance(error, commands.MissingRequiredArgument):
35    await ctx.send('To use the kick command do: !kick <member>')
36    # Using this method we can account for all our commands individually 
37    # I recommend using this method, however both methods work basically the same
38    # For example if we had a ban command, we could do the same 
39    # Doing @ban.error and accounting for that one as well!
Jacobo
15 Apr 2019
1CHECK OUT 'https://realpython.com/lessons/handling-exceptions/'
queries leading to this page
discord py exceptiondiscord py error handlingerror handling discord pydiscord py try except get errordiscord py error handlingerror handling discor pythondiscord py exceptionscatch on message error discord pyhow to catch error in discord pyerror handling discord pymake discord py exceptionserror message discord pytry except discord error pythondiscord py error handling with expecterror object in discord pyhandling exceptions discord pyerrors in discord pydiscord handling errors with discord pydiscord py errorerror command discord pydiscord py command errorsdiscord py all errorshow to make a on error event in discord pyhow to use error discord pypython discord catch command errorrserror handling python discordshow errors discord pydiscord python api error handlingdiscordpy handle command errorerror handling discordpyall command errors discord pyhow to check for error discord pydiscord py error eventall discord py errorsif error in python discord pydiscord py list of errorscatch clientexception 3a errors discord pyon error discord pydiscord python 40errorerror check discord pyall command handling error discord pyerror messages discord pydiscord py error handlerall discord py error handlingerror handleing discord pyhandling event error discord pyerrors with discord pydiscord command error handlinghttp exception in running discord pydisplay error in discord pydiscord py error handlediscord py list or errorscatch errors discord pythonget error discord pydiscord 2cpy exceptionsdiscord py exception handlingdiscord py serror handlinghow does discord py collect errorserrors discord pyerror handling in discord pyerror handling discord pydiscord python error handlingdiscord python errordiscord py errorerror handler discord pydiscord py catch errorexcept exception as error 3a discord pywhy dont i see errors discord pydiscord py event errordiscord py errorsdiscord py on errorall errors discord pyhow to handle errors discord pyall error handlings discord pypython catch discord exceptionhow to make errors with discord pysending error messages discord pyhow did discord py catch errorserror discord pydiscordpy error handlinghow to add errors to discord pypython errors with discord pyon error for a command discord pyerrors in discord py