This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
bot.go
136 lines (109 loc) · 3.16 KB
/
bot.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
package aternos_discord_bot
import (
"fmt"
"github.com/bwmarrin/discordgo"
aternos "github.com/sleeyax/aternos-api"
"github.com/sleeyax/aternos-discord-bot/worker"
"log"
"net/http"
"time"
)
func (ab *Bot) setupHandlers() {
ab.discord.AddHandler(ab.handleCommands)
ab.discord.AddHandler(ab.handleJoinServer)
ab.discord.AddHandler(ab.handleLeaveServer)
}
func (ab *Bot) handleJoinServer(s *discordgo.Session, e *discordgo.GuildCreate) {
// The GuildCreate event also fires after the bot has been restarted, so we have to check whether we joined recently or not.
if time.Now().Sub(e.JoinedAt).Minutes() <= 2 {
log.Printf("Joined new server %s (ID: %s)\n", e.Name, e.ID)
}
}
func (ab *Bot) handleLeaveServer(s *discordgo.Session, e *discordgo.GuildDelete) {
log.Printf("Left server %s (ID: %s)\n", e.BeforeDelete.Name, e.BeforeDelete.ID)
ab.Database.DeleteServerSettings(e.BeforeDelete.ID)
}
func (ab *Bot) Start() error {
if err := ab.Database.Connect(); err != nil {
return fmt.Errorf("failed to connect to database: %e", err)
}
ab.workers = make(map[string]*worker.Worker)
session, err := discordgo.New("Bot " + ab.DiscordToken)
if err != nil {
return err
}
ab.discord = session
ab.setupHandlers()
if err = ab.discord.Open(); err != nil {
return err
}
return ab.registerCommands()
}
func (ab *Bot) Stop() error {
if err := ab.removeCommands(); err != nil {
return err
}
if err := ab.Database.Disconnect(); err != nil {
return fmt.Errorf("failed to disconnect database: %e", err)
}
return ab.discord.Close()
}
// registerCommands registers all available Discord commands.
func (ab *Bot) registerCommands() error {
var err error
ab.registeredCommands, err = ab.discord.ApplicationCommandBulkOverwrite(ab.discord.State.User.ID, "", commands)
return err
}
// removeCommands removes all Discord commands that were previously registered using registerCommands.
func (ab *Bot) removeCommands() error {
for _, v := range ab.registeredCommands {
if err := ab.discord.ApplicationCommandDelete(ab.discord.State.User.ID, "", v.ID); err != nil {
return err
}
}
ab.registeredCommands = nil
return nil
}
// getWorker returns the active worker for the specified guildId from the pool or creates a new one if it doesn't exist yet.
func (ab *Bot) getWorker(guildId string) (*worker.Worker, error) {
w, ok := ab.workers[guildId]
opts, err := ab.createOptions(guildId)
if err != nil {
return nil, err
}
if !ok {
w = worker.New(guildId, opts)
ab.workers[guildId] = w
} else {
w.Reconfigure(opts)
}
return w, nil
}
// createOptions creates new aternos configuration options.
func (ab *Bot) createOptions(guildId string) (*aternos.Options, error) {
options := &aternos.Options{
Cookies: []*http.Cookie{
{
Name: "ATERNOS_LANGUAGE",
Value: "en",
},
},
Proxy: ab.Proxy,
InsecureSkipVerify: true,
}
settings, err := ab.Database.ReadServerSettings(guildId)
if err != nil {
return nil, err
}
options.Cookies = append(options.Cookies,
&http.Cookie{
Name: "ATERNOS_SESSION",
Value: settings.SessionCookie,
},
&http.Cookie{
Name: "ATERNOS_SERVER",
Value: settings.ServerCookie,
},
)
return options, nil
}