discord.py机器人nuke命令用户确认逻辑失效 所有回复均触发else分支
核心问题定位
所有分支判断失效的根本原因是字符串方法调用遗漏了括号:
代码中写的msg.content.lower没有加执行括号,此时拿到的是lower方法本身的对象引用,不是经过小写转换后的消息内容字符串。把方法对象和("y", "yes")这类字符串元组做成员匹配时,永远不可能匹配成功,因此所有if、elif分支都会被跳过,直接进入else逻辑。
修正方法
把所有分支判断语句里的msg.content.lower补上方法调用的括号,替换为msg.content.lower()即可:
- 原错误写法:
if msg.content.lower in ("y", "yes"): - 修正后写法:
if msg.content.lower() in ("y", "yes"):
修正后的核心判断代码段
msg = await self.client.wait_for('message', check=lambda message:message.author == ctx.author and message.channel.id == ctx.channel.id) if msg.content.lower() in ("y", "yes"): embedis = discord.Embed(color=0xa3a3ff, title=f"Channel ({ctx.channel.name}) has been nuked :boom:", description=f"Nuked by: {ctx.author.name}#{ctx.author.discriminator} \n **Reason:** {reason}") embedis.set_image(url = "https://66.media.tumblr.com/23dad7011515a9c21647fefb07e1c0e0/dd0cbd9bb94e2a45-1e/s640x960/a02fc34abff6953c59adafc190de6e5276969175.gif") embedis.set_footer(text=f"Rylie - Thanks for using this bot! - {p} - {self.client.version}", icon_url = str(self.client.user.avatar_url)) await ctx.channel.delete(reason=reason) channel = await ctx.channel.clone() await channel.send(embed=embedis) elif msg.content.lower() in ("n", "no"): embegs = discord.Embed(color=0xa3a3ff, title = ":red_circle: NOTICE :red_circle:", description = f"{ctx.channel.mention} was not nuked!") embegs.set_footer(text=f"Rylie - Thanks for using this bot! - {p} - {self.client.version}", icon_url = str(self.client.user.avatar_url)) await ctx.send(embed=embegs) else: embers = discord.Embed(color=0xa3a3ff, title = ":red_circle: NOTICE :red_circle:", description = "No proper response was given, action was terminated") embers.set_footer(text=f"Rylie - Thanks for using this bot! - {p} - {self.client.version}", icon_url = str(self.client.user.avatar_url)) await ctx.send(embed=embers)
额外的已知逻辑缺陷提示
- 当前
wait_for没有设置超时时间,如果用户长时间不返回确认消息,命令会一直阻塞占用资源,建议加上timeout=30参数并捕获asyncio.TimeoutError做超时提示 - 代码里虽然定义了
channel可选参数,但后续删除、克隆频道的操作全写死用了ctx.channel,完全没有使用传入的目标channel参数,属于原有逻辑的遗漏 - 确认提示里固定写死了“删除当前频道”的文案,和实际要操作的目标channel不匹配,也属于配套遗漏
内容的提问来源于stack exchange,提问作者Harukomaze
相关产品推荐
相关产品推荐

