You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nextcord Bot自定义前缀显示异常:help命令输出函数地址而非前缀

问题解决方法

核心原因是bot.command_prefix指向的是你的get_prefix函数而非实际前缀字符串,所以直接拼接会显示函数对象。以下是两种可靠的解决方式:

方式一:利用ctx.prefix(推荐)

当命令被触发时,Nextcord会自动获取当前服务器的前缀并存储在ctx.prefix中,直接使用这个属性即可拿到实际前缀字符串:

@bot.command()
async def help(ctx):
    # 直接使用ctx.prefix获取当前服务器的实际前缀
    help_content = f"""
当前服务器前缀:{ctx.prefix}
可用命令:
{ctx.prefix}set_prefix <新前缀> - 修改服务器前缀
{ctx.prefix}help - 查看帮助信息
"""
    await ctx.send(help_content)

方式二:手动调用get_prefix函数

如果你的get_prefix是同步函数,直接传入bot和ctx调用;如果是异步函数,需要加await:

同步get_prefix场景

def get_prefix(bot, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    return prefixes.get(str(message.guild.id), '!')

@bot.command()
async def help(ctx):
    current_prefix = get_prefix(bot, ctx)
    help_content = f"使用方法:{current_prefix}command1, {current_prefix}command2"
    await ctx.send(help_content)

异步get_prefix场景

async def get_prefix(bot, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    return prefixes.get(str(message.guild.id), '!')

@bot.command()
async def help(ctx):
    current_prefix = await get_prefix(bot, ctx)
    help_content = f"使用方法:{current_prefix}command1, {current_prefix}command2"
    await ctx.send(help_content)

自定义全局HelpCommand(覆盖默认help)

如果需要替换默认的help命令,可通过自定义HelpCommand类实现,同样使用self.context.prefix获取前缀:

from nextcord.ext import commands

class CustomHelp(commands.HelpCommand):
    async def send_bot_help(self, mapping):
        prefix = self.context.prefix
        help_msg = f"📢 当前服务器前缀:{prefix}\n\n"
        for cog, cmds in mapping.items():
            cog_name = cog.qualified_name if cog else "通用命令"
            help_msg += f"**{cog_name}**\n"
            for cmd in cmds:
                help_msg += f"{prefix}{cmd.name} - {cmd.short_doc or '无描述'}\n"
        await self.context.send(help_msg)

# 绑定到机器人
bot.help_command = CustomHelp()

内容的提问来源于stack exchange,提问作者Daniel S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 06:43:20