mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-10-02 07:18:49 +00:00
session: Introduce SendGateway (#356)
* Support SendGateway to help with voice usage * Remove unnecessary Gateway interface function * Add clarification comment Co-authored-by: Tyler <admin@meow.tf>
This commit is contained in:
parent
91c86f02ec
commit
769215970e
|
@ -365,6 +365,18 @@ func (s *Session) AddInteractionHandlerFunc(f webhook.InteractionHandlerFunc) {
|
||||||
s.AddInteractionHandler(f)
|
s.AddInteractionHandler(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendGateway is a helper to send messages over the gateway. It will check
|
||||||
|
// if the gateway is open and available, then send the message.
|
||||||
|
func (s *Session) SendGateway(ctx context.Context, m ws.Event) error {
|
||||||
|
// The only necessary check here is checking if gateway is nil, however
|
||||||
|
// this will save us a bit of work in serialization.
|
||||||
|
if !s.GatewayIsAlive() {
|
||||||
|
return ErrClosed
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.Gateway().Send(ctx, m)
|
||||||
|
}
|
||||||
|
|
||||||
// Close closes the underlying Websocket connection, invalidating the session
|
// Close closes the underlying Websocket connection, invalidating the session
|
||||||
// ID. It will send a closing frame before ending the connection, closing it
|
// ID. It will send a closing frame before ending the connection, closing it
|
||||||
// gracefully. This will cause the bot to appear as offline instantly. To
|
// gracefully. This will cause the bot to appear as offline instantly. To
|
||||||
|
|
|
@ -52,12 +52,12 @@ func (e ReconnectError) Unwrap() error { return e.Err }
|
||||||
type MainSession interface {
|
type MainSession interface {
|
||||||
// AddHandler describes the method in handler.Handler.
|
// AddHandler describes the method in handler.Handler.
|
||||||
AddHandler(handler interface{}) (rm func())
|
AddHandler(handler interface{}) (rm func())
|
||||||
// Gateway returns the session's main Discord gateway.
|
|
||||||
Gateway() *gateway.Gateway
|
|
||||||
// Me returns the current user.
|
// Me returns the current user.
|
||||||
Me() (*discord.User, error)
|
Me() (*discord.User, error)
|
||||||
// Channel queries for the channel with the given ID.
|
// Channel queries for the channel with the given ID.
|
||||||
Channel(discord.ChannelID) (*discord.Channel, error)
|
Channel(discord.ChannelID) (*discord.Channel, error)
|
||||||
|
// SendGateway is a helper to send messages over the gateway.
|
||||||
|
SendGateway(ctx context.Context, m ws.Event) error
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -327,7 +327,7 @@ func (s *Session) askDiscord(
|
||||||
|
|
||||||
// https://discord.com/developers/docs/topics/voice-connections#retrieving-voice-server-information
|
// https://discord.com/developers/docs/topics/voice-connections#retrieving-voice-server-information
|
||||||
// Send a Voice State Update event to the gateway.
|
// Send a Voice State Update event to the gateway.
|
||||||
if err := s.session.Gateway().Send(ctx, data); err != nil {
|
if err := s.session.SendGateway(ctx, data); err != nil {
|
||||||
return errors.Wrap(err, "failed to send Voice State Update event")
|
return errors.Wrap(err, "failed to send Voice State Update event")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -531,7 +531,7 @@ func (s *Session) Leave(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify Discord that we're leaving.
|
// Notify Discord that we're leaving.
|
||||||
sendErr := s.session.Gateway().Send(ctx, &gateway.UpdateVoiceStateCommand{
|
sendErr := s.session.SendGateway(ctx, &gateway.UpdateVoiceStateCommand{
|
||||||
GuildID: s.state.GuildID,
|
GuildID: s.state.GuildID,
|
||||||
ChannelID: discord.ChannelID(discord.NullSnowflake),
|
ChannelID: discord.ChannelID(discord.NullSnowflake),
|
||||||
SelfMute: true,
|
SelfMute: true,
|
||||||
|
|
Loading…
Reference in a new issue