-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI.go.bak3
125 lines (100 loc) · 2.99 KB
/
AI.go.bak3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
var (
discord *discordgo.Session
endpoint = "http://127.0.0.1:8080/api" // your API endpoint goes here
)
func main() {
configFile, err := os.Open("config.json")
if err != nil {
log.Fatalf("Failed to open config file: %v", err)
}
defer configFile.Close()
var config struct {
DiscordBotToken string `json:"token"`
}
err = json.NewDecoder(configFile).Decode(&config)
if err != nil {
log.Fatalf("Failed to decode config file: %v", err)
}
discordToken := config.DiscordBotToken
if discordToken == "" {
log.Fatalf("No discord bot token was provided in config file")
}
var errDiscord error
discord, errDiscord = discordgo.New("Bot " + discordToken)
if errDiscord != nil {
log.Fatalf("Failed to create a new Discord session: %v", errDiscord)
}
discord.AddHandler(handleMessage)
errDiscord = discord.Open()
if errDiscord != nil {
log.Fatalf("Failed to open a Discord connection: %v", errDiscord)
}
defer discord.Close()
log.Println("Discord bot is running. Press CTRL-C to exit.")
select {}
}
func handleMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot {
return
}
if strings.HasPrefix(m.Content, fmt.Sprintf("<@%s>", s.State.User.ID)) {
question := strings.TrimSpace(strings.TrimPrefix(m.Content, fmt.Sprintf("<@%s>", s.State.User.ID)))
if question == "" {
return
}
go func() {
var typingDuration time.Duration = 2 // set the typing indicator duration in seconds
// show the typing indicator
s.ChannelTyping(m.ChannelID)
time.Sleep(typingDuration * time.Second)
response, errAPI := askAPI(question)
if errAPI != nil {
log.Printf("Failed to get a response from the API: %v", errAPI)
return
}
// send the response message with the answer and stop the typing indicator
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s, %s", m.Author.Mention(), response))
}()
}
}
func askAPI(question string) (string, error) {
client := &http.Client{}
reqBody := map[string]string{"prompt": question} // modify the request body to include a prompt field
reqBytes, err := json.Marshal(reqBody)
if err != nil {
return "", fmt.Errorf("failed to marshal request body: %v", err)
}
req, err := http.NewRequest("POST", endpoint, bytes.NewReader(reqBytes))
if err != nil {
return "", fmt.Errorf("failed to create a new HTTP request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("failed to send HTTP request to the API: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("the API returned an error status code: %v", resp.Status)
}
var responseBuilder strings.Builder
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
responseBuilder.WriteString(scanner.Text())
responseBuilder.WriteString("\n")
}
return responseBuilder.String(), nil
}