繁体   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