This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcore.go
171 lines (152 loc) · 4.6 KB
/
core.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"
"regexp"
"runtime"
"strconv"
"syscall"
"time"
config "maquiaBot/config"
handlers "maquiaBot/handlers"
gencommands "maquiaBot/handlers/general-commands"
osucommands "maquiaBot/handlers/osu-commands"
osuapi "maquiaBot/osu-api"
osutools "maquiaBot/osu-tools"
structs "maquiaBot/structs"
"github.com/bwmarrin/discordgo"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
func main() {
// Create data folders and stuff
if _, err := os.Stat("./data"); os.IsNotExist(err) {
err = os.MkdirAll("./data", 0755)
readErr(err)
log.Println("Created data directory.")
err = ioutil.WriteFile("./data/genitalRecords.json", []byte{}, 0644)
readErr(err)
log.Println("Created data/genitalRecords.json.")
err = ioutil.WriteFile("./data/reminders.json", []byte{}, 0644)
readErr(err)
log.Println("Created data/reminders.json.")
err = ioutil.WriteFile("./data/tasks.json", []byte{}, 0644)
readErr(err)
log.Println("Created data/tasks.json.")
err = os.MkdirAll("./data/channelData", 0755)
readErr(err)
log.Println("Created data/channelData directory.")
err = os.MkdirAll("./data/channelData", 0755)
readErr(err)
log.Println("Created data/channelData directory.")
err = os.MkdirAll("./data/osuData", 0755)
readErr(err)
log.Println("Created data/osuData directory.")
err = ioutil.WriteFile("./data/osuData/mapFarm.json", []byte{}, 0644)
readErr(err)
log.Println("Created data/osuData/mapFarm.json.")
err = ioutil.WriteFile("./data/osuData/profileCache.json", []byte{}, 0644)
readErr(err)
log.Println("Created data/osuData/profileCache.json.")
}
// Obtain config
config.NewConfig()
osuAPI := osuapi.NewClient(config.Conf.OsuToken)
osucommands.OsuAPI = osuAPI
osutools.OsuAPI = osuAPI
discord, err := discordgo.New("Bot " + config.Conf.DiscordToken)
readErr(err)
// Handle farm data
go osutools.FarmUpdate(discord)
// Add handlers
discord.AddHandler(handlers.MessageHandler)
discord.AddHandler(handlers.ReactAdd)
discord.AddHandler(handlers.ServerJoin)
discord.AddHandler(handlers.ServerLeave)
// Open a websocket connection to Discord and begin listening
for {
err = discord.Open()
if err == nil {
break
}
}
log.Println("Bot is now running in " + strconv.Itoa(len(discord.State.Guilds)) + " servers.")
discord.UpdateStatus(0, "maquiahelp | maquiaprefix | "+strconv.Itoa(len(discord.State.Guilds))+" servers")
// Resume all reminder timers
reminders := []structs.Reminder{}
_, err = os.Stat("./data/reminders.json")
if err == nil {
f, err := ioutil.ReadFile("./data/reminders.json")
readErr(err)
_ = json.Unmarshal(f, &reminders)
} else {
readErr(err)
}
reminderTimers := []structs.ReminderTimer{}
for _, reminder := range reminders {
reminderTimer := structs.ReminderTimer{
Reminder: reminder,
Timer: *time.NewTimer(reminder.Target.Sub(time.Now().UTC())),
}
reminderTimers = append(reminderTimers, reminderTimer)
go gencommands.RunReminder(discord, reminderTimer)
}
gencommands.ReminderTimers = reminderTimers
// Resume all tasks
tasks := []structs.Task{}
_, err = os.Stat("./data/tasks.json")
if err == nil {
f, err := ioutil.ReadFile("./data/tasks.json")
readErr(err)
_ = json.Unmarshal(f, &tasks)
} else {
readErr(err)
}
taskCache := []structs.TaskCache{}
for _, task := range tasks {
activeTask := structs.TaskCache{
Task: task,
Active: true,
}
taskCache = append(taskCache, activeTask)
go gencommands.RunTask(discord, task)
}
gencommands.TaskCache = taskCache
// Get osu! tracking data for channels
var channels []string
err = filepath.Walk("./data/channelData", func(path string, info os.FileInfo, err error) error {
readErr(err)
channels = append(channels, path)
return nil
})
readErr(err)
IDregex, _ := regexp.Compile(`(?i)(\d+)\.json`)
for _, channel := range channels {
if IDregex.MatchString(channel) {
chID := IDregex.FindStringSubmatch(channel)[1]
ch, err := discord.Channel(chID)
if err == nil {
go osutools.TrackPost(*ch, discord)
}
}
}
// Open DB
// tools.DB, err = gorm.Open("mysql", config.Conf.Database.Username+":"+config.Conf.Database.Password+"@/"+config.Conf.Database.Name)
// readErr(err)
// Create a channel to keep the bot running until a prompt is given to close
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Kill)
<-sc
// Close sessions
discord.Close()
// tools.DB.Close()
}
func readErr(err error) {
if err != nil {
pc, fn, line, _ := runtime.Caller(1)
log.Fatalf("[error] in %s[%s:%d] %v\n", runtime.FuncForPC(pc).Name(), fn, line, err)
}
}