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

如何用Python制作Discord机器人指令列表及实现类似FredBoat的指令列表

用Python制作Discord机器人指令列表(含FredBoat风格实现)

一、基础环境准备

  • 安装discord.py库:pip install discord.py
  • 提前在Discord开发者平台创建机器人并获取Token,开启Message Content Intent权限

二、基础指令列表实现

先做一个简单的!help指令,返回所有可用指令的基础列表:

import discord
from discord.ext import commands

# 配置机器人权限与前缀
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)

# 定义测试指令
@bot.command(name='ping')
async def ping(ctx):
    await ctx.send('Pong!')

@bot.command(name='hello')
async def hello(ctx):
    await ctx.send(f'Hello {ctx.author.mention}!')

# 基础帮助指令
@bot.command(name='help')
async def help_command(ctx):
    help_text = """
**可用指令列表**
- `!ping`:返回Pong!,测试机器人连通性
- `!hello`:机器人向你发送打招呼消息
- `!help`:显示此帮助信息
    """
    await ctx.send(help_text)

# 启动机器人
bot.run('你的机器人Token')

三、实现FredBoat风格的分类式指令列表

参考你提供的FredBoat指令列表(分音乐、管理、工具等分类),我们通过commands.Cog按模块组织指令,实现分类展示:

1. 按分类拆分指令模块

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)

# 音乐分类模块
class MusicCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name='play')
    async def play(self, ctx, *, song):
        await ctx.send(f'正在播放:{song}')

    @commands.command(name='skip')
    async def skip(self, ctx):
        await ctx.send('已跳过当前歌曲')

# 管理分类模块
class ModerationCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name='kick')
    async def kick(self, ctx, member: discord.Member):
        await ctx.send(f'已踢出成员:{member.display_name}')

    @commands.command(name='ban')
    async def ban(self, ctx, member: discord.Member):
        await ctx.send(f'已封禁成员:{member.display_name}')

# 工具分类模块
class UtilityCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name='ping')
    async def ping(self, ctx):
        await ctx.send(f'延迟:{round(bot.latency*1000)}ms')

    @commands.command(name='avatar')
    async def avatar(self, ctx, member: discord.Member=None):
        member = member or ctx.author
        await ctx.send(member.avatar.url)

# 加载所有分类模块
async def setup():
    await bot.add_cog(MusicCog(bot))
    await bot.add_cog(ModerationCog(bot))
    await bot.add_cog(UtilityCog(bot))

2. 实现分类式帮助指令(带详情查询)

# 给指令添加描述文本(用于帮助详情)
@ModerationCog.kick.short_doc = "踢出指定服务器成员,需管理员权限"
@ModerationCog.ban.short_doc = "封禁指定服务器成员,需管理员权限"
@MusicCog.play.short_doc = "根据关键词或URL播放音乐"
@MusicCog.skip.short_doc = "跳过当前正在播放的音乐"
@UtilityCog.ping.short_doc = "查看机器人与Discord服务器的延迟"
@UtilityCog.avatar.short_doc = "查看指定成员的头像,不填则查看自己的"

# 分类式帮助指令(支持指定分类查看详情)
@bot.command(name='help')
async def help_command(ctx, category=None):
    # 若指定分类,展示该分类下的详细指令
    if category:
        for cog in bot.cogs.values():
            clean_cog_name = cog.__class__.__name__.replace('Cog', '')
            if clean_cog_name.lower() == category.lower():
                embed = discord.Embed(title=f"{clean_cog_name} 分类指令", color=discord.Color.blue())
                for command in cog.get_commands():
                    embed.add_field(name=f"`!{command.name}`", value=command.short_doc, inline=False)
                await ctx.send(embed=embed)
                return
        await ctx.send(f"未找到名为「{category}」的分类")
        return
    
    # 默认展示所有分类及指令概览
    embed = discord.Embed(title="FredBoat风格指令列表", color=discord.Color.blue())
    for cog_name, cog in bot.cogs.items():
        clean_name = cog_name.replace('Cog', '')
        command_list = "\n".join([f"`!{cmd.name}`" for cmd in cog.get_commands()])
        embed.add_field(name=clean_name, value=command_list, inline=False)
    embed.set_footer(text="使用 !help [分类名] 查看对应分类的详细指令,如 !help Music")
    await ctx.send(embed=embed)

# 启动机器人
bot.run('你的机器人Token')

四、优化与扩展建议

  • 若要使用Discord官方斜杠指令,可改用discord.app_commands装饰器替换commands.command,体验更贴合平台规范
  • 给敏感指令添加权限验证,比如管理类指令可加@commands.has_permissions(kick_members=True)装饰器,限制仅管理员使用
  • 可添加指令别名,比如给!help添加!h别名:@bot.command(name='help', aliases=['h'])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 19:57:35