Answer the question
In order to leave comments, you need to log in
How to handle an error in discord.py when the data type is filled incorrectly?
For example, there is a command, and the user needs to fill in its first value with the int type. But you need to check that if the user enters not an int into the variable, but for example a string, then instead of an error in the console, he will receive a message that you need to enter a number, not letters. Please help with code
Answer the question
In order to leave comments, you need to log in
myVariable = input('Enter a number')
if type(myVariable) == int or type(myVariable) == float:
# Do something
else:
print('The variable is not a number')
Use an error handler as shown here . There will be something like this
from discord.ext.commands import CommandError, ConversionError
#обработчик команды
@bot.command()
#черная магия discord.py анализирует type hints чтобы понять, как парсить входное сообщение
async def plus(ctx, x: int, y: int): #указываем, что параметры команды - это целые числа
z = x + y
await ctx.send(f"{x} + {y} = {z}")
#обработчик ошибки
@plus.error
async def plus_error(ctx, error):
if isinstance(error, ConversionError):
await ctx.send(f"Ошибка преобразования аргументов plus. Вы ввели не числа?")
elif isinstance(error, CommandError):
await ctx.send(f"Ошибка выполнения команды plus")
else:
await ctx.send(f"Неизвестная ошибка выполнения команды plus")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question