英文:
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()
我尝试创建 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="startgive", description="starts a giveaway")
@app_commands.describe(
duration="duration of the giveaway",
winners="number of winners",
prize="the prize being given away",
)
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("Type non-negative number of winners!", 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("Invalid time format! (1 minutes/1m)", ephemeral=True)
else:
await interaction.response.send_message("You don't have permissions to use this.", 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 + "/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()
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论