Discord.py该命令已是一个现有的命令或别名.错误



我是python的新手,标题中有一个错误。我知道问题出在哪里,但我想让一个命令根据用户在命令中提供的论点提供不同的结果。

例如,如果它将是"!sens 20 cm 800 dpi"机器人回复"{}ingame"但如果"!sens 10 ingame 800 dpi"机器人回复"{}cm">

我不想用不同的名字做两个命令。

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)

为了解决这类问题,您可以创建一个if-else语句进行检查,如下面的代码

@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")

最新更新