discord.py如何创建多个Embed及首个指令无响应问题解决
问题原因
你重复注册了两次on_message事件回调,后定义的函数会直接覆盖先定义的函数,因此只有第二个针对.week指令的判断逻辑会生效,第一个.kade的逻辑完全被覆盖不会执行。
可行解决方案
方案1:合并消息判断逻辑到同一个on_message事件
将所有指令的判断逻辑都放到同一个on_message回调内即可,代码示例:
import discord from discord.ext import commands # 高版本discord.py要求显式配置intents,需同时在Discord开发者后台开启对应权限 client = commands.Bot(command_prefix = '.', intents=discord.Intents.all()) @client.event async def on_ready(): await client.change_presence(status=discord.Status.online, activity=discord.Game('private')) print('bot is online') @client.event async def on_message(message): # 避免机器人自己触发自己的指令 if message.author.bot: return if message.content.startswith('.kade'): embedVar = discord.Embed(title="Kade Engine Download", description="Downloads for Kade Engine 1.8", color=0x2C93FA) embedVar.add_field(name="Kade Engine Installation", value="https://github.com/KadeDev/Kade-Engine/releases/tag/1.8", inline=False) embedVar.add_field(name="Kade Engine Source (Recommended for Mod Makers)", value="https://github.com/KadeDev/Kade-Engine", inline=False) await message.channel.send(embed=embedVar) if message.content.startswith('.week'): embedVar = discord.Embed(title="How to make a week", description="How to make a week", color=0x2C93FA) embedVar.add_field(name="test", value="test", inline=False) embedVar.add_field(name="test", value="test", inline=False) await message.channel.send(embed=embedVar) # 如果后续还要使用commands框架装饰器注册指令,需保留下方代码 await client.process_commands(message) client.run('WONT SHOW BOT TOKEN')
方案2:使用commands框架原生指令注册(更推荐)
你已经引入了commands.Bot,可以直接使用框架自带的指令注册逻辑,不需要手动判断消息前缀,后续新增指令更易维护,代码示例:
import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True client = commands.Bot(command_prefix = '.', intents=intents) @client.event async def on_ready(): await client.change_presence(status=discord.Status.online, activity=discord.Game('private')) print('bot is online') @client.command(name='kade') async def kade_engine_download(ctx): embedVar = discord.Embed(title="Kade Engine Download", description="Downloads for Kade Engine 1.8", color=0x2C93FA) embedVar.add_field(name="Kade Engine Installation", value="https://github.com/KadeDev/Kade-Engine/releases/tag/1.8", inline=False) embedVar.add_field(name="Kade Engine Source (Recommended for Mod Makers)", value="https://github.com/KadeDev/Kade-Engine", inline=False) await ctx.send(embed=embedVar) @client.command(name='week') async def make_week_guide(ctx): embedVar = discord.Embed(title="How to make a week", description="How to make a week", color=0x2C93FA) embedVar.add_field(name="test", value="test", inline=False) embedVar.add_field(name="test", value="test", inline=False) await ctx.send(embed=embedVar) client.run('WONT SHOW BOT TOKEN')
内容的提问来源于stack exchange,提问作者Dr Uber
相关产品推荐
相关产品推荐

