Discord机器人模态框提交后编辑用户角色及昵称报错求助
解决Discord.py验证码模态框的角色与用户操作错误
我尝试了多种方法解决该问题,但均未奏效。(抱歉我的英语不好,我是法国人,已经尽力了^^)
完整代码
#captchamodal class Captcha(discord.ui.Modal, title="Formulaire de Verification"): captresult = discord.ui.TextInput(label="code du Captcha", placeholder=f"code = {captrand}", max_length=6, min_length=6) async def on_submit(self, interaction: discord.Interaction): if captrand == self.captresult: await interaction.response.send_message(f"le Captcha a bien été validé {interaction.user.display_name} !", ephemeral=True) role = discord.utils.get(interaction.guild.role, id=1115589231474122783) #the errors come from this line, and the 2 following await interaction.user.edit(nick=f"🥖{interaction.user.display_name}") await interaction.user.add_roles(role) class CaptchaButton(discord.ui.View): def __init__(self): super().__init__() @discord.ui.button(label="Captcha", style=discord.ButtonStyle.blurple, emoji="<Roger:1117098522936881162>") async def on_click(self, interaction: discord.Interaction, button: discord.ui.Button): await interaction.response.send_modal(Captcha()) @tree.command(name = "captcha", description = "[MODERATION] génère un captcha.", guild=discord.Object(id=guild_id)) async def CaptchaEmbed(interaction: discord.Interaction): emb=discord.Embed(title="Code à usage unique", description=f"{interaction.user.display_name}, intéragis avec le bouton ci-dessous pour remplir le captcha avec un code :\r\n", color = discord_blue, timestamp=datetime.datetime.now()) emb.set_author(name="BreadBot", url="https://discordapp.com/users/793183664858071040", icon_url="https://cdn.discordapp.com/avatars/1102573935658283038/f769161ee8e6a63563b1166ab7c016b5.webp?size=128") emb.set_thumbnail(url=f"{interaction.user.avatar}") # type: ignore emb.set_footer(text=f"{interaction.guild.name}", icon_url=interaction.guild.icon) # type: ignore await interaction.response.send_message(embed=emb, view=CaptchaButton(), ephemeral=True)
遇到的错误
Cannot access member "role" for type "Guild" Member "role" is unknown "role" is not a known member of "None"
Cannot access member "edit" for type "User" Member "edit" is unknown
Cannot access member "add_roles" for type "User" Member "add_roles" is unknown
我尝试将代码中的user替换为member,但完全没有效果。
错误原因与修正方案
1. Guild角色获取错误
interaction.guild.role不存在,正确属性是复数形式的roles,表示服务器的角色列表。- 需先判断
interaction.guild是否不为None(避免DM场景报错),再获取角色。
2. User对象无服务器操作权限
interaction.user是全局User对象,没有edit(修改昵称)和add_roles(添加角色)方法,这些操作需要使用服务器内的Member对象即interaction.member。- 同样要判断
interaction.member是否存在,确保操作有效。
修正后的完整代码
#captchamodal class Captcha(discord.ui.Modal, title="Formulaire de Verification"): captresult = discord.ui.TextInput(label="code du Captcha", placeholder=f"code = {captrand}", max_length=6, min_length=6) async def on_submit(self, interaction: discord.Interaction): # 先验证验证码是否正确(需取TextInput的value属性) if captrand == self.captresult.value: await interaction.response.send_message(f"le Captcha a bien été validé {interaction.user.display_name} !", ephemeral=True) # 检查是否在服务器内 if not interaction.guild or not interaction.member: await interaction.followup.send("无法在当前环境执行此操作!", ephemeral=True) return # 获取目标角色(使用roles复数) role = discord.utils.get(interaction.guild.roles, id=1115589231474122783) if not role: await interaction.followup.send("目标角色不存在!", ephemeral=True) return # 使用member对象执行服务器内操作 await interaction.member.edit(nick=f"🥖{interaction.member.display_name}") await interaction.member.add_roles(role) else: await interaction.response.send_message("验证码错误!", ephemeral=True) class CaptchaButton(discord.ui.View): def __init__(self): super().__init__() @discord.ui.button(label="Captcha", style=discord.ButtonStyle.blurple, emoji="<Roger:1117098522936881162>") async def on_click(self, interaction: discord.Interaction, button: discord.ui.Button): await interaction.response.send_modal(Captcha()) @tree.command(name = "captcha", description = "[MODERATION] génère un captcha.", guild=discord.Object(id=guild_id)) async def CaptchaEmbed(interaction: discord.Interaction): emb=discord.Embed(title="Code à usage unique", description=f"{interaction.user.display_name}, intéragis avec le bouton ci-dessous pour remplir le captcha avec un code :\r\n", color = discord_blue, timestamp=datetime.datetime.now()) emb.set_author(name="BreadBot", url="https://discordapp.com/users/793183664858071040", icon_url="https://cdn.discordapp.com/avatars/1102573935658283038/f769161ee8e6a63563b1166ab7c016b5.webp?size=128") emb.set_thumbnail(url=f"{interaction.user.avatar}") # type: ignore emb.set_footer(text=f"{interaction.guild.name}", icon_url=interaction.guild.icon) # type: ignore await interaction.response.send_message(embed=emb, view=CaptchaButton(), ephemeral=True)
额外注意点
- 原代码中直接用
captrand == self.captresult比较验证码是错误的,TextInput组件的值需要通过.value属性获取。 - 添加了角色不存在的判断,避免后续操作报错。
内容的提问来源于stack exchange,提问作者Wishrito
相关产品推荐
相关产品推荐

