This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.go
240 lines (199 loc) · 5.37 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
package main
import (
"fmt"
"github.com/TechReborn/DiscordBot/file"
"github.com/bwmarrin/discordgo"
"log"
"strings"
"time"
)
var (
Token string //The token of the bot user
BotID string //The id of the bot
DiscordClient *discordgo.Session //The discord client
)
func main() {
err := populateInitialJiraVersions()
if err != nil {
fmt.Println("Failed to load jira versions")
panic(err)
}
err = populateInitialGameVersions()
if err != nil {
fmt.Println("Failed to load jira versions")
panic(err)
}
ticker := time.NewTicker(time.Second * 30)
go func() {
for range ticker.C {
updateCheck()
}
}()
err = LoadDiscord()
if err != nil {
fmt.Println("Failed to load discord")
panic(err)
}
}
func updateCheck() {
go jiraUpdateCheck(postJiraMessage)
go gameUpdateCheck(postGameMessage)
}
func postGameMessage(message string) error {
fmt.Println(message)
lines, err := file.ReadLines("channels.txt")
if err != nil {
return err
}
for _, element := range lines {
DiscordClient.ChannelMessageSend(element, message)
}
return nil
}
func postJiraMessage(message string) error {
fmt.Println(message)
lines, err := file.ReadLines("jira_channels.txt")
if err != nil {
return err
}
for _, element := range lines {
DiscordClient.ChannelMessageSend(element, message)
}
return nil
}
//LoadDiscord is based a lot off https://github.com/bwmarrin/discordgo/blob/master/examples/pingpong/main.go
func LoadDiscord() error {
t, err := getToken()
if err != nil {
return err
}
Token = t
dg, err := discordgo.New("Bot " + Token)
if err != nil {
return err
}
DiscordClient = dg
u, err := dg.User("@me")
if err != nil {
return err
}
BotID = u.ID
dg.AddHandler(handleMessage)
err = dg.Open()
if err != nil {
return err
}
fmt.Println("Bot is now running. Press CTRL-C to exit.")
<-make(chan struct{})
return nil
}
//Called when a message is posted
func handleMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
channel, _ := DiscordClient.Channel(m.ChannelID)
channelName := channel.Name
if channel.Type == discordgo.ChannelTypeDM || channel.Type == discordgo.ChannelTypeGroupDM {
channelName = m.Author.Username
}
fmt.Println("#" + channelName + " <" + m.Author.Username + ">:" + m.Content)
if m.Author.ID == BotID {
return
}
if m.Content == "!gameNotify" {
if isAuthorAdmin(m.Author) {
err := file.AppendString(m.ChannelID, "channels.txt")
if err == nil {
s.ChannelMessageSend(m.ChannelID, "The bot will now announce new minecraft versions here!")
} else {
log.Println("Failed to write game channels", err)
s.ChannelMessageSend(m.ChannelID, "An error occurred, contact bot owner")
}
}
}
if m.Content == "!jiraNotify" {
if isAuthorAdmin(m.Author) {
err := file.AppendString(m.ChannelID, "jira_channels.txt")
if err == nil {
s.ChannelMessageSend(m.ChannelID, "The bot will now announce new jira versions here!")
} else {
log.Println("Failed to write jira channels", err)
s.ChannelMessageSend(m.ChannelID, "An error occurred, contact bot owner")
}
}
}
if file.Exists("discord_muted.txt") {
lines, err := file.ReadLines("discord_muted.txt")
if err == nil {
for _, str := range lines {
if str == m.GuildID {
return
}
}
}
}
if m.Content == "!mute" {
if !isAuthorAdmin(m.Author) {
s.ChannelMessageSend(m.ChannelID, "You do not have permission to run that command.")
return
}
err := file.AppendString(m.GuildID, "discord_muted.txt")
if err == nil {
s.ChannelMessageSend(m.ChannelID, "The bot will no longer response to commands in this server!")
} else {
s.ChannelMessageSend(m.ChannelID, "An error occurred, contact bot owner")
}
}
if m.Content == "!commands" || m.Content == "!help" {
cmdList := ""
lines, err := file.ReadLines("commands.txt")
if err == nil {
for _, element := range lines {
command := "!" + strings.Split(element, "=")[0]
cmdList = cmdList + "`" + command + "` "
}
}
if isAuthorAdmin(m.Author) {
cmdList = cmdList + "`!addCom` "
cmdList = cmdList + "`!verNotify` "
}
s.ChannelMessageSend(m.ChannelID, "The following commands are available for you to use. "+cmdList)
}
if m.Content == "!myID" {
s.ChannelMessageSend(m.ChannelID, "You ID: `"+m.Author.ID+"`")
}
if strings.HasPrefix(m.Content, "!addCom") {
if !isAuthorAdmin(m.Author) {
s.ChannelMessageSend(m.ChannelID, "You do not have permission to run that command.")
return
}
text := strings.Replace(m.Content, "!addCom ", "", -1)
textLine := strings.Replace(text, " ", "=", 1)
err := file.AppendString(textLine, "commands.txt")
if err == nil {
s.ChannelMessageSend(m.ChannelID, "The command has been added!")
} else {
s.ChannelMessageSend(m.ChannelID, "An error occurred, contact bot owner")
}
}
if file.Exists("commands.txt") {
lines, err := file.ReadLines("commands.txt")
if err == nil {
for _, element := range lines {
command := "!" + strings.Split(element, "=")[0]
reply := strings.Split(element, "=")[1]
if m.Content == command {
s.ChannelMessageSend(m.ChannelID, reply)
}
}
}
}
}
func isAuthorAdmin(user *discordgo.User) bool {
if user.ID != "98473211301212160" { //TODO have a file or some better way to do this.
return false
}
return true
}
//Loads the token from the file
func getToken() (string, error) {
return file.ReadString("token.txt")
}