This commit is contained in:
unintendedfraud
2023-05-06 11:38:30 +02:00
parent 7b5de32be7
commit 13145cad31
2 changed files with 8 additions and 10 deletions
+7 -5
View File
@@ -10,22 +10,23 @@ import (
)
func OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) {
log.Printf("new message posted: [%s], par [%s], authorID [%s], state user id [%s]", m.Content, m.Author.Username, m.Author.ID, s.State.User.ID)
log.Printf("author [%+v], state user [%+v]", m.Author, s.State.User)
if m.Author.ID == s.State.User.ID {
return
}
if containUser(m.Mentions, s.State.User.ID) {
fmt.Println("going through openai stuff")
fmt.Println("create ai client")
aiclient := openai.NewClient(env.GetEnvVariables().OpenaiHalToken)
fmt.Println("ai client created")
res, err := aiclient.ChatCompletions(m.Content)
if err != nil {
log.Panicf("failed to query open ai with the following prompt [%s]. Error: %s", m.Content, err.Error())
}
fmt.Println("ai client replied")
if len(res.Choices) > 0 {
aiResponse := res.Choices[0].Message.Content
if _, err = s.ChannelMessageSend(m.ChannelID, aiResponse); err != nil {
@@ -33,13 +34,14 @@ func OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) {
}
}
fmt.Println("### ai did not return any response ###", res.Usage, res.Choices)
return
}
}
func containUser(users []*discordgo.User, userID string) bool {
for _, u := range users {
fmt.Println("## mentions ##", u.ID, u.Username, "-----", userID)
if u.ID == userID {
return true
}
+1 -5
View File
@@ -8,7 +8,6 @@ import (
)
type Client struct {
Token string
HttpClient *resty.Client
}
@@ -16,10 +15,7 @@ func NewClient(token string) *Client {
httpclient := resty.New()
httpclient.SetAuthToken(token)
return &Client{
Token: token,
HttpClient: httpclient,
}
return &Client{HttpClient: httpclient}
}
func (c Client) ChatCompletions(prompt string) (*CompletionResponse, error) {