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

discord.py工单系统仅命令执行者可开单问题修复求助

问题根因
  • 反应校验逻辑限制仅发起!setuppartnership命令的用户可触发反应事件,其他用户的点击会被直接过滤
  • 工单频道创建逻辑硬编码使用命令发起者作为权限授予对象,即使用其他用户触发反应,也只会为命令发起者创建工单
  • 未校验触发反应的消息是否为工单卡片消息,可能被同频道其他相同表情的反应误触发,同时命令内的无限循环如果被多次调用会产生冗余监听线程
修复后代码
# Ticket
@client.command()
async def setuppartnership(ctx):
    guild = ctx.guild
    embed = discord.Embed(
        title = 'Richiesta Partnership',
        description = 'Vuoi fare partnership? Usa la reazione qui sotto per creare un nuovo ticket.',
        color=0x53eeff
    )
 
    msg = await ctx.send(embed=embed)
    await msg.add_reaction("📩")
    
    while True:
        def check(reaction, user):
            # 校验:表情正确、是当前工单卡片的反应、触发者不是机器人
            return str(reaction.emoji) == '📩' and reaction.message.id == msg.id and user != client.user
        # 接收返回的触发事件的用户对象
        reaction, user = await client.wait_for("reaction_add", check=check)
        # 使用触发反应的用户创建工单、分配权限
        ticket_channel = await guild.create_text_channel(name=f'partnership・{user.name}')
        await ticket_channel.set_permissions(user, send_messages=True, read_messages=True, add_reactions=True, embed_links=True, attach_files=True, read_message_history=True, external_emojis=True)
        await ticket_channel.set_permissions(ctx.guild.default_role, send_messages=False, read_messages=False)
关键改动说明
  • 移除了校验逻辑中对ctx.author的限制,新增消息ID校验确保只有工单卡片的反应能触发,同时排除机器人自身的反应避免误触发
  • 接收wait_for返回的触发事件的用户对象,使用该用户进行频道命名和权限分配
  • 用官方内置的ctx.guild.default_role替代手动取ID的全员角色,兼容性更好
  • 移除了v1.x版本后废弃的pass_context=True参数,如果你使用的是0.x旧版本可以加回来

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 11:06:05