Discord.py工单机器人按钮点击报错,请求修复指导
Discord.py工单机器人按钮报错修复方案
错误原因
报错TypeError: ticket_embed_.Ticket_Embed_button() missing 1 required positional argument: 'button'是因为按钮回调函数的参数定义不符合Discord.py规范:
- 多余了
ctx参数,按钮回调的触发逻辑里不会传入ctx对象 - 参数顺序错误,正确的按钮回调参数顺序应为
self, interaction, button
修复步骤及修正后代码
关键修改点:
- 移除
Ticket_Embed_button函数中的ctx参数,调整为正确的参数顺序 - 将函数内所有
ctx.guild替换为interaction.guild,删除重复的消息发送代码(原代码中重复发送了两次工单创建通知) - 增加分类不存在的判断,避免机器人崩溃
修正后的完整代码:
class ticket_info(discord.ui.View): def __init__(self): super().__init__(timeout=None) @discord.ui.button(label="Close",style=discord.ButtonStyle.blurple, emoji="<:locked:1158126357570650224>", custom_id="ticket_close") async def Verifybutton(self,interaction:discord.Interaction,button:discord.ui.Button): await interaction.response.send_message("Closing Ticket", ephemeral=True) await interaction.channel.delete() class ticket_embed_(discord.ui.View): def __init__(self): super().__init__(timeout=None) @discord.ui.button(label="Open Ticket",style=discord.ButtonStyle.blurple, emoji="<:ticket_:1158126385299210351>", custom_id="ticket_open") async def Ticket_Embed_button(self, interaction:discord.Interaction, button:discord.ui.Button): # 获取Support分类,增加判断避免找不到时报错 category = discord.utils.get(interaction.guild.categories, name='Support') if not category: await interaction.response.send_message("未找到名为'Support'的分类,请先创建该分类!", ephemeral=True) return ticket_channel = await category.create_text_channel(f'ticket-{interaction.user.name}') await ticket_channel.set_permissions(interaction.user, read_messages=True, send_messages=True) await ticket_channel.set_permissions(interaction.guild.default_role, read_messages=False) # 发送一次通知即可,删除重复的ctx.send await interaction.response.send_message(f"{interaction.user.mention}, 你的工单已创建在 {ticket_channel.mention}", ephemeral=True) view= ticket_info() info = discord.Embed(title=f"欢迎", description="请按照下方格式说明你的问题:", color=0x476EAA) info.add_field(name=" ", value="你的用户名:", inline=False) info.add_field(name=" ", value="问题描述:", inline=False) info.set_footer(text="Seshi - Enhance Your Sessions", icon_url="https://cdn.discordapp.com/attachments/1154789399217852487/1158113442515914833/Seshi-S-wbackground.png?ex=651b10b1&is=6519bf31&hm=c85012ff3b35024f6e44bff045dcd7cbe3f51ca7385b99cecc2459bd64973027&") await ticket_channel.send(embed=info, view=view) @client.hybrid_command() async def create_ticket_embed(ctx): view= ticket_embed_() channel = client.get_channel(1155249308430504037) ticket_embed = discord.Embed(title="Seshi Support", description="在Seshi,我们互相尊重、互相帮助,你可以随时通过下方按钮创建工单获取支持!") ticket_embed.set_image(url="https://cdn.discordapp.com/attachments/1154789399217852487/1158172703765827764/Seshi-Enhance-banner.png?ex=651b47e2&is=6519f662&hm=15a183764cba338a4f47be40737599b77d4829c4c6349281e25e162936b72592&") ticket_embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/1154789399217852487/1158133155824414760/Seshi-S.png?ex=651b230d&is=6519d18d&hm=a17c1f3cdef1e91d1308e00d7785c39556a45457cbd0d28fed7925ea4f1f2331&") ticket_embed.add_field(name='点击 "<:ticket_:1158126385299210351> Open Ticket" 按钮创建工单!', value=" ") await channel.send(embed=ticket_embed, view=view) await ctx.send("嵌入消息发送成功!")
额外说明
- 确保服务器中已创建名为
Support的分类,否则机器人会提示找不到分类 - 原代码中重复发送了两次工单创建通知,已删除重复部分,避免用户收到两条相同消息
内容的提问来源于stack exchange,提问作者Patryk Patrzk
相关产品推荐
相关产品推荐

