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

Discord.py机器人on_message重复回复问题求助

解决Discord机器人重复回复问题

问题核心是on_message事件会每次收到消息就触发一次,你的代码现在不管用户发什么,只要不是机器人消息,都会先发送初始提示,再判断内容,导致重复回复。要实现分步独立的对话流程,需要跟踪用户的当前对话状态。

解决方案要点:

  • 用全局字典存储用户的对话阶段,区分不同用户的对话进度
  • 根据用户当前所处的状态,执行对应逻辑,而非每次都触发初始提示

修改后的示例代码:

# 全局变量存储用户对话状态,键为用户ID,值为当前对话阶段
user_dialog_states = {}

async def on_message(message):
    if message.author.bot:
        return
    user_id = message.author.id

    # 判断用户是否处于初始状态
    if user_id not in user_dialog_states:
        # 首次触发,发送选择提示并切换状态
        await message.reply("Are you reporting a bug or exploit? State 'bug' or 'exploit'")
        user_dialog_states[user_id] = "waiting_for_choice"
    else:
        current_state = user_dialog_states[user_id]
        # 处理等待选择的状态
        if current_state == "waiting_for_choice":
            if message.content.lower() == 'bug':
                await message.reply("Use the following format:\nUsername:\nBug Report Explanation:\nProof:\nSend streamable files (mp4, Gyazo)")
                # 完成当前步骤,重置用户状态
                del user_dialog_states[user_id]
            elif message.content.lower() == 'exploit':
                # 可在此添加漏洞报告对应的提示逻辑
                await message.reply("Please provide details about the exploit...")
                del user_dialog_states[user_id]
            else:
                await message.reply("Invalid input. Please enter 'bug' or 'exploit'.")

关键说明:

  • user_dialog_states确保每个用户的对话进度独立,不会互相干扰
  • 只有当用户处于初始状态时,才会发送首次提示;后续回复会根据当前状态执行对应逻辑
  • 完成对话步骤后删除用户状态,让对话回到初始状态,方便下次触发

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 04:30:58