This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
giveaways.go
284 lines (263 loc) · 8.63 KB
/
giveaways.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
package main
import (
"database/sql"
"log"
"math/rand"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
func getGiveawayForGuild(guildId string) *Giveaway {
var giveaway Giveaway
err := DbMap.SelectOne(&giveaway, "SELECT * FROM Giveaways WHERE guild_id = ? AND end_time IS NULL", guildId)
if err != nil && err != sql.ErrNoRows {
log.Panicln("getGiveawayaForGuild DbMap.SelectOne " err.Error())
}
if err == sql.ErrNoRows {
return nil
}
return &giveaway
}
func getAllUnfinishedGiveaways() []Giveaway {
var res []Giveaway
_, err := DbMap.Select(&res, "SELECT * FROM Giveaways WHERE end_time IS NULL")
if err != nil {
log.Panicln("getAllUnfinishedGiveaways DbMap.Select " err.Error())
return nil
}
return res
}
func createMissingGiveaways() {
for i := 0; i < len(session.State.Guilds); i {
// Jak się tak dziwnie nie wyciągnie gildii to nie działa
guild, _ := session.Guild(session.State.Guilds[i].ID)
for _, channel := range guild.Channels {
if channel.Name == getGiveawayChannelNameForGuild(guild.ID) {
giveaway := getGiveawayForGuild(guild.ID)
if giveaway == nil {
giveaway = &Giveaway{
StartTime: time.Now(),
GuildId: guild.ID,
GuildName: guild.Name,
}
err := DbMap.Insert(giveaway)
if err != nil {
log.Panicln("createMissingGiveaways DbMap.Insert " err.Error())
}
}
break
}
}
}
}
func getGiveawayChannelNameForGuild(guildID string) string {
var serverConfig ServerConfig
err := DbMap.SelectOne(&serverConfig, "SELECT * FROM ServerConfig")
if err != nil {
log.Println("getGiveawayChannelNameForGuild(" guildID ") " err.Error())
return ""
}
return serverConfig.MainChannel
}
func finishGiveaways() {
giveaways := getAllUnfinishedGiveaways()
for _, giveaway := range giveaways {
finishGiveaway(giveaway.GuildId)
}
createMissingGiveaways()
}
func finishGiveaway(guildID string) {
giveaway := getGiveawayForGuild(guildID)
guild, err := session.Guild(giveaway.GuildId)
if err != nil {
log.Println("Nie mogę się dobrać do gildii o ID " guildID ", pomijam.")
return
}
var giveawayChannelId string
for _, channel := range guild.Channels {
if channel.Name == getGiveawayChannelNameForGuild(guildID) {
giveawayChannelId = channel.ID
break
}
}
var participants []Participant
_, err = DbMap.Select(&participants, "SELECT * FROM Participants WHERE giveaway_id = ? AND is_accepted = true", giveaway.Id)
if err != nil {
log.Panicln("finishGiveaway DbMap.Select " err.Error())
}
if participants == nil || len(participants) == 0 {
giveaway.EndTime.Time = time.Now()
giveaway.EndTime.Valid = true
_, err := DbMap.Update(giveaway)
if err != nil {
log.Panicln("finishGiveaway DbMap.Select " err.Error())
}
notifyWinner(giveaway.GuildId, giveawayChannelId, nil, "")
return
}
code, err := getCSRVCode()
if err != nil {
log.Println("finishGiveaway getCSRVCode " err.Error())
_, _ = session.ChannelMessageSend(giveawayChannelId, "Błąd API Craftserve, nie udało się pobrać kodu!")
return
}
rand.Seed(time.Now().UnixNano())
winner := participants[rand.Int()%len(participants)]
giveaway.InfoMessageId.String = notifyWinner(giveaway.GuildId, giveawayChannelId, &winner.UserId, code)
giveaway.InfoMessageId.Valid = true
giveaway.EndTime.Time = time.Now()
giveaway.EndTime.Valid = true
giveaway.Code.String = code
giveaway.Code.Valid = true
giveaway.WinnerId.String = winner.UserId
giveaway.WinnerId.Valid = true
giveaway.WinnerName.String = winner.UserName
giveaway.WinnerName.Valid = true
_, err = DbMap.Update(giveaway)
if err != nil {
log.Panicln("finishGiveaway DbMap.Update " err.Error())
}
}
func getParticipantsNames(giveawayId int) []string {
var participants []Participant
_, err := DbMap.Select(&participants, "SELECT user_name FROM Participants WHERE giveaway_id = ? AND is_accepted = true", giveawayId)
if err != nil {
if err == sql.ErrNoRows {
return nil
}
log.Panicln("getParticipantsNames DbMap.Select " err.Error())
}
names := make([]string, len(participants))
for i := range participants {
names[i] = participants[i].UserName
}
return names
}
func getParticipantByMessageId(messageId string) *Participant {
var participant Participant
err := DbMap.SelectOne(&participant, "SELECT * FROM Participants WHERE message_id = ?", messageId)
if err != nil && err != sql.ErrNoRows {
log.Panicln("getParticipantByMessageId DbMap.SelectOne " err.Error())
}
if err == sql.ErrNoRows {
return nil
}
return &participant
}
func getParticipantsByGiveawayId(giveawayId int) []Participant {
var participants []Participant
_, err := DbMap.Select(&participants, "SELECT * FROM Participants WHERE giveaway_id = ? AND is_accepted = true", giveawayId)
if err != nil {
if err == sql.ErrNoRows {
return nil
}
log.Panicln("getParticipantsByGiveawayId DbMap.Select " err.Error())
}
return participants
}
func getParticipantsNamesString(giveawayId int) string {
participants := getParticipantsNames(giveawayId)
if participants == nil {
return ""
}
return strings.Join(participants, ", ")
}
func getParticipantCandidateByMessageId(messageId string) *ParticipantCandidate {
var candidate ParticipantCandidate
err := DbMap.SelectOne(&candidate, "SELECT * From ParticipantCandidates WHERE message_id = ?", messageId)
if err != nil {
if err == sql.ErrNoRows {
return nil
}
log.Panicln("getParticipantCandidateByMessageId DbMap.Select " err.Error())
}
return &candidate
}
func notifyWinner(guildID, channelID string, winnerID *string, code string) string {
guild, err := session.Guild(guildID)
var guildName string
if err != nil {
log.Println("notifyWinner session.Guild(" guildID ") " err.Error())
guildName = guildID
} else {
guildName = guild.Name
}
if winnerID == nil {
log.Println("Giveaway na " guildName " zakończył się bez uczestników.")
message, _ := session.ChannelMessageSend(channelID, "Dzisiaj nikt nie wygrywa, ponieważ nikt nie pomagał ;(")
return message.ID
}
winner, err := session.GuildMember(guildID, *winnerID)
if err != nil {
log.Println("notifyWinner session.GuildMember(" guildID ", " *winnerID ") " err.Error())
return ""
}
log.Println(winner.User.Username " wygrał giveaway na " guild.Name ". Kod: " code)
embed := discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{
URL: "https://craftserve.pl",
Name: "Wygrałeś kod na serwer diamond!",
IconURL: "https://cdn.discordapp.com/avatars/524308413719642118/c2a17b4479bfcc89d2b7e64e6ae15ebe.webp",
},
Description: "Gratulacje! W loterii wygrałeś darmowy kod na serwer w CraftServe!",
}
embed.Fields = []*discordgo.MessageEmbedField{}
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: "KOD:", Value: code})
dm, _ := session.UserChannelCreate(*winnerID)
_, _ = session.ChannelMessageSendEmbed(dm.ID, &embed)
embed = discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{
URL: "https://craftserve.pl",
Name: "Wyniki giveaway!",
IconURL: "https://cdn.discordapp.com/avatars/524308413719642118/c2a17b4479bfcc89d2b7e64e6ae15ebe.webp",
},
Description: winner.User.Username " wygrał kod. Moje gratulacje ;)",
}
message, _ := session.ChannelMessageSendEmbed(channelID, &embed)
return message.ID
}
func deleteFromGiveaway(guildID, userID string) {
giveawayId := getGiveawayForGuild(guildID).Id
participants := getParticipantsByGiveawayId(giveawayId)
for _, participant := range participants {
if participant.UserId == userID {
participant.IsAccepted.Valid = true
participant.IsAccepted.Bool = false
_, err := DbMap.Update(&participant)
if err != nil {
log.Panicln(err)
}
}
}
for _, participant := range participants {
updateThxInfoMessage(&participant.MessageId, "", participant.ChannelId, participant.UserId, participant.GiveawayId, nil, reject)
}
return
}
func blacklistUser(guildID, userID, blacklisterID string) error {
blacklist := &Blacklist{GuildId: guildID,
UserId: userID,
BlacklisterId: blacklisterID}
err := DbMap.Insert(blacklist)
if err != nil {
log.Panicln("blacklistUser DbMap.Isert(blacklist)" err.Error())
}
return err
}
func unblacklistUser(guildID, userID string) error {
_, err := DbMap.Exec("DELETE FROM Blacklists WHERE guild_id = ? AND user_id = ?", guildID, userID)
if err != nil {
log.Panicln("unblacklistUser DbMap.Exec" err.Error())
}
return err
}
func isBlacklisted(guildID, userID string) bool {
ret, err := DbMap.SelectInt("SELECT count(*) FROM Blacklists WHERE guild_id = ? AND user_id = ?", guildID, userID)
if err != nil {
log.Panicln("isBlacklisted DbMap.SelectInt " err.Error())
}
if ret == 1 {
return true
}
return false
}