-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
135 lines (110 loc) · 3.24 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
package main
import (
"flag"
"fmt"
"github.com/5HT2/taro-bot/bot"
"github.com/5HT2/taro-bot/cmd"
"github.com/5HT2/taro-bot/plugins"
"github.com/5HT2/taro-bot/util"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/diamondburned/arikawa/v3/state"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
)
var (
pluginDir = flag.String("plugindir", "bin", "Default dir to search for plugins")
debugLog = flag.Bool("debug", false, "Debug messages and faster config saving")
)
func main() {
flag.Parse()
log.Printf("Running on Go version: %s\n", runtime.Version())
// Load configs before anything else, as it will be needed
bot.LoadConfig()
bot.LoadPluginConfig()
var token = bot.C.BotToken
if token == "" {
log.Fatalln("No bot_token given")
}
s := state.NewWithIntents("Bot "+token,
gateway.IntentGuildMessages,
gateway.IntentGuildEmojis,
gateway.IntentGuildMessageReactions,
gateway.IntentDirectMessages,
gateway.IntentGuildMembers,
)
bot.Client = *s
if s == nil {
log.Fatalln("Session failed: is nil")
}
// Add handlers
s.AddHandler(func(e *gateway.MessageCreateEvent) {
go cmd.CommandHandler(e)
go cmd.ResponseHandler(e)
})
s.AddHandler(func(e *gateway.GuildMemberUpdateEvent) {
go cmd.UpdateMemberCache(e)
})
if err := s.Open(bot.Ctx); err != nil {
log.Fatalln("Failed to connect:", err)
}
// Cancel context when SIGINT / SIGKILL / SIGTERM. SIGTERM is used by `docker stop`
ctx, cancel := signal.NotifyContext(bot.Ctx, os.Interrupt, os.Kill, syscall.SIGTERM)
defer cancel()
if err := s.Open(ctx); err != nil {
log.Println("cannot open:", err)
}
u, err := s.Me()
if err != nil {
log.Fatalln("Failed to get bot user:", err)
}
bot.User = u
http.DefaultClient = &bot.HttpClient
// We want http bash requests immediately accessible just in case something needs them.
// Though, this shouldn't really ever happen, it doesn't hurt.
util.RegisterHttpBashRequests()
// Call plugins after logging in with the bot, but before doing anything else at all
go plugins.RegisterAll(*pluginDir)
// Set up the bots status
go bot.LoadActivityStatus()
// Now we can start the routine-based tasks
go bot.SetupConfigSaving()
go bot.Scheduler.StartAsync()
log.Printf("Started as %v (%s). Debugging is set to `%v`.\n", u.ID, util.FormattedUserTag(*u), *debugLog)
go checkGuildCounts(s)
<-ctx.Done() // block until Ctrl+C / SIGINT / SIGTERM
log.Println("received signal, shutting down")
bot.SaveConfig()
bot.SavePluginConfig()
plugins.SaveConfig()
plugins.Shutdown()
if err := s.Close(); err != nil {
log.Println("cannot close:", err)
}
log.Println("closed connection")
}
func checkGuildCounts(s *state.State) {
guilds, err := s.Guilds()
if err != nil {
log.Printf("checkGuildCounts: %v\n", err)
}
fmtGuilds := make([]string, 0)
members := 0
for _, guild := range guilds {
if guildMembers, err := s.Members(guild.ID); err == nil {
numMembers := len(guildMembers)
members += numMembers
fmtGuilds = append(fmtGuilds, fmt.Sprintf("- %v - %s - (%s)", guild.ID, guild.Name, util.JoinIntAndStr(numMembers, "member")))
}
}
log.Printf(
"Currently serving %s on %s\n%s",
util.JoinIntAndStr(members, "user"),
util.JoinIntAndStr(len(guilds), "guild"),
strings.Join(fmtGuilds, "\n"),
)
}