簡體   English   中英

如何通過pycord中的斜杠命令在discord bot中生成圖像?

[英]How to generate images in discord bot through slash commands in pycord?

我在從斜杠命令復制提示時遇到問題

@bot.slash_command(name = 'generate', description='Generate any image')
async def generate(message, imageprompt: Option(str, description="The prompt to generate", required = True)):
    await message.channel.trigger_typing() #shows that the bot is typing 
    prompt = message.summary(imageprompt)
    image_url = await generate_image(prompt)
    try:
        await message.respond(image_url) #responds to the slash command (bot must respond within 3 seconds)
    except:
        await message.send(image_url) #sends as a regular message, if it cannot send as a slash command 

在第 4 行,我如何使機器人能夠從 imageprompt 復制文本並根據它生成圖像? 我之前對生成圖像進行了編碼,但是在斜線命令的情況下我無法弄清楚如何根據用戶提供的上下文生成圖像

我嘗試使用其他語法,如 message.content、message.summary 它們都不起作用我嘗試谷歌搜索但沒有找到答案

斜杠命令的第一個參數是 discord.ApplicationContext object。這是上下文,所以這就是為什么它通常命名為ctx

await ctx.defer()機器人執行“機器人正在思考”加載,因此如果圖像需要一段時間才能生成,您仍然可以做出響應。

prompt是用戶給出的值,當您運行斜杠命令時會被詢問”

最后回復我使用ctx.respond的消息

@bot.slash_command(name="generate", description="Generate any image")
async def generate(
    ctx: discord.ApplicationContext,
    prompt: Option(str, description="The prompt to generate", required=True),
):
    await ctx.defer()
    image_url = await generate_image(prompt)
    await ctx.respond(image_url)

如果您制作的generate_image function 有效,則此代碼應該有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM