-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathguild.go
154 lines (135 loc) · 4.68 KB
/
guild.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
package waterlink
import (
"fmt"
"github.com/gompus/snowflake"
"github.com/lukasl-dev/waterlink/v2/filter"
"github.com/lukasl-dev/waterlink/v2/internal/message"
"github.com/lukasl-dev/waterlink/v2/internal/message/opcode"
"github.com/lukasl-dev/waterlink/v2/internal/pkgerror"
"github.com/lukasl-dev/waterlink/v2/track"
"time"
)
// Guild is a struct that is used to send guild-scoped messages via the
// Connection.
type Guild struct {
// conn is the connection to write the message payloads to.
conn *Connection
// id is the id of the guild to which this scope belongs to.
id snowflake.Snowflake
}
// Destroy destroys the server-side audio player instance.
func (g Guild) Destroy() error {
return g.wrapErr("destroy", g.writeJSON(message.Destroy{
Outgoing: message.Outgoing{Op: opcode.Destroy},
Guild: message.Guild{GuildID: g.id.String()},
}))
}
// UpdateVoice provides an intercepted voice server update event to the server.
// This causes the server to connect to a voice channel.
func (g Guild) UpdateVoice(session string, token, endpoint string) error {
switch {
case session == "":
return g.newErr("update voice", "session must be present (not empty)")
case token == "":
return g.newErr("update voice", "token must be present (not empty)")
case endpoint == "":
return g.newErr("update voice", "endpoint must be present (not empty)")
}
return g.wrapErr("voice update", g.writeJSON(message.VoiceUpdate{
Outgoing: message.Outgoing{Op: opcode.VoiceUpdate},
Guild: g.guild(),
Session: session,
Event: message.VoiceUpdateEvent{
GuildID: g.id.String(),
Token: token,
Endpoint: endpoint,
},
}))
}
// Play plays the preloaded audio track whose id is given via the guild's audio
// player. If no params should be given, the defaultPlayParams are used.
func (g Guild) Play(trackID string, params ...PlayParams) error {
switch {
case trackID == "":
return g.newErr("play", "trackID must not be empty")
case len(params) == 0:
params = []PlayParams{defaultPlayParams}
case len(params) > 1:
return g.newErr("play", "too many params")
}
p := params[0]
return g.wrapErr("play", g.writeJSON(message.Play{
Outgoing: message.Outgoing{Op: opcode.Play},
Guild: g.guild(),
Track: trackID,
StartTime: p.startTime(),
EndTime: p.endTime(),
Volume: p.volume(),
NoReplace: p.Pause,
Pause: p.Pause,
}))
}
// PlayTrack plays the given audio track. If no params should be given, the
// defaultPlayParams are used.
func (g Guild) PlayTrack(tr track.Track, params ...PlayParams) error {
return g.Play(tr.ID, params...)
}
// Stop stops the audio playback of the guild's audio player.
func (g Guild) Stop() error {
return g.wrapErr("stop", g.writeJSON(message.Stop{
Outgoing: message.Outgoing{Op: opcode.Stop},
Guild: g.guild(),
}))
}
// SetPaused pauses or resumes the audio playback of the guild's audio player.
func (g Guild) SetPaused(paused bool) error {
return g.wrapErr("set paused", g.writeJSON(message.Pause{
Outgoing: message.Outgoing{Op: opcode.Pause},
Guild: g.guild(),
Pause: paused,
}))
}
// Seek seeks the current playing audio track to a specific position.
func (g Guild) Seek(position time.Duration) error {
return g.wrapErr("seek", g.writeJSON(message.Seek{
Outgoing: message.Outgoing{Op: opcode.Seek},
Guild: g.guild(),
Position: uint(position.Milliseconds()),
}))
}
// UpdateVolume updates the volume of the guild's audio player. The value must be
// between 0 and 1000. Defaults to 100.
func (g Guild) UpdateVolume(volume uint16) error {
if volume > 1000 {
return g.newErr("update volume", "volume must be between 0 and 1000")
}
return g.wrapErr("update volume", g.writeJSON(message.Volume{
Outgoing: message.Outgoing{Op: opcode.Volume},
Guild: g.guild(),
Volume: volume,
}))
}
// Filters sets the filter of the current playback.
func (g Guild) Filters(filters filter.Filters) error {
return g.wrapErr("filters", g.writeJSON(message.Filters{
Outgoing: message.Outgoing{Op: opcode.Filters},
Guild: g.guild(),
Filters: filters,
}))
}
// writeJSOn writes v as JSON into g's underlying connection.
func (g Guild) writeJSON(v interface{}) error {
return g.conn.conn.WriteJSON(v)
}
// newErr creates a new error with action as prefix.
func (g Guild) newErr(action, msg string) error {
return pkgerror.New(fmt.Sprintf("connection: guild %q: %s: %s", action, g.id, msg))
}
// wrapErr wraps err and adds action as prefix to the error message.
func (g Guild) wrapErr(action string, err error) error {
return pkgerror.Wrap(fmt.Sprintf("connection: guild %q: %s:", action, g.id), err)
}
// guild creates a message.Guild from g.
func (g Guild) guild() message.Guild {
return message.Guild{GuildID: g.id.String()}
}