-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.go
200 lines (151 loc) · 4.99 KB
/
discord.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
package main
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
)
var (
token = "your-token-here"
challenges = NewChallenges()
statistics = NewStatistics()
)
func ConnectToDiscord() {
dg, err := discordgo.New("Bot " + token)
if err != nil {
panic(err)
}
dg.AddHandler(messageCreate)
dg.Identify.Intents = discordgo.IntentsGuildMessages
err = dg.Open()
if err != nil {
panic(err)
}
fmt.Println("Bot is now running")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// ignore messages from bot himself
if m.Author.ID == s.State.User.ID {
return
}
// !challengeBot - bot will always accept challenges
if m.Content == "!challengeBot" {
handleChallengeBot(s, m)
return
}
// !challenge <username>
if strings.HasPrefix(m.Content, "!challenge") && len(strings.Split(m.Content, " ")) == 2 {
handleChallenge(s, m)
return
}
// !accepct <username>
if strings.HasPrefix(m.Content, "!accept") && len(strings.Split(m.Content, " ")) == 2 {
handleAcceptChallenge(s, m)
return
}
// !challenges
if m.Content == "!challenges" {
handleChallenges(s, m)
return
}
// !leaderboard
if m.Content == "!leaderboard" {
handleLeaderboard(s, m)
return
}
// !help
if m.Content == "!help" {
handleHelp(s, m)
return
}
// help as default
handleHelp(s, m)
}
func handleChallengeBot(s *discordgo.Session, m *discordgo.MessageCreate) {
f1 := NewFighter("The Battle Bot")
f2 := NewFighter(m.Author.Username)
result := Fight(f1, f2)
statistics.AddStatistic(result.Winner.Username, result.Loser.Username)
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s has beaten %s\n", result.Winner.Username, result.Loser.Username))
}
func handleChallenge(s *discordgo.Session, m *discordgo.MessageCreate) {
splits := strings.Split(m.Content, " ")
if len(splits) != 2 {
s.ChannelMessageSend(m.ChannelID, "wrong usage of !challenge. Use !challenge <username> to challenge the user with <username>")
return
}
user1 := m.Author.Username
user2 := splits[1]
exists := challenges.ChallengeExists(user1, user2)
if exists {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("a challenge between you and %s already exists\nYou can accept the challenge using !accept %s", user2, user2))
return
}
err := challenges.AddChallengers(user1, user2)
if err != nil {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("an error occured: %s", err.Error()))
return
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("you have challenged %s to an epic fight. He has to accecpt now", user2))
}
func handleAcceptChallenge(s *discordgo.Session, m *discordgo.MessageCreate) {
splits := strings.Split(m.Content, " ")
if len(splits) != 2 {
s.ChannelMessageSend(m.ChannelID, "wrong usage of !accept. Use !accept <username> to accept a challenge from the user with <username>")
return
}
user1 := m.Author.Username
user2 := splits[1]
exists := challenges.ChallengeExists(user1, user2)
if !exists {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("no challenge between you and %s exists\nYou can challenge start a challenge with !challenge %s", user2, user2))
return
}
f1 := NewFighter(user1)
f2 := NewFighter(user2)
result := Fight(f1, f2)
statistics.AddStatistic(result.Winner.Username, result.Loser.Username)
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s has beaten %s\n", result.Winner.Username, result.Loser.Username))
err := challenges.RemoveChallengers(user1, user2)
if err != nil {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("an error occured: %s", err.Error()))
return
}
}
func handleChallenges(s *discordgo.Session, m *discordgo.MessageCreate) {
user := m.Author.Username
challengers := challenges.OpenChallengers(user)
if len(challengers) == 0 {
s.ChannelMessageSend(m.ChannelID, "you have no open challenges")
return
}
msg := "Your open challenges are:\n"
for _, challenger := range challengers {
msg = fmt.Sprintf("%s\t%s\n", msg, challenger)
}
s.ChannelMessageSend(m.ChannelID, msg)
}
func handleLeaderboard(s *discordgo.Session, m *discordgo.MessageCreate) {
stats := statistics.GetStatistics()
msg := "Leaderboard:\n"
for idx, stat := range stats {
msg = fmt.Sprintf("%s%d. Place - %s\n", msg, idx+1, stat.Username)
}
s.ChannelMessageSend(m.ChannelID, msg)
}
func handleHelp(s *discordgo.Session, m *discordgo.MessageCreate) {
msg := "commands:\n"
msg = fmt.Sprintf("%s\t%s\n", msg, "!help - shows all available commands")
msg = fmt.Sprintf("%s\t%s\n", msg, "!challengeBot - bot will always accept challenges")
msg = fmt.Sprintf("%s\t%s\n", msg, "!challenges - shows all your open challenges")
msg = fmt.Sprintf("%s\t%s\n", msg, "!challenge <username> - challenge another user")
msg = fmt.Sprintf("%s\t%s\n", msg, "!accepct <username> - accept a challenge from another user")
msg = fmt.Sprintf("%s\t%s\n", msg, "!leaderboard - shows the leaderboard")
s.ChannelMessageSend(m.ChannelID, msg)
}