简体   繁体   English

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

[英]Discord.py slash-command ctx.send()

everyone having a small issue with the Slash commands.每个人都对 Slash 命令有一个小问题。 The idea is that I fetch an Array of Assetdata convert each into an embedded and then send the embedded.我的想法是,我获取一个 Assetdata 数组,将每个数组转换为嵌入式,然后发送嵌入式。 If there is an image it will show an image and if a.mp4 is linked to the asset it will convert the mp4 to gif and then attache the file locally with file = discord.File().如果有图像,它将显示图像,如果 a.mp4 链接到资产,它将把 mp4 转换为 gif,然后使用 file = discord.File() 在本地附加文件。 This all worked just fine bevor moving to Slash commands.在转向 Slash 命令之前,这一切都工作得很好。 For some reason, the slash command wants to send a response bevor receiving all the data.出于某种原因,斜杠命令想要在接收所有数据之前发送响应。 This is an issue because the gif processing takes time or if a large sum of data is requested the request takes longer.这是一个问题,因为 gif 处理需要时间,或者如果请求大量数据,则请求需要更长的时间。 The function works fine if have a single asset to display and an Image.如果要显示单个资产和图像,function 工作正常。

So my question is how do I get ctx.send() to wait??所以我的问题是如何让 ctx.send() 等待?

Thanks in Advance everyone!在此先感谢大家!

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


@slash.slash(
    name="assetsearch",
    description="Sends a Message",
    guild_ids=[XXXXXXXXXXXXXXXX], #Hidden for StackOverflow
    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 = ""

Working Function bevor using slash-command使用斜杠命令工作 Function

    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 = ""

I figured it out.我想到了。 The Slashcomands are expecting a response within 3s if you are working with an API in the background and then precessing data this could become a criterium you cant meet consistently.如果您在后台使用 API 然后处理数据,Slashcomands 期望在 3 秒内得到响应,这可能成为您无法始终满足的标准。 This is why you would have the Bot itself respond directly upon request.这就是为什么您会让 Bot 本身根据请求直接响应的原因。 Once you are done with processing your data you can respond normaly处理完数据后,您可以正常回复

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

If you don't want the Bots message如果您不想要机器人消息

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

...or edit the message the Bot sent. ...或编辑机器人发送的消息。

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

Can't you just use asyncio?你不能只使用 asyncio 吗?

import asyncio导入异步

await asyncio.sleep(number)等待 asyncio.sleep(number)

Or is this not what you are looking for?或者这不是您要找的东西?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM