当我在使用python和nextcard编写discordbot时使用defer()时,Slash命令没有响应并出错



我正在使用python开发一个discordbot。一切都很好,一个小问题是有时当我使用slash_command时,它会返回:"应用程序没有响应"。即使在后台slash_command完成了任务。

我尝试使用ctx.defer((延迟,但收到以下错误消息:

'命令引发异常:AttributeError:"Interaction"对象具有没有属性"推迟">

Im使用nextcard,这是代码的示例:

import nextcord
from nextcord.ext import commands, tasks, application_checks
import boto3
import traceback
import sys
client = boto3.client(
'dynamodb',
aws_access_key_id= '',
aws_secret_access_key='',
region_name='us-east-1'
)
dynamodb = boto3.resource(
'dynamodb',
aws_access_key_id= '',
aws_secret_access_key='',
region_name='us-east-1'
)
ddb_exceptions = client.exceptions
table = dynamodb.Table('')
table_edit_messages = dynamodb.Table('')
token=''
intents = nextcord.Intents.all()
bot = commands.Bot(command_prefix="/", intents=intents)
messages = {}
owners = [8350.., 4594.., 9094..]
@bot.slash_command()
async def start(ctx, guild_id):
global owners
if ctx.user.id in owners:
await ctx.defer()
client.put_item(TableName='XXXXX', Item={'guild_id':{'N':str(guild_id)}, 'guild_name':{"S":str(bot.get_guild(int(guild_id)))}})
embed = nextcord.Embed(title= '✅ Bot started !', description='''The bot is started successfully for server: ` {} ` !'''.format(str(bot.get_guild(int(guild_id)))) + 'n' + 'n'
         + '''*Please check that the name of the server is correctly displayed. If not, it's likely that the bot is not in the specified server!*''', color=0xC86C90)
await ctx.send(embed=embed, ephemeral=True)

我相信读/写dynamodb是命令需要3秒以上才能响应的原因。顺便说一句,这不是返回"应用程序没有响应"的命令。这是另一个从dynamodb读取/写入的命令,并且有多个if/else语句。

我有两个问题:为什么defer不起作用;第二个问题是,除了defer,还有其他解决方案可以让命令等待3秒以上吗

谢谢。

问题1

查看nextcard的文档-Context似乎没有任何名为defer的函数

问题2

可以使命令延迟的一种方法是使用非阻塞睡眠。

import asyncio
asyncio.sleep(5)

话虽如此,slash命令预计会在3秒内返回响应。nextcard。交互.响应.延迟((

@bot.slash_command()
async def start(interaction: nextcord.Interaction):
global owners
if interaction.user.id in owners:
await interaction.response.defer()
# Additionally sleep for more time if needed
# await asyncio.sleep(5)
client.put_item(TableName='XXXXX', Item={'guild_id':{'N':str(guild_id)}, 'guild_name':{"S":str(bot.get_guild(int(guild_id)))}})
embed = nextcord.Embed(title= '✅ Bot started !', description='''The bot is started successfully for server: ` {} ` !'''.format(str(bot.get_guild(int(guild_id)))) + 'n' + 'n'
         + '''*Please check that the name of the server is correctly displayed. If not, it's likely that the bot is not in the specified server!*''', color=0xC86C90)
await interaction.response.send_message(embed=embed, ephemeral=True)

最新更新