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
/
Copy paththxUtils.go
153 lines (141 loc) · 5.87 KB
/
thxUtils.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
package main
import (
"database/sql"
"log"
"time"
"github.com/bwmarrin/discordgo"
)
type State byte
const (
wait State = iota
confirm
reject
)
func isThxMessage(messageID string) bool {
ret, err := DbMap.SelectInt("SELECT count(*) FROM Participants WHERE message_id = ?", messageID)
if err != nil {
log.Panicln("isThxMessage DbMap.SelectInt " + err.Error())
}
if ret == 1 {
return true
}
return false
}
func isThxmeMessage(messageID string) bool {
ret, err := DbMap.SelectInt("SELECT count(*) FROM ParticipantCandidates WHERE message_id = ?", messageID)
if err != nil {
log.Panicln("isThxmeMessage DbMap.SelectInt " + err.Error())
}
return ret == 1
}
func updateThxInfoMessage(messageId *string, guildId, channelId, participantId string, giveawayId int, confirmerId *string, state State) *string {
embed := discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{
URL: "https://craftserve.pl",
Name: "Giveaway info",
IconURL: "https://cdn.discordapp.com/avatars/524308413719642118/c2a17b4479bfcc89d2b7e64e6ae15ebe.webp",
},
Description: "**Ten bot organizuje giveaway kodów na serwery Diamond. Każdy kod przedłuża serwer o 7 dni.**\n" +
"Aby wziąć udział pomagaj innym użytkownikom. " +
"Jeżeli komuś pomożesz, to poproś tą osobę aby napisala `!thx @TwojNick` - w ten sposób dostaniesz się do loterii. " +
"To jest nasza metoda na rozruszanie tego Discorda, tak, aby każdy mógł liczyć na pomoc. " +
"Każde podziękowanie to jeden los, więc warto pomagać!\n\n" +
"**Pomoc musi odbywać się na tym serwerze na tekstowych kanałach publicznych.**\n\n" +
"W aktualnym giveawayu są: " + getParticipantsNamesString(giveawayId) + "\n\n" +
"Nagrody rozdajemy o " + config.GiveawayTimeString + ", Powodzenia!",
Timestamp: time.Now().Format(time.RFC3339),
}
embed.Color = 0x234d20
embed.Fields = []*discordgo.MessageEmbedField{}
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: "Dodany", Value: "<@" + participantId + ">", Inline: true})
var status string
if state == wait {
status = "Oczekiwanie"
} else if state == confirm {
if confirmerId != nil {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: "Potwierdzający", Value: "<@" + *confirmerId + ">", Inline: true})
}
status = "Potwierdzono"
} else if state == reject {
if confirmerId != nil {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: "Odrzucający", Value: "<@" + *confirmerId + ">", Inline: true})
}
status = "Odrzucono"
}
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: "Status", Value: status, Inline: true})
var message *discordgo.Message
var err error
if messageId != nil {
message, err = session.ChannelMessageEditEmbed(channelId, *messageId, &embed)
if err != nil {
log.Println("updateThxInfoMessage session.ChannelMessageEditEmbed(" + channelId + ", " + *messageId + ", embed) " + err.Error())
return nil
}
} else {
message, err = session.ChannelMessageSendEmbed(channelId, &embed)
if err != nil {
log.Println("updateThxInfoMessage session.ChannelMessageEditEmbed(" + channelId + ", nil, embed) " + err.Error())
return nil
}
}
notifyThxOnThxInfoChannel(guildId, channelId, message.ID, participantId, confirmerId, state)
return &message.ID
}
func notifyThxOnThxInfoChannel(guildId, channelId, messageId, participantId string, confirmerId *string, state State) {
embed := discordgo.MessageEmbed{
Timestamp: time.Now().Format(time.RFC3339),
Author: &discordgo.MessageEmbedAuthor{
URL: "https://craftserve.pl",
Name: "Nowe podziękowanie",
IconURL: "https://cdn.discordapp.com/avatars/524308413719642118/c2a17b4479bfcc89d2b7e64e6ae15ebe.webp",
},
Color: 0x234d20,
}
embed.Fields = []*discordgo.MessageEmbedField{}
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: "Dla", Value: "<@" + participantId + ">", Inline: true})
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: "Kanał", Value: "<#" + channelId + ">", Inline: true})
if state == wait {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: "Status", Value: "Oczekiwanie", Inline: true})
} else if state == confirm {
if confirmerId != nil {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: "Status", Value: "Potwierdzono przez <@" + *confirmerId + ">", Inline: true})
}
} else if state == reject {
if confirmerId != nil {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: "Status", Value: "Odrzucono przez <@" + *confirmerId + ">", Inline: true})
}
}
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{Name: "Link", Value: "https://discordapp.com/channels/" + guildId + "/" + channelId + "/" + messageId, Inline: false})
var serverConfig ServerConfig
err := DbMap.SelectOne(&serverConfig, "SELECT * from ServerConfig WHERE guild_id=?", guildId)
if err != nil {
log.Println("notifyThxOnThxInfoChannel Unable to read from database! ", err)
return
}
if serverConfig.ThxInfoChannel == "" {
return
}
var thxNotification ThxNotification
err = DbMap.SelectOne(&thxNotification, "SELECT * from ThxNotifications WHERE message_id=?", messageId)
if err == sql.ErrNoRows {
message, err := session.ChannelMessageSendEmbed(serverConfig.ThxInfoChannel, &embed)
if err != nil {
log.Println("notifyThxOnThxInfoChannel Unable to send thx info! ", err)
return
}
thxNotification = ThxNotification{
MessageId: messageId,
ThxNotificationMessageId: message.ID,
}
err = DbMap.Insert(&thxNotification)
if err != nil {
log.Println("notifyThxOnThxInfoChannel Unable to insert to database! ", err)
return
}
return
}
_, err = session.ChannelMessageEditEmbed(serverConfig.ThxInfoChannel, thxNotification.ThxNotificationMessageId, &embed)
if err != nil {
log.Println("notifyThxOnThxInfoChannel Unable to edit embed! ", err)
}
}