【问题标题】:Discord.py "The command is already an existing command or alias"Discord.py "该命令已经是一个现有的命令或别名"
【发布时间】:2021-06-24 02:08:24
【问题描述】:
import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='$')

def read_token():
    with open("token.txt", "r") as f:
        lines = f.readlines()
        return lines[0].strip()

token = read_token()
client = discord.Client()

@bot.command()
async def commands():
    embed = discord.Embed(title="Commands")
    embed.add_field(name="$info", value="Shows info about the bot", inline=False)
    embed.add_field(name="$users", value="Shows how many members the server has", inline=False)
    await message.channel.send(content=None, embed=embed)

@bot.command()
async def info():
    await message.channel.send("Bot feito por Ra'Ed#2931")

@bot.command()
async def users():
    await message.channel.send(f"""Esse server tem {id.member_count} membros""")

bot.add_command(commands)
bot.add_command(info)
bot.add_command(users)

client.run(token)

当我运行此代码时,我会收到以下输出:

discord.ext.commands.errors.CommandRegistrationError:命令命令已经是现有命令或别名。

我能做些什么来解决这个问题?

【问题讨论】:

    标签: python discord


    【解决方案1】:

    您好,错误告诉您“命令”命令已经存在,因此请尝试将其从命令更改为帮助。

    我知道你没有添加它但你正在使用它例如:bot = commands.Bot(command_prefix='$')

    该代码将起作用:

    
    
    import discord
    from discord.ext import commands
    
    bot = commands.Bot(command_prefix='$')
    bot.remove_command('help')
    
    def read_token():
        with open("token.txt", "r") as f:
            lines = f.readlines()
            return lines[0].strip()
    
    token = read_token()
    client = discord.Client()
    
    @bot.command()
    async def help():
        embed = discord.Embed(title="Commands")
        embed.add_field(name="$info", value="Shows info about the bot", inline=False)
        embed.add_field(name="$users", value="Shows how many members the server has", inline=False)
        await message.channel.send(content=None, embed=embed)
    
    @bot.command()
    async def info():
        await message.channel.send("Bot feito por Ra'Ed#2931")
    
    @bot.command()
    async def users():
        await message.channel.send(f"""Esse server tem {id.member_count} membros""")
    
    bot.add_command(commands)
    bot.add_command(info)
    bot.add_command(users)
    
    client.run(token)
    

    我把它从“命令”改成了“帮助”

    【讨论】:

    • 不行,这次的问题是一样的,但是命令帮助“命令帮助已经是一个现有的命令或别名。”即使我把它的名字改成随机的。
    【解决方案2】:
    import discord
    from discord.ext import commands
    
    client = commands.Bot(command_prefix="$")
    
    def read_token():
        with open("token.txt", "r") as f:
            lines = f.readlines()
            return lines[0].strip()
    
    token = read_token()
    
    @client.command()
    async def comandos(ctx):
        embed = discord.Embed(title="Commands")
        embed.add_field(name="$info", value="Shows info about the bot", inline=False)
        embed.add_field(name="$users", value="Shows how many members the server has", inline=False)
        await ctx.send(content=None, embed=embed)
    
    @client.command()
    async def info(ctx):
        await ctx.send("Made by Ra'Ed#2931")
    
    @client.command()
    async def users(ctx):
        await ctx.send(f"""This server has {id.member_count} members""")
    
    client.run(token)
    

    为此更改代码,将参数 ctx 添加到函数中,从 bot 更改为 client,删除 bot.add_command(),现在它可以正常工作了

    【讨论】:

      【解决方案3】:

      所有错误意味着有一个同名的命令或其别名中的名称已经存在。您要么必须查找并更改命令名称,要么更改当前命令名称。您不能有 2 个具有相同名称的命令。我不知道你的另一个命令是什么,所以我也不能告诉你太多。另外,没有理由添加bot.add_command("command_name")..它会自动被理解。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-23
        • 2021-01-24
        • 1970-01-01
        • 2022-10-01
        • 2021-11-05
        • 1970-01-01
        • 2021-05-21
        相关资源
        最近更新 更多