-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
77 lines (67 loc) · 1.73 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/events"
"github.com/jckli/picsiv/commands"
"github.com/jckli/picsiv/dbot"
_ "github.com/joho/godotenv/autoload"
)
func main() {
picsiv := dbot.New(os.Getenv("VERSION"))
h := commands.CommandHandlers(picsiv)
client := picsiv.Setup(
h,
bot.NewListenerFunc(picsiv.ReadyEvent),
bot.NewListenerFunc(func(e *events.MessageCreate) {
commands.OnMessageCreate(e, picsiv)
}),
)
picsiv.Client = client
cache := picsiv.InitializeCache()
picsiv.Cache = cache
ticker := time.NewTicker(time.Minute * 30)
defer ticker.Stop()
go func() {
for {
<-ticker.C
picsiv.Cache = picsiv.InitializeCache()
}
}()
var err error
if picsiv.Config.DevMode {
picsiv.Logger.Info(
fmt.Sprintf(
"Running in dev mode. Syncing commands to server ID: %s",
picsiv.Config.DevServerID,
),
)
_, err = client.Rest().
SetGuildCommands(client.ApplicationID(), picsiv.Config.DevServerID, commands.CommandList)
} else {
picsiv.Logger.Info(
"Running in global mode. Syncing commands globally.",
)
_, err = client.Rest().SetGlobalCommands(client.ApplicationID(), commands.CommandList)
}
if err != nil {
picsiv.Logger.Error(fmt.Sprintf("Failed to sync commands: %s", err.Error()))
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.OpenGateway(ctx)
if err != nil {
picsiv.Logger.Error("Error while connecting: ", err)
}
defer client.Close(context.TODO())
picsiv.Logger.Info("Bot is now running.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
picsiv.Logger.Info("Shutting down...")
}