Search code examples
discorddiscord.pybots

Discord.py The command is already an existing command or alias. error


I'm new to python, I'm getting an error in the title. I know what the problem is but I want to make one command providing different results based on what arguements the user provided in the command.

Like for example if it's going to be '!sens 20 cm 800 dpi' the bot replies '{} ingame' but if '!sens 10 ingame 800 dpi' the bot replies '{} cm'

I dont want to make two commands with different names.

 import math
    import discord
    from discord.ext import commands
    TOKEN = ''
    bot = commands.Bot(command_prefix='!')
    d= 166461.6
    @bot.command(name="sens")
    async def sens_cm(ctx, x: float,cm, y: float,dpi):
        await ctx.send('{} ingame'.format(d/(x*y)))
    @bot.command(name="sens")
    async def sens(ctx, x1: float,ingame, y1: float,dpi):
        await ctx.send('{} cm'.format(d/(x1*y1)))
    bot.run(TOKEN)

Solution

  • Inorder to fix this type of problem you can create an if-else statement to check, like in the below code

    @bot.command(name="sens")
    async def sens_cm(ctx, x: float,cm:str, y: float,dpi):
      if cm=="ingame":
           await ctx.send(f'{d/(x*y)} cm')
      elif cm=="cm":
           await ctx.send(f'{d/(x*y)} ingame')
      else:
           await ctx.send("Invalid argument")