-
Notifications
You must be signed in to change notification settings - Fork 1
/
discord.go
190 lines (176 loc) · 4.48 KB
/
discord.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
)
const prefix string = "!"
var groqLUT map[string][]Message = map[string][]Message{}
func command(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == botID {
return
}
//We don't like other bots either
if m.Author.Bot {
return
}
if len(m.Content) == 0 {
return
}
if m.ReferencedMessage != nil {
ctx, contains := groqLUT[m.ReferencedMessage.ID]
if contains {
msg, history, err := AskGroq(m.Content, ctx)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Oopsie woopsie we got an error groq sisters:"+err.Error())
log.Println(err)
}
var messages = chunk(msg, 1000)
for _, v := range messages {
msg, err := s.ChannelMessageSend(m.ChannelID, v)
if err != nil {
panic(err)
}
groqLUT[msg.ID] = history
}
return
}
}
if strings.HasPrefix(m.Content, prefix) && len(m.Content) > len(prefix) {
m.Content = m.Content[len(prefix):]
tokens := strings.Split(m.Content, " ")
if tokens == nil {
return
}
switch strings.ToLower(tokens[0]) {
case "youtube":
if len(tokens) > 1 {
youtubeSearch(s, m, strings.Join(tokens[1:], " "))
}
case "next":
nextVideo(s, m)
case "limit":
if len(tokens) > 1 {
count, err := strconv.Atoi(tokens[1])
if err != nil || count < 1 || count > 25 {
s.ChannelMessageSend(m.ChannelID, "Failed to update limit")
break
}
MaxYoutubeResults = count
} else {
s.ChannelMessageSend(m.ChannelID, "Current limit:"+fmt.Sprint(MaxYoutubeResults))
}
case "moon", "moonphase", "phase":
moonPhase(s, m)
case "version", "v":
version(s, m)
case "fortune", "f":
if len(tokens) > 1 {
//Only want one word since that's all the API can take.
fortune(s, m, tokens[1])
} else {
//Can also be called without a word.
fortune(s, m, "")
}
case "4chan", "4ch":
if len(tokens) > 1 {
//Only want one word since that's all the API can take.
var board = strings.Trim(tokens[1], `\/`)
if BannedBoard(board) {
s.ChannelMessageSend(m.ChannelID, "Sorry but this is a banned board")
return
}
fourchan(s, m, tokens[1])
} else {
s.ChannelMessageSend(m.ChannelID, "Provide a board please!")
}
case "r", "radio", `r/a/dio`:
if len(tokens) > 1 {
switch tokens[1] {
case "now", "current", "dj", "np":
radioCurrent(s, m)
case "q", "queue", "next":
radioQueue(s, m)
case "search", "s":
if len(tokens) > 2 {
radioSearch(s, m, strings.Join(tokens[2:], " "))
}
default:
radioHelp(s, m)
}
} else {
radioCurrent(s, m)
}
case "np", "song", "dj":
radioCurrent(s, m)
case "queue":
radioQueue(s, m)
case "b", "birb", "bird", "birds":
birds(s, m)
case "clap", "👏", "c":
clap(s, m, tokens[1:])
case "groq":
msg, history, err := func() (string, []Message, error) {
if m.ReferencedMessage != nil {
var ctx = groqLUT[m.ReferencedMessage.ID]
return AskGroq(strings.Join(tokens[1:], " "), ctx)
} else {
var x = AskGroqSystem("You are a discord bot which has to answer questions succinctly as to not flood the channel", nil)
return AskGroq(strings.Join(tokens[1:], " "), x)
}
}()
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Oopsie woopsie we got an error groq sisters:"+err.Error())
log.Println(err)
}
var messages = chunk(msg, 2000)
for _, v := range messages {
msg, err := s.ChannelMessageSend(m.ChannelID, v)
if err != nil {
panic(err)
}
groqLUT[msg.ID] = history
}
case "groq-prompt":
role := strings.Join(tokens[1:], " ")
history := func() []Message {
if m.ReferencedMessage != nil {
var ctx = groqLUT[m.ReferencedMessage.ID]
return AskGroqSystem(role, ctx)
} else {
return AskGroqSystem(role, nil)
}
}()
msg, err := s.ChannelMessageSend(m.ChannelID, "My role is now:"+role)
if err != nil {
panic(err)
}
groqLUT[msg.ID] = history
default:
}
}
}
func chunk(s string, chunkSize int) []string {
if len(s) == 0 {
return nil
}
if chunkSize >= len(s) {
return []string{s}
}
var chunks []string = make([]string, 0, (len(s)-1)/chunkSize+1)
currentLen := 0
currentStart := 0
for i := range s {
if currentLen == chunkSize {
chunks = append(chunks, s[currentStart:i])
currentLen = 0
currentStart = i
}
currentLen++
}
chunks = append(chunks, s[currentStart:])
return chunks
}