-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrama.go
163 lines (135 loc) · 3.74 KB
/
drama.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/sashabaranov/go-openai"
"math/rand"
"os"
"strings"
)
type Words struct {
Peoples []string `json:"peoples"`
Categories [][]string `json:"categories"`
}
type Config struct {
Token string `json:"token"`
OpenAiKey string `json:"openai_key"`
GuildID string `json:"guild_id"`
}
func main() {
config, err := loadConfig()
if err != nil {
fmt.Println("Error loading config: ", err)
return
}
words, err := loadWords()
if err != nil {
fmt.Println("Error loading words: ", err)
return
}
discord, err := discordgo.New("Bot " + config.Token)
if err != nil {
fmt.Println("Error creating Discord session: ", err)
return
}
err = discord.Open()
if err != nil {
fmt.Println("Error opening Discord session: ", err)
return
}
err = createCommand(discord, config.GuildID)
if err != nil {
fmt.Println("Error creating command: ", err)
return
}
discord.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.ApplicationCommandData().Name == "generate" {
err := generateDescription(s, i, words, config)
if err != nil {
return
}
}
})
fmt.Printf("Bot is now running on %d servers", len(discord.State.Guilds))
select {}
}
func loadConfig() (*Config, error) {
bytes, err := os.ReadFile("config.json")
if err != nil {
return nil, fmt.Errorf("error reading file: %v", err)
}
var config Config
err = json.Unmarshal(bytes, &config)
if err != nil {
return nil, fmt.Errorf("error unmarshalling JSON: %v", err)
}
return &config, nil
}
func loadWords() (*Words, error) {
bytes, err := os.ReadFile("words.json")
if err != nil {
return nil, fmt.Errorf("error reading file: %v", err)
}
var words Words
err = json.Unmarshal(bytes, &words)
if err != nil {
return nil, fmt.Errorf("error unmarshalling JSON: %v", err)
}
return &words, nil
}
func createCommand(discord *discordgo.Session, guildID string) error {
command := &discordgo.ApplicationCommand{
Name: "generate",
Description: "Generuje opis dramatycznej sytuacji",
}
_, err := discord.ApplicationCommandCreate(discord.State.User.ID, guildID, command)
if err != nil {
return fmt.Errorf("error creating command: %v", err)
}
return nil
}
func generateDescription(s *discordgo.Session, i *discordgo.InteractionCreate, words *Words, config *Config) error {
var wordsArr = [3]string{}
for i := 0; i < 3; i++ {
categoryIndex := rand.Intn(len(words.Categories))
wordIndex := rand.Intn(len(words.Categories[categoryIndex]))
wordsArr[i] = words.Categories[categoryIndex][wordIndex]
}
peopleIndex := rand.Intn(len(words.Peoples))
prompt := fmt.Sprintf("Proszę wygeneruj dla mnie TYLKO JEDEN opis dramatycznej sytuacji korzystając z wyrazów: %s, pamiętaj aby uwzględnić osobę: %s. Napisz to w jednym krótkim zdaniu.", strings.Join(wordsArr[:], ", "), words.Peoples[peopleIndex])
client := openai.NewClient(config.OpenAiKey)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
},
)
if err != nil {
fmt.Printf("Completion error: %v\n", err)
return nil
}
embed := &discordgo.MessageEmbed{
Title: "EternalCode.pl - DramaGPT",
Description: resp.Choices[0].Message.Content,
Color: 0x00ff00,
}
reply := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{embed},
},
}
respond := s.InteractionRespond(i.Interaction, reply)
if respond != nil {
fmt.Println(respond)
}
return nil
}