-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
236 lines (201 loc) · 5.56 KB
/
main.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"os"
"fmt"
"strings"
"net/http"
"log"
"flag"
"math/rand"
"github.com/bwmarrin/discordgo"
)
type ResponseMagic struct {
Response Response `json:"magic"`
}
type Response struct {
Question string `json:"question"`
Answer string `json:"answer"`
Type string `json:"type"`
}
var (
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not")
)
var BotID string
var Keys = []string{
"Affirmative",
"Non-committal",
"Negative",
}
var Responses = map[string][]string{
"Affirmative": {
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
},
"Non-committal": {
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
},
"Negative": {
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful.",
},
}
var commands = []*discordgo.ApplicationCommand{
{
Name: "magic8",
Description: "Command for getting a magic 8 ball response",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "question",
Description: "question",
Required: true,
},
},
},
}
var commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"magic8": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
channel := make(chan ResponseMagic)
go GetResponse(i.ApplicationCommandData().Options[0].StringValue(), channel)
response := <-channel
embed := &discordgo.MessageEmbed{
Title: response.Response.Answer,
Fields: []*discordgo.MessageEmbedField{
&discordgo.MessageEmbedField{
Name: "Question",
Value: response.Response.Question,
},
&discordgo.MessageEmbedField{
Name: "Answer Type",
Value: response.Response.Type,
},
},
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
embed,
},
},
})
},
}
var discord *discordgo.Session
func init() {
var err error
token := os.Getenv("token")
discord, err = discordgo.New("Bot " + token)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
discord.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
}
func main() {
user, err := discord.User("@me")
if err != nil {
fmt.Println(err.Error())
}
BotID = user.ID
discord.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("Bot is up!")
})
discord.AddHandler(MessageHandler)
err = discord.Open()
for _, v := range commands {
_, err := discord.ApplicationCommandCreate(discord.State.User.ID, "", v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
}
if err != nil {
fmt.Println(err.Error())
return
}
go KeepAlive()
defer discord.Close()
<- make(chan struct{})
return
}
func MessageHandler(session *discordgo.Session, message *discordgo.MessageCreate) {
if message.Author.ID == BotID {
return
}
if strings.HasPrefix(message.Content, "!magic8"){
question := strings.TrimSpace(strings.TrimPrefix(message.Content, "!magic8"))
if question == ""{
embed := &discordgo.MessageEmbed{
Title: "Question cannot be nothing.",
Description:"How to use command: `!magic8 (question)`",
Fields: []*discordgo.MessageEmbedField{
&discordgo.MessageEmbedField{
Name: "Asker",
Value: message.Author.Mention(),
},
},
}
session.ChannelMessageSendEmbed(message.ChannelID, embed)
return
}
channel := make(chan ResponseMagic)
go GetResponse(question, channel)
response := <-channel
embed := &discordgo.MessageEmbed{
Title: response.Response.Answer,
Fields: []*discordgo.MessageEmbedField{
&discordgo.MessageEmbedField{
Name: "Question",
Value: response.Response.Question,
},
&discordgo.MessageEmbedField{
Name: "Answer Type",
Value: response.Response.Type,
},
&discordgo.MessageEmbedField{
Name: "Asker",
Value: message.Author.Mention(),
},
},
}
session.ChannelMessageSendEmbed(message.ChannelID, embed)
}
}
func GetResponse(question string, channel chan ResponseMagic) {
type_ := Keys[rand.Intn(len(Keys))]
answer := Responses[type_][rand.Intn(len(Responses[type_]))]
responseObject := ResponseMagic{
Response: Response{
Question: question,
Answer: answer,
Type: type_,
},
}
channel<-responseObject
}
func KeepAlive(){
http.HandleFunc("/", IndexHandler)
http.ListenAndServe(":8000", nil)
}
func IndexHandler(w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w, "<a href='https://www.youtube.com/watch?v=dQw4w9WgXcQ'>Click here</a>")
}