Discord机器人报错RuntimeWarning:未等待Messageable.send协程求解
问题排查与解决
核心错误修复
报错的直接原因是异步方法未被等待:channel.send()是discord.py中的异步协程函数,必须使用await关键字调用,否则Python会抛出协程从未执行的警告。
将代码中这一行:
channel.send(embed=embed)
修改为:
await channel.send(embed=embed)
其他潜在问题修正
除核心错误外,代码还有几个需要优化的点:
- 频道ID必须是整数类型:
interaction.guild.get_channel()的参数不能是字符串,要传入数字形式的频道ID,比如channel = interaction.guild.get_channel(123456789012345678)。 - 避免昵称为空的情况:
self.user.nick在用户未设置服务器昵称时会返回None,建议改用self.user.display_name,它会优先显示昵称,无昵称时显示用户名。 - 回复文本修正:把
Thx for your!改为更通顺的Thanks for your application!。
完整修正后的代码
class MyModal2(ui.Modal, title='Application for membership'): new_client = ui.TextInput(label='Your first name last name', placeholder='example: Zenicu Lena', style=discord.TextStyle.short) new_client_id = ui.TextInput(label='Your staticID', placeholder='example:#45970', style=discord.TextStyle.short) buissnes = ui.TextInput(label='Where you work', placeholder='example: Government ', style=discord.TextStyle.short) about = ui.TextInput(label='Date/Time', placeholder='example: 23.12 / 23:15 ', style=discord.TextStyle.short) async def on_submit(self, interaction: discord.Interaction): # 替换为实际的整数频道ID channel = interaction.guild.get_channel(123456789012345678) embed = discord.Embed( title="New application for membership", description=f'1. {self.new_client.value},\n2. {self.new_client_id.value},\n3. {self.buissnes.value},\n4. {self.about.value}', color=discord.Color.purple() ) # 使用display_name避免昵称为空 embed.set_author(name=self.user.display_name) # 加上await等待异步方法执行 await channel.send(embed=embed) await interaction.response.send_message(f'{interaction.user.mention} - Thanks for your application!', ephemeral=True) @bot.tree.command(name='invite', description='Application for membership') async def modal(interaction: discord.Interaction): polit_modal = MyModal2() polit_modal.user = interaction.user await interaction.response.send_modal(polit_modal)
内容的提问来源于stack exchange,提问作者zenicu lena
相关产品推荐
相关产品推荐

