-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
450 lines (400 loc) · 12.9 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package main
import (
"context"
"flag"
"fmt"
"log"
"math"
"math/rand"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
//"time"
"github.com/bwmarrin/discordgo"
"github.com/go-co-op/gocron"
"go.mongodb.org/mongo-driver/bson"
//"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func init() {
//flag.StringVar(&token, "t", "", "Bot Token")
flag.Parse()
}
var token = os.Getenv("DISCORD_TOKEN")
var dbClient mongo.Client = mongo.Client{}
var scheduler gocron.Scheduler = gocron.Scheduler{}
func main() {
scheduler = *gocron.NewScheduler(time.UTC)
//Init banana database
dbClient = *initDatabase()
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + token)
if err != nil {
fmt.Println("Error creating Discord session: ", err)
return
}
// Register ready as a callback for the ready events.
dg.AddHandler(ready)
// Register messageCreate as a callback for the messageCreate events.
dg.AddHandler(messageCreate)
// We need information about guilds (which includes their channels),
// messages and voice states.
dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentsGuildVoiceStates
// Open the websocket and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("Error opening Discord session: ", err)
}
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Delam opici zvuky. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session.
dg.Close()
}
// This function will be called (due to AddHandler above) when the bot receives
// the "ready" event from Discord.
func ready(s *discordgo.Session, event *discordgo.Ready) {
// Set the playing status.
//s.UpdateGameStatus(0, "Krsipina smrdi jak tvoje mamka lool opice")
s.UpdateGameStatus(0, "epicka bananova plantáž")
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the autenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot {
return
}
if strings.ToLower(m.Content) == "b" {
_ = GetUserData(dbClient, m.Author.Username, m.Author.ID)
banans := rand.Intn(16)
addBanans(dbClient, m.Author.ID, banans)
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: 0x5f119e,
Title: m.Author.Username,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Dostal/a jsi: " + strconv.Itoa(int(banans)) + " 🍌",
Value: "🐒Získal/a jsi banány!🐒",
Inline: false,
},
},
Footer: &discordgo.MessageEmbedFooter{
Text: "Credits: @Matyslav_ || Přispěj na vývoj opičáka na patreon.com/Padisoft 🐒",
},
}
s.ChannelMessageSendEmbed(m.ChannelID, embed)
}
if strings.ToLower(m.Content) == "plantaz" {
user := GetUserData(dbClient, m.Author.Username, m.Author.ID)
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: 0x5f119e,
Title: m.Author.Username,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Vlastníš: " + strconv.Itoa(int(user["bananas"].(int32))) + " 🍌",
Value: "Miluju opice. 🐒 A taky banány!",
Inline: false,
},
},
Footer: &discordgo.MessageEmbedFooter{
Text: "Credits: @Matyslav_ || Přispěj na vývoj opičáka na patreon.com/Padisoft 🐒",
},
}
s.ChannelMessageSendEmbed(m.ChannelID, embed)
}
if strings.ToLower(m.Content) == "b money" {
user := GetUserData(dbClient, m.Author.Username, m.Author.ID)
money := 0
if user["money"] != nil {
money = int(user["money"].(int32))
} else {
addMoney(dbClient, m.Author.ID, 0)
}
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: 0x5f119e,
Title: m.Author.Username,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Vlastníš: " + strconv.Itoa(money) + " Opicich dolaru",
Value: "Miluju opice. 🐒 A taky banány!",
Inline: false,
},
},
Footer: &discordgo.MessageEmbedFooter{
Text: "Credits: @Matyslav_ || Přispěj na vývoj opičáka na patreon.com/Padisoft 🐒",
},
}
s.ChannelMessageSendEmbed(m.ChannelID, embed)
}
if strings.ToLower(m.Content) == "b sell" {
user := GetUserData(dbClient, m.Author.Username, m.Author.ID)
bananas := int(user["bananas"].(int32))
money := math.Round(float64(bananas / 5))
resetBananas(dbClient, m.Author.ID, bananas)
addMoney(dbClient, m.Author.ID, int(money))
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: 0x5f119e,
Title: m.Author.Username,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Prodal/a/o jsi : " + strconv.Itoa(bananas) + "🍌 za " + strconv.Itoa(int(money)) + " Opicich dolaru",
Value: "Miluju opice. 🐒 A taky banány!",
Inline: false,
},
},
Footer: &discordgo.MessageEmbedFooter{
Text: "Credits: @Matyslav_ || Přispěj na vývoj opičáka na patreon.com/Padisoft 🐒",
},
}
s.ChannelMessageSendEmbed(m.ChannelID, embed)
}
if strings.ToLower(m.Content) == "b hovno" {
user := GetUserData(dbClient, m.Author.Username, m.Author.ID)
if user["money"] == nil {
addMoney(dbClient, m.Author.ID, 0)
}
money := int(user["money"].(int32))
if money > 100 {
addHovno(dbClient, m.Author.ID)
addMoney(dbClient, m.Author.ID, -100)
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: 0x5f119e,
Title: m.Author.Username,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Koupil/a/o jsi 1 opici hovno za 100 Opicich dolaru. Pouzij jej prikazem 'hovno @User'",
Value: "Miluju opice. 🐒 A taky banány!",
Inline: false,
},
},
Footer: &discordgo.MessageEmbedFooter{
Text: "Credits: @Matyslav_ || Přispěj na vývoj opičáka na patreon.com/Padisoft 🐒",
},
}
s.ChannelMessageSendEmbed(m.ChannelID, embed)
} else {
s.ChannelMessageSendReply(m.ChannelID, "Potrebujes aspon 100 opicich dolaru pro koupi opiciho hovna", m.Reference())
}
}
if strings.Contains(strings.ToLower(m.Content), "hovno") {
if len(m.Mentions) == 1 {
res := subHovno(dbClient, m.Author.Username, m.Author.ID)
if !res {
s.ChannelMessageSendReply(m.ChannelID, "Nemas dost hoven :// kup nejake pres b hovno", m.Reference())
return
}
scheduler.Clear()
if m.Mentions[0].ID == "m.Mentions[0].ID" {
s.ChannelMessageSend(m.ChannelID, "⚠️⚠️⚠️⚠️")
s.ChannelMessageSend(m.ChannelID, "Za trest budes ohovnen")
scheduler.Every(10).Seconds().Do(func() {
s.ChannelMessageSend(m.ChannelID, "<@"+m.Author.ID+"> byl/a/o jsi proklet opicim prokletim. Hod hovno po nekom dalsim aby jsi se ho zbavil")
},
)
scheduler.StartAsync()
return
}
s.ChannelMessageSendReply(m.ChannelID, "Hodil/a/o jsi opici hovno po <@"+m.Mentions[0].ID+">", m.Reference())
scheduler.Every(2).Minutes().Do(func() {
s.ChannelMessageSend(m.ChannelID, "<@"+m.Mentions[0].ID+"> byl/a/o jsi proklet opicim prokletim. Hod hovno po nekom dalsim aby jsi se ho zbavil")
},
)
scheduler.StartAsync()
}
}
if strings.ToLower(m.Content) == "b top" {
topUsers := GetTopUsers(dbClient)
var fields []*discordgo.MessageEmbedField
//decodes the monkeys
for i, monke := range topUsers {
field := discordgo.MessageEmbedField{
Name: strconv.Itoa(i+1) + ". " + monke["userName"].(string),
Value: "Banánů: " + strconv.Itoa(int(monke["bananas"].(int32))),
Inline: false,
}
fields = append(fields, &field)
}
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: 0xfcba03, // Green
Title: "🐒** Nejlepší opičáci: **🐒",
Fields: fields,
Footer: &discordgo.MessageEmbedFooter{
Text: "Credits: @Matyslav_ || Přispěj na vývoj opičáka na patreon.com/Padisoft 🐒",
},
}
s.ChannelMessageSendEmbed(m.ChannelID, embed)
}
if strings.Contains(strings.ToLower(m.Content), "get nerded") {
if len(m.Mentions) == 1 {
nerdedMessagesCount := 0
nerdedMessagesTBD := 15
messages, _ := s.ChannelMessages(m.ChannelID, 100, "", "", "")
for _, message := range messages {
if message.Author.ID == m.Mentions[0].ID {
s.MessageReactionAdd(message.ChannelID, message.ID, "🤓")
nerdedMessagesCount++
nerdedMessagesTBD--
if nerdedMessagesTBD == 0 {
break
}
}
}
}
}
if strings.Contains(strings.ToLower(m.Content), "get jinxed") {
if len(m.Mentions) == 1 {
nerdedMessagesCount := 0
nerdedMessagesTBD := 15
messages, _ := s.ChannelMessages(m.ChannelID, 100, "", "", "")
for _, message := range messages {
if message.Author.ID == m.Mentions[0].ID {
s.MessageReactionAdd(message.ChannelID, message.ID, "jinx1:1074460008307245067")
nerdedMessagesCount++
nerdedMessagesTBD--
if nerdedMessagesTBD == 0 {
break
}
}
}
}
}
if strings.ToLower(m.Content) == "opice hovno" {
s.ChannelMessageSendReply(m.ChannelID, "ZIJU TI VE ZDECH ZIJU TI VE ZDECH", m.Reference())
}
}
func initDatabase() *mongo.Client {
//MongoDB databse connection
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOptions := options.Client().
ApplyURI("mongodb+srv://monkiopicak:JB5NR5RJImwhLxtN@monkidatabse.cxodm.mongodb.net/?retryWrites=true&w=majority").
SetServerAPIOptions(serverAPIOptions)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
return client
}
func GetUserData(client mongo.Client, userName, userId string) bson.M {
collection := client.Database("farmsDb").Collection("userFarm")
var opicak bson.M
err := collection.FindOne(context.TODO(), bson.M{"userId": userId}).Decode(&opicak)
if err == nil {
} else {
log.Print(err)
collection := client.Database("farmsDb").Collection("userFarm")
_, err := collection.InsertOne(context.TODO(), bson.D{{"userId", userId}, {"userName", userName}, {"bananas", 0}, {"xp", 0}, {"hovna", 0}})
if err != nil {
log.Print(err)
}
}
return opicak
}
func GetTopUsers(client mongo.Client) []bson.M {
collection := client.Database("farmsDb").Collection("userFarm")
findOptions := options.Find()
// Sort by `price` field descending
findOptions.SetSort(bson.D{{"bananas", -1}})
findOptions.SetLimit(10)
//Does the query
documents, err := collection.Find(context.TODO(), bson.D{}, findOptions)
if err != nil {
log.Print(err)
}
//decodes the querry
var monkeys []bson.M
if err = documents.All(context.TODO(), &monkeys); err != nil {
log.Print(err)
}
if err != nil {
log.Print(err)
}
return (monkeys)
}
func addBanans(client mongo.Client, userId string, banans int) {
collection := client.Database("farmsDb").Collection("userFarm")
_, err := collection.UpdateOne(context.TODO(), bson.M{"userId": userId},
bson.D{
{Key: "$inc", Value: bson.D{{Key: "bananas", Value: banans}}},
},
)
if err != nil {
log.Print(err)
}
}
func addHovno(client mongo.Client, userId string) {
collection := client.Database("farmsDb").Collection("userFarm")
_, err := collection.UpdateOne(context.TODO(), bson.M{"userId": userId},
bson.D{
{Key: "$inc", Value: bson.D{{Key: "hovna", Value: 1}}},
},
)
if err != nil {
log.Print(err)
}
}
func subHovno(client mongo.Client, username, userId string) bool {
user := GetUserData(client, username, userId)
if user["hovna"] == nil {
addField(client, userId, "hovna", 0)
return false
} else if (int(user["hovna"].(int32))) <= 0 {
return false
}
collection := client.Database("farmsDb").Collection("userFarm")
_, err := collection.UpdateOne(context.TODO(), bson.M{"userId": userId},
bson.D{
{Key: "$inc", Value: bson.D{{Key: "hovna", Value: -1}}},
},
)
if err != nil {
log.Print(err)
}
return true
}
func addMoney(client mongo.Client, userId string, money int) {
collection := client.Database("farmsDb").Collection("userFarm")
_, err := collection.UpdateOne(context.TODO(), bson.M{"userId": userId},
bson.D{
{Key: "$inc", Value: bson.D{{Key: "money", Value: money}}},
},
)
if err != nil {
log.Print(err)
}
}
func resetBananas(client mongo.Client, userId string, bananas int) {
collection := client.Database("farmsDb").Collection("userFarm")
_, err := collection.UpdateOne(context.TODO(), bson.M{"userId": userId},
bson.D{
{Key: "$inc", Value: bson.D{{Key: "bananas", Value: -bananas}}},
},
)
if err != nil {
log.Print(err)
}
}
func addField(client mongo.Client, userId, fieldName string, value int) {
collection := client.Database("farmsDb").Collection("userFarm")
_, err := collection.UpdateOne(context.TODO(), bson.M{"userId": userId},
bson.D{
{Key: "$set", Value: bson.D{{Key: fieldName, Value: value}}},
},
)
if err != nil {
log.Print(err)
}
}