discord.py 实现斜线命令发送预设投票的问题排查与解决
discord.py 实现斜线命令发送预设投票的问题排查与解决
刚接触discord.py的话踩这种小坑太正常啦!我来帮你理清楚你遇到的问题和解决办法:
核心错误原因:交互响应方法用错了
你报错里的AttributeError: 'InteractionResponse' object has no attribute 'send'是因为**interaction.response对象根本没有send()这个方法**。在discord.py的交互命令(斜线命令)里,要发送响应内容,得用send_message()方法才行。
第二个隐藏问题:投票时长参数类型不匹配
你原来写的duration=4也会有问题——discord的Poll类要求duration参数必须是datetime.timedelta类型,而不是单纯的数字。直接写数字会导致后续运行报错,所以得用时间差对象来指定时长。
修正后的完整代码片段
你已经自己调整出了正确的代码,我再贴出来方便大家参考:
import datetime @client.tree.command(name="repo", description="Create a poll to see who's free for REPO", guild=GUILD_ID) async def repo(interaction: discord.Interaction): p = discord.Poll(question="Are you free for REPO?", duration=datetime.timedelta(hours=4)) p.add_answer(text="Yes") p.add_answer(text="No") await interaction.response.send_message(poll=p)
总结两个关键修正点
- 把错误的
await interaction.response.send(poll=p)替换成await interaction.response.send_message(poll=p) - 导入
datetime模块,用datetime.timedelta(xxx)来设置投票时长(比如hours=4代表4小时,也可以用minutes、days等单位)
备注:内容来源于stack exchange,提问作者Gankick
相关产品推荐
相关产品推荐

