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

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

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

问题

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

@app_commands.command(name="startgive", description="starts a giveaway")
@app_commands.describe(
    duration="giveaway的持续时间",
    winners="获胜者数量",
    prize="正在赠送的奖品",
)
async def startgive(self, interaction: discord.Interaction, duration: str, winners: int, prize: str):
    if interaction.user.guild_permissions.administrator:
        try:
            if (winners <= 0):
                await interaction.response.send_message("输入非负数的获胜者数量!", ephemeral=True)
            else:
                created_at = datetime.now()
                duration = parser.parse(duration)
                duration = duration + round(created_at.timestamp())

                embed = None
                try:
                    last_id = self.giveaways.pop()['id']
                    self.id = int(last_id) + 1
                except:
                    self.id = 1

                btn = GiveawayButton(self, self.id)

                giveaway = await self.create_giveaway({
                    'guild_id': interaction.guild.id,
                    'prize': prize,
                    'entries': None,
                    'winners': None,
                    'possible_winners': winners,
                    'duration': str(datetime.fromtimestamp(duration)),
                    'created_at': str(created_at),
                    'ended': False,
                })

                giveaway['created_at'] = datetime.fromtimestamp(duration)
                giveaway['interaction'] = interaction
                giveaway['entries'] = []
                giveaway['winners'] = []
                giveaway['btn'] = btn

                embed = await self.create_embed(giveaway)
                giveaway['embed'] = embed

                await interaction.response.send_message(embed=embed, view=btn)
                self.task_launcher(giveaway, 1)
        except Exception as e:
            print(e)
            await interaction.response.send_message("无效的时间格式!(1分钟/1m)", ephemeral=True)
    else:
        await interaction.response.send_message("您没有权限使用此功能。", ephemeral=True)

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

async def create_giveaway(self, data):
    endpoint = self.url + "/create-giveaway/"
    async with aiohttp.ClientSession() as session:
        async with session.post(endpoint, json=data) as r:
            if r.status == 200:
                await self.get_giveaways()
                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:

    @app_commands.command(name=&quot;startgive&quot;, description=&quot;starts a giveaway&quot;)
@app_commands.describe(
duration=&quot;duration of the giveaway&quot;, 
winners=&quot;number of winners&quot;,
prize=&quot;the prize being given away&quot;,
)
async def startgive(self, interaction: discord.Interaction, duration: str, winners: int, prize: str):
if interaction.user.guild_permissions.administrator:
try:
if (winners &lt;= 0):
await interaction.response.send_message(&quot;Type non-negative number of winners!&quot;, ephemeral=True)
else:
created_at = datetime.now()
duration = parser.parse(duration)
duration = duration + round(created_at.timestamp())
embed = None
try:        
last_id = self.giveaways.pop()[&#39;id&#39;]
self.id = int(last_id) + 1
except:
self.id = 1
btn = GiveawayButton(self, self.id)
giveaway = await self.create_giveaway({
&#39;guild_id&#39;: interaction.guild.id,
&#39;prize&#39;: prize,
&#39;entries&#39;: None,
&#39;winners&#39;: None,
&#39;possible_winners&#39;: winners,
&#39;duration&#39;: str(datetime.fromtimestamp(duration)),
&#39;created_at&#39;: str(created_at),
&#39;ended&#39;: False,
})
giveaway[&#39;created_at&#39;] = datetime.fromtimestamp(duration)
giveaway[&#39;interaction&#39;] = interaction
giveaway[&#39;entries&#39;] = []
giveaway[&#39;winners&#39;] = []
giveaway[&#39;btn&#39;] = btn
embed = await self.create_embed(giveaway)
giveaway[&#39;embed&#39;] = embed
await interaction.response.send_message(embed=embed, view=btn)
self.task_launcher(giveaway, 1)
except Exception as e:
print(e)
await interaction.response.send_message(&quot;Invalid time format! (1 minutes/1m)&quot;, ephemeral=True)
else:
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:

async def create_giveaway(self, data):
endpoint = self.url + &quot;/create-giveaway/&quot;
async with aiohttp.ClientSession() as session:
async with session.post(endpoint, json=data) as r:
if r.status == 200:
await self.get_giveaways()
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:

确定