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

能否通过discord.py内置功能实现双语帮助命令?

使用discord.py实现双语帮助消息的内置扩展方案

discord.py原生帮助系统没有直接提供多语言切换功能,但可以通过自定义帮助命令结合命令别名的方式,实现类似的内置式解决方案,无需单独编写两个独立命令。

实现思路

  1. 自定义继承自commands.HelpCommand的帮助类,在类中根据用户触发的命令别名(!help或!hilfe)判断输出语言。
  2. 给默认帮助命令添加hilfe别名,让两个指令触发同一套自定义帮助逻辑。

代码示例

from discord.ext import commands

# 多语言帮助文本(可按需扩展)
lang_content = {
    "en": {
        "title": "**Bot Help**",
        "help_cmd": "!help - Display this help message",
        "hello_cmd": "!hello - Send a greeting message"
    },
    "de": {
        "title": "**Bot Hilfe**",
        "help_cmd": "!hilfe - Zeige diese Hilfe-Nachricht",
        "hello_cmd": "!hello - Sende eine Begrüßungsnachricht"
    }
}

class BilingualHelp(commands.HelpCommand):
    async def send_bot_help(self, mapping):
        # 根据触发的命令别名判断语言
        lang = "de" if self.invoked_with.lower() == "hilfe" else "en"
        content = lang_content[lang]
        
        # 拼接并发送帮助消息
        help_msg = f"{content['title']}\n{content['help_cmd']}\n{content['hello_cmd']}"
        await self.get_destination().send(help_msg)

# 初始化Bot并绑定自定义帮助命令
bot = commands.Bot(command_prefix="!", help_command=BilingualHelp())
# 给帮助命令添加德文别名
bot.help_command.add_command_alias("hilfe")

# 示例命令
@bot.command()
async def hello(ctx):
    await ctx.send("Hello!" if ctx.invoked_with != "hilfe" else "Hallo!")

bot.run("YOUR_BOT_TOKEN")

优势说明

  • 复用同一套帮助逻辑,避免重复代码,比单独实现两个命令更简洁。
  • 基于discord.py内置的帮助命令扩展,属于原生生态内的解决方案,无需额外依赖。
  • 可轻松扩展更多语言,只需在lang_content字典中添加对应语言的文本即可。

内容的提问来源于stack exchange,提问作者Xaver

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 15:20:29