2024-01-21 00:27:52 -07:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"himbot/lib"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/diamondburned/arikawa/v3/api"
|
|
|
|
"github.com/diamondburned/arikawa/v3/api/cmdroute"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json/option"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/sendpart"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Ask(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
|
|
|
|
// Cooldown Logic
|
|
|
|
allowed, cooldownString := lib.CooldownHandler(*data.Event, "ask", time.Minute)
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
return lib.ErrorResponse(errors.New(cooldownString))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command Logic
|
|
|
|
var options struct {
|
|
|
|
Prompt string `discord:"prompt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := data.Options.Unmarshal(&options); err != nil {
|
2024-02-23 18:42:51 -07:00
|
|
|
lib.CancelCooldown(data.Event.Member.User.ID.String(), "ask")
|
2024-01-21 00:27:52 -07:00
|
|
|
return lib.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:52:59 -07:00
|
|
|
respString, err := lib.ReplicateTextGeneration(options.Prompt)
|
2024-01-21 00:27:52 -07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ChatCompletion error: %v\n", err)
|
2024-02-23 18:42:51 -07:00
|
|
|
lib.CancelCooldown(data.Event.Member.User.ID.String(), "ask")
|
2024-01-21 00:27:52 -07:00
|
|
|
return &api.InteractionResponseData{
|
|
|
|
Content: option.NewNullableString("ChatCompletion Error!"),
|
|
|
|
AllowedMentions: &api.AllowedMentions{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(respString) > 1800 {
|
|
|
|
textFile := bytes.NewBuffer([]byte(respString))
|
|
|
|
|
|
|
|
file := sendpart.File{
|
2024-02-05 00:17:25 -07:00
|
|
|
Name: "himbot_response.md",
|
2024-01-21 00:27:52 -07:00
|
|
|
Reader: textFile,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.InteractionResponseData{
|
2024-02-05 00:17:25 -07:00
|
|
|
Content: option.NewNullableString("Prompt: " + options.Prompt + "\n"),
|
2024-01-21 00:27:52 -07:00
|
|
|
AllowedMentions: &api.AllowedMentions{},
|
|
|
|
Files: []sendpart.File{file},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &api.InteractionResponseData{
|
2024-02-05 00:17:25 -07:00
|
|
|
Content: option.NewNullableString("Prompt: " + options.Prompt + "\n--------------------\n" + respString),
|
2024-01-21 00:27:52 -07:00
|
|
|
AllowedMentions: &api.AllowedMentions{},
|
|
|
|
}
|
|
|
|
}
|