如何获取Discord用户输入并匹配症状列表实现诊断机器人?
解决方案:实现Discord机器人的症状收集与匹配功能
你的核心问题是没有正确获取用户的输入消息,以及错误地将上下文对象ctx和症状列表做比对。下面分两种方案给出修复后的代码,你可以根据需求选择:
方案一:保留按钮功能的完整实现
这个方案会在用户点击"Yes"按钮后,引导用户输入症状并完成匹配:
import discord from discord.ext import commands allergy_symptoms = ["sneezing", "runny nose", "congestion", "cough", "wheezing", "shortness of breath", "itchy eyes", "hives", "rash", "itchiness"] bot = commands.Bot(command_prefix="!", intents=discord.Intents.all()) class MyView(discord.ui.View): def __init__(self, ctx): super().__init__(timeout=60) # 设置按钮超时时间(60秒) self.ctx = ctx @discord.ui.button(label="Yes", style=discord.ButtonStyle.green) async def button_callback(self, interaction: discord.Interaction, button: discord.ui.Button): await interaction.response.send_message("Excellent. To start, please list any symptoms you may be experiencing in a single message. For example: I have been experiencing congestion, sneezing, and a runny nose.") # 等待用户发送症状消息(只接收命令发起者的消息) def check(m): return m.author == self.ctx.author and m.channel == self.ctx.channel try: symptom_msg = await bot.wait_for("message", check=check, timeout=60) except TimeoutError: await self.ctx.send("你超时未输入,诊断流程已终止。") return # 处理用户输入:拆分症状并转为小写,去除无关字符 user_symptoms = [s.strip().lower() for s in symptom_msg.content.split(",")] # 匹配过敏症状 matched_symptoms = [symptom for symptom in user_symptoms if symptom in allergy_symptoms] if matched_symptoms: await self.ctx.send(f"你可能患有过敏症!匹配到的症状:{', '.join(matched_symptoms)}") else: await self.ctx.send("暂时未匹配到过敏症的典型症状。") @bot.command() async def diagnose(ctx): await ctx.send(f'Hello, I am the R.E.M.E.D.Y. Bot. I can assist you with a diagnosis and recommend a remedy for it. Is this something you are looking for?', view=MyView(ctx)) # 替换为你的机器人token bot.run("YOUR_BOT_TOKEN")
关键修复点:
- 给
MyView传入ctx,确保能识别命令发起者 - 使用
bot.wait_for("message")获取用户输入,通过check函数过滤非目标用户的消息 - 对用户输入做清洗(转小写、去空格),再和过敏列表比对
- 添加超时处理,避免机器人一直等待
方案二:简化版本(移除按钮,直接引导)
如果不需要按钮功能,代码可以更简洁:
import discord from discord.ext import commands allergy_symptoms = ["sneezing", "runny nose", "congestion", "cough", "wheezing", "shortness of breath", "itchy eyes", "hives", "rash", "itchiness"] bot = commands.Bot(command_prefix="!", intents=discord.Intents.all()) @bot.command() async def diagnose(ctx): await ctx.send(f'Hello, I am the R.E.M.E.D.Y. Bot. I can assist you with a diagnosis and recommend a remedy for it.') await ctx.send("To start, please list any symptoms you may be experiencing in a single message. For example: I have been experiencing congestion, sneezing, and a runny nose.") # 等待用户输入 def check(m): return m.author == ctx.author and m.channel == ctx.channel try: symptom_msg = await bot.wait_for("message", check=check, timeout=60) except TimeoutError: await ctx.send("你超时未输入,诊断流程已终止。") return # 处理输入并匹配 user_symptoms = [s.strip().lower() for s in symptom_msg.content.split(",")] matched_symptoms = [symptom for symptom in user_symptoms if symptom in allergy_symptoms] if matched_symptoms: await ctx.send(f"你可能患有过敏症!匹配到的症状:{', '.join(matched_symptoms)}") else: await ctx.send("暂时未匹配到过敏症的典型症状。") bot.run("YOUR_BOT_TOKEN")
注意事项:
- 必须开启机器人的
Message Content Intent,否则无法读取用户发送的消息内容 - 替换代码中的
YOUR_BOT_TOKEN为你自己的机器人令牌 - 可以根据需求调整超时时间(当前为60秒)
内容的提问来源于stack exchange,提问作者senorbryan
相关产品推荐
相关产品推荐

