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

Discord机器人对表情反应无响应 无法提交数据到Google Sheets

问题原因及修复方案

你的代码核心问题是缩进逻辑错误,导致监听表情反应的代码块完全不会执行,具体问题和修复方案如下:

1. 核心错误:缩进位置错误

你目前把while True循环和所有监听反应的逻辑,都写在了check校验函数的return语句之后,这部分属于永远不会触发的死代码:

  • check函数执行到return就直接返回校验结果,后续代码完全不会运行
  • 监听反应的代码应该放在check函数外部,和添加表情的代码同层级

2. 次要错误:参数名错误

bot.wait_for的超时控制参数是timeout,不是你写的wait_for,参数错误会导致超时逻辑异常。

3. 冗余逻辑移除

你只需要监听一次用户的反应即可,不需要加while True循环,避免逻辑冗余。

修复后的完整代码

@slash.slash(
    name="테스트쓰기",
    description="샘플 데이터를 구글 시트에 작성합니다.",
    guild_ids=[865433__________],
    options=[
        create_option(
            name="이름",
            description="스프레드시트에 표시될 이름",
            option_type=3,
            required=True
        ),
        create_option(
            name="생일",
            description="MM-DD 형식으로 입력",
            option_type=3,
            required=True
        )
    ],
    connector={
    '이름': 'uname',
    '생일': 'btday'
    }
)
async def ap_sheet(ctx:SlashContext, uname:str, btday:str):
    checker = await ctx.send(embed=check_emb(ctx, uname=uname, btday=btday))
    
    await checker.add_reaction("⭕")
    await checker.add_reaction("❌")

    def check(reaction, user):
        return user == ctx.user and reaction.message.id == checker.id and str(reaction.emoji) in ['⭕','❌']
        
    try:
        reaction, user = await bot.wait_for("reaction_add", check=check, timeout=20.0)
        if str(reaction.emoji) == '⭕': # 用户确认,写入谷歌表格
            await checker.delete()
            await ctx.send(discord.Embed(title="완료", description="입력한 정보를 내보냈습니다.", color=discord.Color.blue()), delete_after=5)
            # 注意gspread是同步库,直接调用会阻塞机器人运行,建议用await asyncio.to_thread(gc1.append_row, [uname, ctx.author.id, btday])写法优化
            gc1.append_row([uname, ctx.author.id, btday])
            
        elif str(reaction.emoji) == '❌': # 用户取消操作
            await checker.delete()
            await ctx.send(discord.Embed(title="취소됨!", description="사용자의 조작에 의해 취소되었어요.", color=discord.Color.red()), delete_after=5)
        
    except asyncio.TimeoutError: # 超时未操作处理
        await checker.delete()
        await ctx.send("시간 초과되었습니다.", delete_after=5)

额外检查项

如果修复缩进后仍无响应,请确认以下配置:

  • Discord开发者后台已经给机器人开启了Server Members Intent和Message Content Intent权限
  • 机器人在当前服务器拥有添加反应、读取消息历史的权限
  • 你使用的Discord框架(discord.py/nextcord/pycord等)的斜杠命令、反应监听API和你写的代码逻辑匹配

内容的提问来源于stack exchange,提问作者BlacK.CHi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 21:54:03