如何使用command_prefix='!'创建Discord多参数自定义命令?
解决方案
一、改用discord.py的Commands框架实现命令
别用on_message硬写前缀判断了,官方的Commands框架专门处理带参数的命令,更省心。先把你的代码改成这样:
import discord from discord.ext import commands # 开启必要的intents,v2版本必须设置,否则收不到消息内容 intents = discord.Intents.default() intents.message_content = True # 初始化Bot,前缀设为! client = commands.Bot(command_prefix='!', intents=intents) # 原来的!hi命令转成Commands格式,更简洁 @client.command() async def hi(ctx): await ctx.send(f"Hi, {ctx.author.display_name}!") # 实现!creeds命令,支持无参数/带参数两种调用 @client.command() async def creeds(ctx, creed_name=None): # 没有输入参数时,返回所有可用信条列表 if creed_name is None: await ctx.send("可用信条:Nicene(尼西亚信经)、Athanasian(亚他那修信经)") else: # 统一转小写,避免大小写敏感问题 creed_name = creed_name.lower() if creed_name == "nicene": await ctx.send("尼西亚信经内容...(替换成实际文本即可)") elif creed_name == "athanasian": await ctx.send("亚他那修信经内容...(替换成实际文本即可)") else: await ctx.send(f"找不到名为{creed_name}的信条,请输入!creeds查看列表") # 启动机器人,替换成你的机器人Token client.run("你的机器人Token")
二、对接SQLite3数据库
等数据库建好后,把信条内容存入creeds表(字段建议设为name和content),然后修改命令逻辑从数据库查询:
import sqlite3 # 封装查询信条的工具函数 def get_creed(creed_name): conn = sqlite3.connect('creeds.db') cursor = conn.cursor() cursor.execute("SELECT content FROM creeds WHERE name = ?", (creed_name.lower(),)) result = cursor.fetchone() conn.close() return result[0] if result else None # 修改后的creeds命令 @client.command() async def creeds(ctx, creed_name=None): if creed_name is None: # 查询所有信条名称,返回列表 conn = sqlite3.connect('creeds.db') cursor = conn.cursor() cursor.execute("SELECT name FROM creeds") creed_list = [row[0] for row in cursor.fetchall()] conn.close() await ctx.send(f"可用信条:{', '.join(creed_list)}") else: content = get_creed(creed_name) if content: await ctx.send(content) else: await ctx.send(f"找不到名为{creed_name}的信条,请输入!creeds查看列表")
三、用Cogs模块化管理命令
之前用Cogs没成功?大概率是没正确加载模块。按下面步骤来:
- 创建
creeds_cog.py文件,把信条命令封装进去:
from discord.ext import commands import sqlite3 class CreedsCog(commands.Cog): def __init__(self, bot): self.bot = bot def get_creed(self, creed_name): conn = sqlite3.connect('creeds.db') cursor = conn.cursor() cursor.execute("SELECT content FROM creeds WHERE name = ?", (creed_name.lower(),)) result = cursor.fetchone() conn.close() return result[0] if result else None @commands.command() async def creeds(self, ctx, creed_name=None): if creed_name is None: conn = sqlite3.connect('creeds.db') cursor = conn.cursor() cursor.execute("SELECT name FROM creeds") creed_list = [row[0] for row in cursor.fetchall()] conn.close() await ctx.send(f"可用信条:{', '.join(creed_list)}") else: content = self.get_creed(creed_name) if content: await ctx.send(content) else: await ctx.send(f"找不到名为{creed_name}的信条,请输入!creeds查看列表") # 加载Cog的固定写法 async def setup(bot): await bot.add_cog(CreedsCog(bot))
- 在主文件里加载这个Cog:
import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True client = commands.Bot(command_prefix='!', intents=intents) # 机器人就绪时加载Cog @client.event async def on_ready(): await client.load_extension('creeds_cog') print(f"已登录为 {client.user}") @client.command() async def hi(ctx): await ctx.send(f"Hi, {ctx.author.display_name}!") client.run("你的机器人Token")
提示:运行前确保discord.py是最新版本,用pip install -U discord.py更新即可
内容的提问来源于stack exchange,提问作者OrthoBotHistorian
相关产品推荐
相关产品推荐

