在 discord.py 中创建新的赠品互动时出现“未知互动”错误。

huangapple go评论90阅读模式
英文:

Getting 'Unknown Interaction' error when creating new giveaway inteaction in discord.py

问题

这是我的交互功能来创建Discord赠品机器人的代码段,使用discord.py:

  1. @app_commands.command(name="startgive", description="starts a giveaway")
  2. @app_commands.describe(
  3. duration="giveaway的持续时间",
  4. winners="获胜者数量",
  5. prize="正在赠送的奖品",
  6. )
  7. async def startgive(self, interaction: discord.Interaction, duration: str, winners: int, prize: str):
  8. if interaction.user.guild_permissions.administrator:
  9. try:
  10. if (winners <= 0):
  11. await interaction.response.send_message("输入非负数的获胜者数量!", ephemeral=True)
  12. else:
  13. created_at = datetime.now()
  14. duration = parser.parse(duration)
  15. duration = duration + round(created_at.timestamp())
  16. embed = None
  17. try:
  18. last_id = self.giveaways.pop()['id']
  19. self.id = int(last_id) + 1
  20. except:
  21. self.id = 1
  22. btn = GiveawayButton(self, self.id)
  23. giveaway = await self.create_giveaway({
  24. 'guild_id': interaction.guild.id,
  25. 'prize': prize,
  26. 'entries': None,
  27. 'winners': None,
  28. 'possible_winners': winners,
  29. 'duration': str(datetime.fromtimestamp(duration)),
  30. 'created_at': str(created_at),
  31. 'ended': False,
  32. })
  33. giveaway['created_at'] = datetime.fromtimestamp(duration)
  34. giveaway['interaction'] = interaction
  35. giveaway['entries'] = []
  36. giveaway['winners'] = []
  37. giveaway['btn'] = btn
  38. embed = await self.create_embed(giveaway)
  39. giveaway['embed'] = embed
  40. await interaction.response.send_message(embed=embed, view=btn)
  41. self.task_launcher(giveaway, 1)
  42. except Exception as e:
  43. print(e)
  44. await interaction.response.send_message("无效的时间格式!(1分钟/1m)", ephemeral=True)
  45. else:
  46. await interaction.response.send_message("您没有权限使用此功能。", ephemeral=True)

self.create_giveaway 发送异步请求到我的仪表盘API,它创建了赠品本身,但没有在服务器上响应Discord消息。这是 create_giveaway 函数和错误:

  1. async def create_giveaway(self, data):
  2. endpoint = self.url + "/create-giveaway/"
  3. async with aiohttp.ClientSession() as session:
  4. async with session.post(endpoint, json=data) as r:
  5. if r.status == 200:
  6. await self.get_giveaways()
  7. return await r.json()

在 discord.py 中创建新的赠品互动时出现“未知互动”错误。

在 discord.py 中创建新的赠品互动时出现“未知互动”错误。

我尝试创建 self.bot.loop.create_task(self.create_giveaway(data)),但这感觉不对,而且我将不得不发送另一个API请求来获取赠品列表。

英文:

I am trying to create discord giveaway bot with discord.py. This is my interaction function for creating the giveaway:

  1. @app_commands.command(name=&quot;startgive&quot;, description=&quot;starts a giveaway&quot;)
  2. @app_commands.describe(
  3. duration=&quot;duration of the giveaway&quot;,
  4. winners=&quot;number of winners&quot;,
  5. prize=&quot;the prize being given away&quot;,
  6. )
  7. async def startgive(self, interaction: discord.Interaction, duration: str, winners: int, prize: str):
  8. if interaction.user.guild_permissions.administrator:
  9. try:
  10. if (winners &lt;= 0):
  11. await interaction.response.send_message(&quot;Type non-negative number of winners!&quot;, ephemeral=True)
  12. else:
  13. created_at = datetime.now()
  14. duration = parser.parse(duration)
  15. duration = duration + round(created_at.timestamp())
  16. embed = None
  17. try:
  18. last_id = self.giveaways.pop()[&#39;id&#39;]
  19. self.id = int(last_id) + 1
  20. except:
  21. self.id = 1
  22. btn = GiveawayButton(self, self.id)
  23. giveaway = await self.create_giveaway({
  24. &#39;guild_id&#39;: interaction.guild.id,
  25. &#39;prize&#39;: prize,
  26. &#39;entries&#39;: None,
  27. &#39;winners&#39;: None,
  28. &#39;possible_winners&#39;: winners,
  29. &#39;duration&#39;: str(datetime.fromtimestamp(duration)),
  30. &#39;created_at&#39;: str(created_at),
  31. &#39;ended&#39;: False,
  32. })
  33. giveaway[&#39;created_at&#39;] = datetime.fromtimestamp(duration)
  34. giveaway[&#39;interaction&#39;] = interaction
  35. giveaway[&#39;entries&#39;] = []
  36. giveaway[&#39;winners&#39;] = []
  37. giveaway[&#39;btn&#39;] = btn
  38. embed = await self.create_embed(giveaway)
  39. giveaway[&#39;embed&#39;] = embed
  40. await interaction.response.send_message(embed=embed, view=btn)
  41. self.task_launcher(giveaway, 1)
  42. except Exception as e:
  43. print(e)
  44. await interaction.response.send_message(&quot;Invalid time format! (1 minutes/1m)&quot;, ephemeral=True)
  45. else:
  46. await interaction.response.send_message(&quot;You don&#39;t have permissions to use this.&quot;, ephemeral=True)

self.create_giveaway sends async request to my dashboard api, it creates the giveaway itself, but doesn't respond with the discord message on the server. Here is the 'create_giveaway' function and the error:

  1. async def create_giveaway(self, data):
  2. endpoint = self.url + &quot;/create-giveaway/&quot;
  3. async with aiohttp.ClientSession() as session:
  4. async with session.post(endpoint, json=data) as r:
  5. if r.status == 200:
  6. await self.get_giveaways()
  7. return await r.json()

在 discord.py 中创建新的赠品互动时出现“未知互动”错误。

在 discord.py 中创建新的赠品互动时出现“未知互动”错误。

I've tried to create self.bot.loop.create_task(self.create_giveaway(data)) instead, but this doesn't feel right and I would have to send another API request to get list of giveaways.

答案1

得分: 0

你需要在3秒内响应互动,否则将被视为无效

如果你的API响应时间超过3秒,将会收到404错误。我建议你延迟响应,然后在你的API返回后再进行回应。

英文:

You have 3 seconds to respond to an interaction before it is invalidated.

If your API takes longer than that to respond you will get the 404 error, I suggest deferring your reply, then responding once your API has returned.

huangapple
  • 本文由 发表于 2023年7月11日 00:25:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定