Discord.py 斜线命令 ctx.send()

Discord.py slash-command ctx.send()

每个人都对 Slash 命令有小问题。 我的想法是,我获取一个 Assetdata 数组,将每个数组转换为嵌入式,然后发送嵌入式。如果有图像,它将显示图像,如果 .mp4 链接到资产,它将把 mp4 转换为 gif,然后使用 file = discord.File() 在本地附加文件。在转向 Slash 命令之前,这一切都工作得很好。出于某种原因,斜杠命令想要在接收所有数据之前发送响应。这是一个问题,因为 gif 处理需要时间,或者如果请求大量数据,则请求需要更长的时间。如果要显示单个资产和图像,该功能可以正常工作。

所以我的问题是如何让 ctx.send() 等待?

提前谢谢大家!

client = commands.Bot(command_prefix = "!")
slash = SlashCommand(client, sync_commands=True)


@slash.slash(
    name="assetsearch",
    description="Sends a Message",
    guild_ids=[XXXXXXXXXXXXXXXX], #Hidden for Whosebug
    options = [
        create_option(
            name = "searchtype",
            description = "Please choose how you would like to search for an Asset.",
            required = True,
            option_type = 3,
            choices = [
                create_choice(
                    name = "Search by Asset Id",
                    value = "id"
                ),
                create_choice(
                    name = "Search by Collection",
                    value = "colec"
                ),
                create_choice(
                    name = "Search by Accountname",
                    value = "acc"
                ),
                create_choice(
                    name = "Search by Asset Name",
                    value = 'match'
                )
            ]
        ),
        create_option(
            name = "searchterm",
            description = "Please enter an Asset Id, an Account name, the name of an Asset or the Collection name.",
            required = True,
            option_type = 3
            ),

        create_option(
            name = "amount",
            description = "Please enter how many Assets you would like to display.",
            required = True,
            option_type = 3
            ),
    ],
)

async def _assetsearch(ctx:SlashContext, searchtype: str, searchterm: str, amount: str):
    response = await getAssetData(searchtype, searchterm, amount) #Fetches an Array of AssetData, async function
    for asset in response:
        nftEmbed, nftFile = await __formatMessage(asset) #formats the message, converts mp4 from website to gif, returns the gif and ebeded
        print(nftEmbed,nftFile)
        await ctx.send(embed=nftEmbed, file=nftFile) #send the embeded and gif, ISSUE!!!
        if os.path.isfile('Nft.gif'): os.remove("Nft.gif")
    response = ""

使用斜杠命令之前的工作函数

    if msg.startswith('$search'):
        try:
            searchType = msg.split(",")[1]
        except:
            searchType = 'match'
        try:
            searchTerm = msg.split(",")[2]
        except:
            searchTerm = 'abcd'
        try:
            searchLimit = msg.split(",")[3]
        except:
            searchLimit = '1'
        response = getAssetData(searchType,searchTerm,searchLimit)
        for asset in response:
            nftEmbed, file = __formatMessage(asset)
            await message.channel.send(embed=nftEmbed, file=file)
            if os.path.isfile('Nft.gif'): os.remove("Nft.gif")
        response = ""

你不能只使用 asyncio 吗?

导入异步

等待asyncio.sleep(个数)

或者这不是您要找的东西?

我明白了。如果您在后台使用 API 然后处理数据,Slashcomands 期望在 3 秒内得到响应,这可能成为您无法始终如一地满足的标准。这就是为什么您会让 Bot 本身根据请求直接响应的原因。处理完数据后,您可以正常回复

async def _slashcomandfunction(ctx:SlashContext, str:otherparameters,):
    message = await ctx.send('Sometext') #Direct respond
    await ctx.channel.send(embed=embed, file=file) #Actual message

如果您不想收到机器人消息

message = await ctx.send('Sometext',delete_after=1)

...或编辑 Bot 发送的消息。

message = await ctx.send('Sometext')
await message.edit(content="newcontent")