mirror of
https://github.com/jackmerrill/hampbot.git
synced 2025-04-19 02:00:47 -04:00
feat: dalle
This commit is contained in:
parent
ab852f001d
commit
62706e8309
77
internal/commands/fun/dalle.go
Normal file
77
internal/commands/fun/dalle.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package fun
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/jackmerrill/hampbot/internal/utils/config"
|
||||||
|
"github.com/jackmerrill/hampbot/internal/utils/embed"
|
||||||
|
"github.com/sashabaranov/go-openai"
|
||||||
|
"github.com/zekroTJA/shireikan"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Dalle struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Dalle) GetInvokes() []string {
|
||||||
|
return []string{"dalle", "ai-art"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Dalle) GetDescription() string {
|
||||||
|
return "Generate AI art with DALL-E"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Dalle) GetHelp() string {
|
||||||
|
return "`dalle [prompt]` - Generate AI art with DALL-E"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Dalle) GetGroup() string {
|
||||||
|
return config.GroupFun
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Dalle) GetDomainName() string {
|
||||||
|
return "hamp.fun.dalle"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Dalle) GetSubPermissionRules() []shireikan.SubPermission {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (c *Dalle) IsExecutableInDMChannels() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Dalle) Exec(ctx shireikan.Context) error {
|
||||||
|
openaiToken := os.Getenv("OPENAI_TOKEN")
|
||||||
|
|
||||||
|
if openaiToken == "" {
|
||||||
|
ctx.ReplyEmbed(embed.NewErrorEmbed(ctx).SetDescription("No OpenAI token set.").MessageEmbed)
|
||||||
|
return fmt.Errorf("no openai token set")
|
||||||
|
}
|
||||||
|
|
||||||
|
prompt := strings.TrimPrefix(ctx.GetMessage().Content, fmt.Sprintf("%sdalle ", config.BotPrefix))
|
||||||
|
|
||||||
|
client := openai.NewClient(openaiToken)
|
||||||
|
|
||||||
|
ctx.GetSession().ChannelTyping(ctx.GetChannel().ID)
|
||||||
|
|
||||||
|
resp, err := client.CreateImage(context.Background(), openai.ImageRequest{
|
||||||
|
Prompt: prompt,
|
||||||
|
Size: openai.CreateImageSize1024x1024,
|
||||||
|
ResponseFormat: openai.CreateImageResponseFormatURL,
|
||||||
|
N: 1,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
ctx.ReplyEmbed(embed.NewErrorEmbed(ctx).SetDescription("Failed to generate image.").MessageEmbed)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.ReplyEmbed(embed.NewSuccessEmbed(ctx).
|
||||||
|
SetTitle("DALL-E").
|
||||||
|
SetDescription(prompt).
|
||||||
|
SetImage(resp.Data[0].URL).
|
||||||
|
MessageEmbed)
|
||||||
|
return nil
|
||||||
|
}
|
6
main.go
6
main.go
@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/jackmerrill/hampbot/internal/commands/fun"
|
"github.com/jackmerrill/hampbot/internal/commands/fun"
|
||||||
studentlife "github.com/jackmerrill/hampbot/internal/commands/studentlife"
|
studentlife "github.com/jackmerrill/hampbot/internal/commands/studentlife"
|
||||||
util "github.com/jackmerrill/hampbot/internal/commands/util"
|
util "github.com/jackmerrill/hampbot/internal/commands/util"
|
||||||
|
"github.com/jackmerrill/hampbot/internal/utils/config"
|
||||||
"github.com/zekroTJA/shireikan"
|
"github.com/zekroTJA/shireikan"
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
@ -38,7 +39,7 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
handler := shireikan.New(&shireikan.Config{
|
handler := shireikan.New(&shireikan.Config{
|
||||||
GeneralPrefix: ">",
|
GeneralPrefix: config.BotPrefix,
|
||||||
AllowBots: false,
|
AllowBots: false,
|
||||||
AllowDM: true,
|
AllowDM: true,
|
||||||
ExecuteOnEdit: true,
|
ExecuteOnEdit: true,
|
||||||
@ -69,6 +70,9 @@ func main() {
|
|||||||
handler.Register(&fun.XKCD{})
|
handler.Register(&fun.XKCD{})
|
||||||
log.Debug("Registered xkcd command")
|
log.Debug("Registered xkcd command")
|
||||||
|
|
||||||
|
handler.Register(&fun.Dalle{})
|
||||||
|
log.Debug("Registered dalle command")
|
||||||
|
|
||||||
log.Info("Registered all commands")
|
log.Info("Registered all commands")
|
||||||
|
|
||||||
handler.Setup(session)
|
handler.Setup(session)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user