如何为Python开发的Discord机器人添加Bug上报交互命令?
实现Discord机器人Bug上报多轮交互的方案
针对你的需求,下面提供两种主流实现方式,适配不同的机器人开发模式:
一、基于前缀命令的多轮交互(适合传统消息命令)
这种方式利用discord.py的wait_for方法,直接在命令流程中等待用户的后续输入,逻辑清晰且自带超时处理:
import discord from discord.ext import commands import asyncio intents = discord.Intents.default() intents.message_content = True # 必须开启消息内容意图 bot = commands.Bot(command_prefix='!', intents=intents) @bot.command(name='report_bug') async def report_bug(ctx): # 发送提示消息 await ctx.send("Describe the bug") # 定义消息校验规则:仅接收发起命令的用户在同一频道的消息 def check(message): return message.author == ctx.author and message.channel == ctx.channel try: # 等待用户输入,超时时间设为60秒 bug_msg = await bot.wait_for('message', check=check, timeout=60) except asyncio.TimeoutError: await ctx.send("Bug上报超时,请重新发起命令。") return # 处理Bug详情:这里可以替换为保存到文件、发送给管理员频道等逻辑 admin_channel = bot.get_channel(1234567890) # 替换为实际管理员频道ID if admin_channel: await admin_channel.send(f"新Bug上报来自{ctx.author.mention}:\n{bug_msg.content}") # 回复用户确认 await ctx.send(f"Bug已成功上报:\n{bug_msg.content}\n感谢你的反馈!") bot.run('你的机器人Token')
二、基于状态跟踪的消息监听模式(适配你的原有代码结构)
如果想保留handle_response的基础逻辑,可以通过维护用户状态来实现多轮交互:
import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='!', intents=intents) # 用集合跟踪正在上报Bug的用户ID user_in_report = set() @bot.event async def on_message(message): if message.author.bot: return # 忽略机器人自身消息 p_message = message.content.lower() if p_message == 'report_bug': user_in_report.add(message.author.id) await message.channel.send("Describe the bug") elif message.author.id in user_in_report: # 处理用户提交的Bug详情 bug_details = message.content # 这里添加自定义处理逻辑(如通知管理员) await message.channel.send(f"Bug已上报:{bug_details}\n感谢反馈!") user_in_report.remove(message.author.id) else: # 调用原有响应逻辑处理其他命令 response = handle_response(p_message) if response: await message.channel.send(response) await bot.process_commands(message) # 必须调用,否则命令框架失效 def handle_response(message) -> str: if message == 'hello': return 'Hi there!' # 其他命令逻辑可在此扩展 return None bot.run('你的机器人Token')
三、推荐:基于斜杠命令+模态框的现代交互方式
Discord目前主推斜杠命令,结合模态框可以让用户在一个交互中完成Bug上报,体验更流畅:
import discord from discord import app_commands from discord.ui import Modal, TextInput import asyncio intents = discord.Intents.default() bot = discord.Client(intents=intents) tree = app_commands.CommandTree(bot) @tree.command(name='report_bug', description='提交Bug反馈') async def report_bug(interaction: discord.Interaction): # 创建模态框表单 modal = Modal(title="Bug上报表单") # 添加多行文本输入框用于填写Bug详情 bug_desc_input = TextInput( label="Bug详细描述", style=discord.TextStyle.long, placeholder="请描述Bug发生的场景、操作步骤和异常表现...", required=True, max_length=1000 ) modal.add_item(bug_desc_input) # 发送模态框给用户 await interaction.response.send_modal(modal) # 等待用户提交表单 try: submitted = await bot.wait_for( 'modal_submit', check=lambda i: i.user == interaction.user and i.custom_id == modal.custom_id, timeout=300 ) except asyncio.TimeoutError: return # 处理提交的Bug信息 bug_details = submitted.fields[0].value admin_channel = bot.get_channel(1234567890) # 替换为实际管理员频道ID if admin_channel: await admin_channel.send(f"新Bug上报来自{submitted.user.mention}:\n{bug_details}") # 回复用户(仅用户可见) await submitted.response.send_message( f"Bug已成功提交:\n{bug_details}\n感谢你的反馈!", ephemeral=True ) @bot.event async def on_ready(): await tree.sync() # 同步斜杠命令到Discord服务器 print(f"机器人已登录:{bot.user}") bot.run('你的机器人Token')
注意事项
- 必须在Discord开发者后台开启
message_content意图,否则机器人无法读取用户消息内容。 - 机器人Token需妥善保管,建议通过环境变量注入,不要硬编码在代码中。
- 处理Bug详情时,可以额外记录用户ID、服务器ID、上报时间等信息,便于后续排查。
内容的提问来源于stack exchange,提问作者Myyraman
相关产品推荐
相关产品推荐

