简体   繁体   English

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

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

I am facing problem in copying the prompt from the slash command我在从斜杠命令复制提示时遇到问题

@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 

here in the 4th line how do i make the bot capable of copying the text from imageprompt and generate the image according to that?在第 4 行,我如何使机器人能够从 imageprompt 复制文本并根据它生成图像? I have the generate image coded previously but i cant figure it out in case of slash commands of how to generate the image according to the user provided context我之前对生成图像进行了编码,但是在斜线命令的情况下我无法弄清楚如何根据用户提供的上下文生成图像

I tried using other syntax like message.content,message.summary none of them work i tried googling but found no answer我尝试使用其他语法,如 message.content、message.summary 它们都不起作用我尝试谷歌搜索但没有找到答案

The first argument given to the slash command is a discord.ApplicationContext object. This is context so thats why its commonly named ctx .斜杠命令的第一个参数是 discord.ApplicationContext object。这是上下文,所以这就是为什么它通常命名为ctx

The line await ctx.defer() makes the bot do the "bot is thinking" loading so you can still respond if the image took a while to generate. await ctx.defer()机器人执行“机器人正在思考”加载,因此如果图像需要一段时间才能生成,您仍然可以做出响应。

prompt is the value given by the users and is asked for when you run the slash command" prompt是用户给出的值,当您运行斜杠命令时会被询问”

And finally to respond to the message i use ctx.respond最后回复我使用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)

This code should work provided that the generate_image function you made works.如果您制作的generate_image function 有效,则此代码应该有效。

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

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