-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.go
83 lines (69 loc) · 2.59 KB
/
app.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
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/gompus/snowflake"
"github.com/lukasl-dev/octave/command/pause"
"github.com/lukasl-dev/octave/command/play"
"github.com/lukasl-dev/octave/command/resume"
"github.com/lukasl-dev/octave/command/seek"
"github.com/lukasl-dev/octave/command/stop"
"github.com/lukasl-dev/octave/command/volume"
"github.com/lukasl-dev/octave/config"
"github.com/lukasl-dev/waterlink/v2"
)
// app is the main application struct. It holds all necessary dependencies to
// run the bot.
type app struct {
// cfg is the application configuration.
cfg config.Config
// session is the discord session on which the bot is running on.
session *discordgo.Session
// conn is the active lavalink connection.
conn *waterlink.Connection
// client is the lavalink client.
client *waterlink.Client
// cmds is the commandHandler that is responsible for handling commands.
cmds *commandHandler
// sessionID is the current session's ID which is received on discordgo.Ready.
sessionID string
}
// newApp returns a new app configured by the given config.Config.
func newApp(cfg config.Config) *app {
return &app{cfg: cfg, cmds: newCommandHandler()}
}
func (a *app) run() (err error) {
a.session, err = discordgo.New(fmt.Sprintf("Bot %s", a.cfg.Token))
if err != nil {
return fmt.Errorf("failed to create discord session: %w", err)
}
a.registerHandlers()
return a.session.Open()
}
// createClient tries to create a new waterlink.Client and defines it in the app.
func (a *app) createClient() (err error) {
a.client, err = waterlink.NewClient(fmt.Sprintf("http://%s", a.cfg.Lavalink.Host), a.credentials())
return err
}
// createConnection tries to create a new waterlink.Connection and defines it in
// the app.
func (a *app) createConnection() (err error) {
a.conn, err = waterlink.Open(fmt.Sprintf("ws://%s", a.cfg.Lavalink.Host), a.credentials())
return err
}
// credentials returns the waterlink.Credentials to use for client and connection.
func (a *app) credentials() waterlink.Credentials {
return waterlink.Credentials{
Authorization: a.cfg.Lavalink.Passphrase,
UserID: snowflake.MustParse(a.session.State.User.ID),
}
}
// registerCommands registers all commands in the internal commandHandler.
func (a *app) registerCommands() {
a.cmds.add(pause.Pause(pause.Deps{Conn: a.conn}))
a.cmds.add(play.Play(play.Deps{Client: a.client, Conn: a.conn}))
a.cmds.add(resume.Resume(resume.Deps{Conn: a.conn}))
a.cmds.add(seek.Seek(seek.Deps{Conn: a.conn}))
a.cmds.add(stop.Stop(stop.Deps{Conn: a.conn}))
a.cmds.add(volume.Volume(volume.Deps{Conn: a.conn}))
}