fix: upload dalle image to discord

This commit is contained in:
Jack Merrill 2023-10-26 10:01:02 -04:00
parent 40f286f29d
commit a95b599bd3
No known key found for this signature in database
GPG Key ID: 2FD365A1E0C8E3C8

View File

@ -3,9 +3,11 @@ package fun
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/jackmerrill/hampbot/internal/utils/config"
"github.com/jackmerrill/hampbot/internal/utils/embed"
"github.com/sashabaranov/go-openai"
@ -68,10 +70,31 @@ func (c *Dalle) Exec(ctx shireikan.Context) error {
return err
}
ctx.ReplyEmbed(embed.NewSuccessEmbed(ctx).
SetTitle("DALL-E").
SetDescription(prompt).
SetImage(resp.Data[0].URL).
MessageEmbed)
// fetch image from url
img, err := http.Get(resp.Data[0].URL)
if err != nil {
ctx.ReplyEmbed(embed.NewErrorEmbed(ctx).SetDescription("Failed to fetch image.").MessageEmbed)
return err
}
ctx.GetSession().ChannelMessageSendComplex(ctx.GetChannel().ID, &discordgo.MessageSend{
Content: "Generated image",
Embed: &discordgo.MessageEmbed{
Title: "Generated image",
Description: fmt.Sprintf("Prompt: `%s`", prompt),
Image: &discordgo.MessageEmbedImage{
URL: "attachment://image.png",
},
Color: 0x00ff00,
},
Reference: ctx.GetMessage().Reference(),
Files: []*discordgo.File{
{
Name: "image.png",
ContentType: "image/png",
Reader: img.Body,
},
},
})
return nil
}