Small optimizations

This commit is contained in:
Atridad Lahiji 2024-01-09 21:05:23 -07:00
parent 4234b7e7bc
commit 4f6b7894a9
No known key found for this signature in database

40
main.go
View file

@ -8,6 +8,7 @@ import (
"himbot/lib" "himbot/lib"
"io" "io"
"log" "log"
"net"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@ -25,6 +26,7 @@ import (
openai "github.com/sashabaranov/go-openai" openai "github.com/sashabaranov/go-openai"
) )
// Command metadata
var commands = []api.CreateCommandData{ var commands = []api.CreateCommandData{
{ {
Name: "ping", Name: "ping",
@ -76,6 +78,7 @@ var commands = []api.CreateCommandData{
}, },
} }
// Entrypoint
func main() { func main() {
godotenv.Load(".env") godotenv.Load(".env")
@ -148,7 +151,8 @@ func (h *handler) cmdAsk(ctx context.Context, data cmdroute.CommandData) *api.In
return errorResponse(err) return errorResponse(err)
} }
client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) apiKey := os.Getenv("OPENAI_API_KEY")
client := openai.NewClient(apiKey)
resp, err := client.CreateChatCompletion( resp, err := client.CreateChatCompletion(
context.Background(), context.Background(),
@ -164,32 +168,18 @@ func (h *handler) cmdAsk(ctx context.Context, data cmdroute.CommandData) *api.In
) )
if err != nil { if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err) return errorResponse(err)
return &api.InteractionResponseData{
Content: option.NewNullableString("ChatCompletion Error!"),
AllowedMentions: &api.AllowedMentions{}, // don't mention anyone
}
} }
respString := resp.Choices[0].Message.Content respString := resp.Choices[0].Message.Content
if len(respString) > 1800 { if len(respString) > 1800 {
textFile := bytes.NewBuffer([]byte(respString)) respString = respString[:1800]
file := sendpart.File{
Name: "himbot_response.txt",
Reader: textFile,
} }
return &api.InteractionResponseData{
Content: option.NewNullableString("Prompt: " + options.Prompt + "\n" + "Response:\n"),
AllowedMentions: &api.AllowedMentions{}, // don't mention anyone
Files: []sendpart.File{file},
}
}
return &api.InteractionResponseData{ return &api.InteractionResponseData{
Content: option.NewNullableString("Prompt: " + options.Prompt + "\n" + "Response: " + respString), Content: option.NewNullableString("Prompt: " + options.Prompt + "\n" + "Response: " + respString),
AllowedMentions: &api.AllowedMentions{}, // don't mention anyone AllowedMentions: &api.AllowedMentions{},
} }
} }
@ -343,9 +333,19 @@ func (h *handler) cmdHS(ctx context.Context, data cmdroute.CommandData) *api.Int
} }
func errorResponse(err error) *api.InteractionResponseData { func errorResponse(err error) *api.InteractionResponseData {
var content string
switch e := err.(type) {
case *net.OpError:
content = "**Network Error:** " + e.Error()
case *os.PathError:
content = "**File Error:** " + e.Error()
default:
content = "**Error:** " + err.Error()
}
return &api.InteractionResponseData{ return &api.InteractionResponseData{
Content: option.NewNullableString("**Error:** " + err.Error()), Content: option.NewNullableString(content),
Flags: discord.EphemeralMessage, Flags: discord.EphemeralMessage,
AllowedMentions: &api.AllowedMentions{ /* none */ }, AllowedMentions: &api.AllowedMentions{},
} }
} }