-
Notifications
You must be signed in to change notification settings - Fork 1
/
letter_counter.go
230 lines (197 loc) · 4.75 KB
/
letter_counter.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/signal"
"syscall"
"time"
"unicode/utf8"
"github.com/bwmarrin/discordgo"
"github.com/albandewilde/letter_counter/discord_helpers"
"github.com/albandewilde/letter_counter/score"
)
type Score struct {
USER string // username#discriminator
CHAR int
CHAR_LVL string
MSG int
MSG_LVL string
RATIO float64
RANK int
}
var discordToken string
func init() {
// Read the discord token
var err error
discordToken, err = readDiscordToken()
if err != nil {
displayError(err)
}
}
func main() {
// Create a new Discord session using the provided bot token.
bot, err := discordgo.New("Bot " + discordToken)
if err != nil {
fmt.Println("Error while creating the Discord session,", err)
return
}
// Register funcs callback.
bot.AddHandler(counting)
bot.AddHandler(getScore)
// Open a websocket connection to Discord and begin listening.
err = bot.Open()
if err != nil {
fmt.Println("Error while opening connection,", err)
return
}
// Wait here until CTRL-C or other term signal is received.
fmt.Println("I'm logged in ! (Press CTRL-C to exit.)\n")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session.
bot.Close()
}
func readDiscordToken() (token string, err error) {
// Read token in the `secrets.json` file
secretFile, err := ioutil.ReadFile("./secrets.json")
if err != nil {
return
}
type Secrets struct {
DISCORD string
}
var secrets Secrets
// Parse json content
err = json.Unmarshal(secretFile, &secrets)
if err != nil {
return
}
token = secrets.DISCORD
return
}
func getScore(s *discordgo.Session, m *discordgo.MessageCreate) {
// To know his score, the message content must be `§score`
if m.Content != "§score" {
return
}
var channel string = safeGetChannelName(s, m)
var guild string = safeGetGuildName(s, m)
fmt.Print(
"\033[36m",
time.Now().Format("[2006-01-02 15:04:05]"),
"\033[0m",
": ",
"The user ",
"\033[35m",
m.Author.Username+"#"+m.Author.Discriminator,
"\033[0m",
" want to know his score in the channel ",
"\033[32m",
channel,
"\033[0m",
" on the server ",
"\033[32m",
guild,
"\033[0m",
".\n",
)
var score Score
var err error
score, err = calculateScoreOfUser(m.Author)
var response string
if err != nil {
displayError(err)
response = "Sorry, something is wrong."
} else {
response = "```txt\n" +
fmt.Sprintf("User: %s\n\n", score.USER) +
fmt.Sprintf("Written characters: %d\nCaracter level: %s\n\n", score.CHAR, score.CHAR_LVL) +
fmt.Sprintf("Messages sent: %d\nMessage level: %s\n\n", score.MSG, score.MSG_LVL) +
fmt.Sprintf("Ratio (written caracters/messages sent): %.2f\n\n", score.RATIO) +
fmt.Sprintf("Rank: #%d\n\n", score.RANK) +
"```"
}
s.ChannelMessageSend(m.ChannelID, response)
}
func counting(s *discordgo.Session, m *discordgo.MessageCreate) {
messageLength := utf8.RuneCountInString(m.Content)
var channel string = safeGetChannelName(s, m)
var guild string = safeGetGuildName(s, m)
fmt.Print(
"\033[36m",
time.Now().Format("[2006-01-02 15:04:05]"),
"\033[0m",
": ",
"The user ",
"\033[35m",
m.Author.Username+"#"+m.Author.Discriminator,
"\033[0m",
" write a message with ",
"\033[33m",
messageLength,
"\033[0m",
" characters in the channel ",
"\033[32m",
channel,
"\033[0m",
" on the server ",
"\033[32m",
guild,
"\033[0m",
".\n",
)
err := score.SaveMessageScore(m.Author, messageLength)
if err != nil {
displayError(err)
}
}
func calculateScoreOfUser(username *discordgo.User) (Score, error) {
// Read user characters and mesages send
var userScore map[string]int
var err error
userScore, err = score.ReadUserScore(username)
if err != nil {
return Score{}, err
}
// Calculate the user rank
rank, err := score.UserRank(discord_helpers.DiscordUserCompleteName(username))
if err != nil {
rank = 0
displayError(err)
}
return Score{
USER: discord_helpers.DiscordUserCompleteName(username),
CHAR: userScore["char"],
CHAR_LVL: score.CalculateLevel(userScore["char"]),
MSG: userScore["msg"],
MSG_LVL: score.CalculateLevel(userScore["msg"]),
RATIO: float64(userScore["char"]) / float64(userScore["msg"]),
RANK: rank,
}, nil
}
func displayError(err error) {
fmt.Print(
"\033[31m",
"Error: ",
"\033[0m",
err,
"\n",
)
}
func safeGetChannelName(s *discordgo.Session, m *discordgo.MessageCreate) string {
channel, err := s.Channel(m.ChannelID)
if err != nil {
return "UNKNOW"
}
return channel.Name
}
func safeGetGuildName(s *discordgo.Session, m *discordgo.MessageCreate) string {
guild, err := s.Guild(m.GuildID)
if err != nil {
return "UNKNOW"
}
return guild.Name
}