Skip to content

Commit

Permalink
Merge pull request #40 from disgoorg/chore/update-snowflake-version
Browse files Browse the repository at this point in the history
update to snowflake/v2
  • Loading branch information
topi314 authored May 11, 2022
2 parents 6a47938 + 139cd3c commit 77f89c6
Show file tree
Hide file tree
Showing 21 changed files with 190 additions and 166 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ While DisGoLink can be used with any [Discord](https://discord.com) Library [Dis
* [DiscordGo](https://github.com/bwmarrin/discordgo) `string`
* [Arikawa](https://github.com/diamondburned/arikawa) `type Snowflake uint64`
* [Disgord](https://github.com/andersfylling/disgord) `type Snowflake uint64`
* [DisGo](https://github.com/disgoorg/disgo) `type Snowflake string`
* [DisGo](https://github.com/disgoorg/disgo) `type ID uint64`

This Library uses the [Disgo Snowflake](https://github.com/disgoorg/snowflake) package like DisGo

Expand Down
3 changes: 1 addition & 2 deletions dgolink/_example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/disgoorg/disgolink/dgolink"
"github.com/disgoorg/disgolink/lavalink"
"github.com/disgoorg/snowflake"
)

var (
Expand Down Expand Up @@ -154,7 +153,7 @@ func (b *Bot) play(s *discordgo.Session, guildID string, voiceChannelID string,
manager, ok := b.PlayerManagers[guildID]
if !ok {
manager = &PlayerManager{
Player: b.Link.Player(snowflake.Snowflake(guildID)),
Player: b.Link.Player(snowflake.ID(guildID)),
RepeatingMode: RepeatingModeOff,
}
b.PlayerManagers[guildID] = manager
Expand Down
33 changes: 27 additions & 6 deletions dgolink/dgolink.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dgolink
import (
"github.com/bwmarrin/discordgo"
"github.com/disgoorg/disgolink/lavalink"
"github.com/disgoorg/snowflake"
"github.com/disgoorg/snowflake/v2"
)

func New(s *discordgo.Session, opts ...lavalink.ConfigOpt) *Link {
Expand All @@ -26,15 +26,30 @@ type Link struct {
}

func (l *Link) OnVoiceStateUpdateHandler(_ *discordgo.Session, voiceStateUpdate *discordgo.VoiceStateUpdate) {
if snowflake.Snowflake(voiceStateUpdate.UserID) != l.UserID() {
userID, err := snowflake.Parse(voiceStateUpdate.UserID)
if err != nil {
l.Logger().Error("Failed to parse user ID: ", err)
return
}
var channelID *snowflake.Snowflake
if userID != l.UserID() {
return
}
var channelID *snowflake.ID
if voiceStateUpdate.ChannelID != "" {
channelID = (*snowflake.Snowflake)(&voiceStateUpdate.ChannelID)
id, err := snowflake.Parse(voiceStateUpdate.ChannelID)
if err != nil {
l.Logger().Error("Failed to parse channel ID: ", err)
return
}
channelID = &id
}
guildID, err := snowflake.Parse(voiceStateUpdate.GuildID)
if err != nil {
l.Logger().Error("Failed to parse guild ID: ", err)
return
}
l.OnVoiceStateUpdate(lavalink.VoiceStateUpdate{
GuildID: snowflake.Snowflake(voiceStateUpdate.GuildID),
GuildID: guildID,
ChannelID: channelID,
SessionID: voiceStateUpdate.SessionID,
})
Expand All @@ -45,8 +60,14 @@ func (l *Link) OnVoiceServerUpdateHandler(_ *discordgo.Session, voiceServerUpdat
if voiceServerUpdate.Endpoint != "" {
endpoint = &voiceServerUpdate.Endpoint
}

guildID, err := snowflake.Parse(voiceServerUpdate.GuildID)
if err != nil {
l.Logger().Error("Failed to parse guild ID: ", err)
return
}
l.OnVoiceServerUpdate(lavalink.VoiceServerUpdate{
GuildID: snowflake.Snowflake(voiceServerUpdate.GuildID),
GuildID: guildID,
Token: voiceServerUpdate.Token,
Endpoint: endpoint,
})
Expand Down
4 changes: 2 additions & 2 deletions dgolink/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.18

require (
github.com/bwmarrin/discordgo v0.24.0
github.com/disgoorg/disgolink/lavalink v1.6.0
github.com/disgoorg/snowflake v1.1.0
github.com/disgoorg/disgolink/lavalink v1.6.1-0.20220502235445-476e7c6ac385
github.com/disgoorg/snowflake/v2 v2.0.0
)

require (
Expand Down
8 changes: 4 additions & 4 deletions dgolink/go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
github.com/bwmarrin/discordgo v0.24.0 h1:Gw4MYxqHdvhO99A3nXnSLy97z5pmIKHZVJ1JY5ZDPqY=
github.com/bwmarrin/discordgo v0.24.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/disgoorg/disgolink/lavalink v1.6.0 h1:HwyqPkxZppCxKy0oHX5irrszfADzFozDclczrBen2P4=
github.com/disgoorg/disgolink/lavalink v1.6.0/go.mod h1:gQ/ehyTfqfUnrPGjgECDaQOUai/ft9YmlPth7TTUJkI=
github.com/disgoorg/disgolink/lavalink v1.6.1-0.20220502235445-476e7c6ac385 h1:EM3uTmHXi0JUX6tQBbyhkMirO0CWM7YfGeciHOpVlsw=
github.com/disgoorg/disgolink/lavalink v1.6.1-0.20220502235445-476e7c6ac385/go.mod h1:e/oo6afxnHvjkgGeJYrDazVIJEsQuWVSFahsek+K1mo=
github.com/disgoorg/log v1.2.0 h1:sqlXnu/ZKAlIlHV9IO+dbMto7/hCQ474vlIdMWk8QKo=
github.com/disgoorg/log v1.2.0/go.mod h1:3x1KDG6DI1CE2pDwi3qlwT3wlXpeHW/5rVay+1qDqOo=
github.com/disgoorg/snowflake v1.1.0 h1:uVF9QqI31uqo0kzbZahwvjaYQxn40SUfFK5pCNzfjYQ=
github.com/disgoorg/snowflake v1.1.0/go.mod h1:wUUTKWS1jSV0gIb3TSPsdJORNF8BcjKTjVeyrULEhr8=
github.com/disgoorg/snowflake/v2 v2.0.0 h1:+xvyyDddXmXLHmiG8SZiQ3sdZdZPbUR22fSHoqwkrOA=
github.com/disgoorg/snowflake/v2 v2.0.0/go.mod h1:SPU9c2CNn5DSyb86QcKtdZgix9osEtKrHLW4rMhfLCs=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand Down
6 changes: 3 additions & 3 deletions disgolink/_example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ import (
"github.com/disgoorg/disgolink/disgolink"
"github.com/disgoorg/disgolink/lavalink"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake"
"github.com/disgoorg/snowflake/v2"
)

var (
URLPattern = regexp.MustCompile("^https?://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]?")

token = os.Getenv("disgolink_token")
guildID = snowflake.GetSnowflakeEnv("guild_id")
guildID = snowflake.GetEnv("guild_id")
client bot.Client
dgolink disgolink.Link
musicPlayers = map[snowflake.Snowflake]*MusicPlayer{}
musicPlayers = map[snowflake.ID]*MusicPlayer{}
)

func main() {
Expand Down
6 changes: 3 additions & 3 deletions disgolink/_example/music_player.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgolink/lavalink"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake"
"github.com/disgoorg/snowflake/v2"
)

func NewMusicPlayer(client bot.Client, guildID snowflake.Snowflake) *MusicPlayer {
func NewMusicPlayer(client bot.Client, guildID snowflake.ID) *MusicPlayer {
player := dgolink.Player(guildID)
musicPlayer := &MusicPlayer{
Player: player,
Expand All @@ -27,7 +27,7 @@ type MusicPlayer struct {
lavalink.Player
queue []lavalink.AudioTrack
client bot.Client
channelID snowflake.Snowflake
channelID snowflake.ID
}

func (p *MusicPlayer) Queue(event *events.ApplicationCommandInteractionEvent, tracks ...lavalink.AudioTrack) {
Expand Down
6 changes: 3 additions & 3 deletions disgolink/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/disgoorg/disgolink/disgolink
go 1.18

require (
github.com/disgoorg/disgo v0.8.11-0.20220430142413-be50405c2a91
github.com/disgoorg/disgolink/lavalink v1.6.0
github.com/disgoorg/disgo v0.10.0
github.com/disgoorg/disgolink/lavalink v1.6.1-0.20220502235445-476e7c6ac385
)

require (
github.com/disgoorg/log v1.2.0 // indirect
github.com/disgoorg/snowflake v1.1.0 // indirect
github.com/disgoorg/snowflake/v2 v2.0.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b // indirect
)
12 changes: 6 additions & 6 deletions disgolink/go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/disgoorg/disgo v0.8.11-0.20220430142413-be50405c2a91 h1:vjBwIECUwFJ5H9EPZaY27Spy2UviMX1/Z1H05FE7AMA=
github.com/disgoorg/disgo v0.8.11-0.20220430142413-be50405c2a91/go.mod h1:F5Qj4yeX3auG0JiEY3pP5nRSUjBsbuhITTAslNsIOvA=
github.com/disgoorg/disgolink/lavalink v1.6.0 h1:HwyqPkxZppCxKy0oHX5irrszfADzFozDclczrBen2P4=
github.com/disgoorg/disgolink/lavalink v1.6.0/go.mod h1:gQ/ehyTfqfUnrPGjgECDaQOUai/ft9YmlPth7TTUJkI=
github.com/disgoorg/disgo v0.10.0 h1:HjATKy13z6/RnjTFHPaJ6XC9o3W0Ls5v9I/gK/hbLLA=
github.com/disgoorg/disgo v0.10.0/go.mod h1:Cyip4bCYHD3rHgDhBPT9cLo81e9AMbDe8ocM50UNRM4=
github.com/disgoorg/disgolink/lavalink v1.6.1-0.20220502235445-476e7c6ac385 h1:EM3uTmHXi0JUX6tQBbyhkMirO0CWM7YfGeciHOpVlsw=
github.com/disgoorg/disgolink/lavalink v1.6.1-0.20220502235445-476e7c6ac385/go.mod h1:e/oo6afxnHvjkgGeJYrDazVIJEsQuWVSFahsek+K1mo=
github.com/disgoorg/log v1.2.0 h1:sqlXnu/ZKAlIlHV9IO+dbMto7/hCQ474vlIdMWk8QKo=
github.com/disgoorg/log v1.2.0/go.mod h1:3x1KDG6DI1CE2pDwi3qlwT3wlXpeHW/5rVay+1qDqOo=
github.com/disgoorg/snowflake v1.1.0 h1:uVF9QqI31uqo0kzbZahwvjaYQxn40SUfFK5pCNzfjYQ=
github.com/disgoorg/snowflake v1.1.0/go.mod h1:wUUTKWS1jSV0gIb3TSPsdJORNF8BcjKTjVeyrULEhr8=
github.com/disgoorg/snowflake/v2 v2.0.0 h1:+xvyyDddXmXLHmiG8SZiQ3sdZdZPbUR22fSHoqwkrOA=
github.com/disgoorg/snowflake/v2 v2.0.0/go.mod h1:SPU9c2CNn5DSyb86QcKtdZgix9osEtKrHLW4rMhfLCs=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
19 changes: 11 additions & 8 deletions lavalink/_example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"syscall"

"github.com/disgoorg/disgolink/lavalink"
"github.com/disgoorg/snowflake"
"github.com/disgoorg/snowflake/v2"
)

var (
urlPattern = regexp.MustCompile("^https?://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]?")
)

func main() {
bot := &Bot{Link: lavalink.New(lavalink.WithUserID("0123456789"))}
bot := &Bot{Link: lavalink.New(lavalink.WithUserID(01234567))}
bot.registerNodes()

sc := make(chan os.Signal, 1)
Expand All @@ -32,8 +32,8 @@ type Bot struct {

func (b *Bot) messageCreateHandler() {
command := "!play channelID url"
channelID := snowflake.Snowflake("")
guildID := snowflake.Snowflake("")
channelID := snowflake.ID(0)
guildID := snowflake.ID(0)
args := strings.Split(command, " ")
if len(args) < 3 {
// TODO: send error message
Expand All @@ -45,13 +45,16 @@ func (b *Bot) messageCreateHandler() {
}
_ = b.Link.BestRestClient().LoadItemHandler(context.TODO(), query, lavalink.NewResultHandler(
func(track lavalink.AudioTrack) {
b.Play(guildID, snowflake.Snowflake(args[1]), channelID, track)
id, _ := snowflake.Parse(args[1])
b.Play(guildID, id, channelID, track)
},
func(playlist lavalink.AudioPlaylist) {
b.Play(guildID, snowflake.Snowflake(args[1]), channelID, playlist.Tracks()[0])
id, _ := snowflake.Parse(args[1])
b.Play(guildID, id, channelID, playlist.Tracks()[0])
},
func(tracks []lavalink.AudioTrack) {
b.Play(guildID, snowflake.Snowflake(args[1]), channelID, tracks[0])
id, _ := snowflake.Parse(args[1])
b.Play(guildID, id, channelID, tracks[0])
},
func() {
// TODO: send error message
Expand All @@ -62,7 +65,7 @@ func (b *Bot) messageCreateHandler() {
))
}

func (b *Bot) Play(guildID snowflake.Snowflake, voiceChannelID snowflake.Snowflake, channelID snowflake.Snowflake, track lavalink.AudioTrack) {
func (b *Bot) Play(guildID snowflake.ID, voiceChannelID snowflake.ID, channelID snowflake.ID, track lavalink.AudioTrack) {
// TODO: join voice channel

if err := b.Link.Player(guildID).Play(track); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion lavalink/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/disgoorg/log v1.2.0
github.com/disgoorg/snowflake v1.1.0
github.com/disgoorg/snowflake/v2 v2.0.0
github.com/gorilla/websocket v1.5.0
github.com/stretchr/testify v1.7.0
)
Expand Down
4 changes: 2 additions & 2 deletions lavalink/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disgoorg/log v1.2.0 h1:sqlXnu/ZKAlIlHV9IO+dbMto7/hCQ474vlIdMWk8QKo=
github.com/disgoorg/log v1.2.0/go.mod h1:3x1KDG6DI1CE2pDwi3qlwT3wlXpeHW/5rVay+1qDqOo=
github.com/disgoorg/snowflake v1.1.0 h1:uVF9QqI31uqo0kzbZahwvjaYQxn40SUfFK5pCNzfjYQ=
github.com/disgoorg/snowflake v1.1.0/go.mod h1:wUUTKWS1jSV0gIb3TSPsdJORNF8BcjKTjVeyrULEhr8=
github.com/disgoorg/snowflake/v2 v2.0.0 h1:+xvyyDddXmXLHmiG8SZiQ3sdZdZPbUR22fSHoqwkrOA=
github.com/disgoorg/snowflake/v2 v2.0.0/go.mod h1:SPU9c2CNn5DSyb86QcKtdZgix9osEtKrHLW4rMhfLCs=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
38 changes: 19 additions & 19 deletions lavalink/lavalink.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/disgoorg/log"
"github.com/disgoorg/snowflake"
"github.com/disgoorg/snowflake/v2"
)

var ErrNoUserID = errors.New("no user id has been configured")
Expand All @@ -31,15 +31,15 @@ type Lavalink interface {
EncodeTrack(track AudioTrack) (string, error)
DecodeTrack(track string) (AudioTrack, error)

Player(guildID snowflake.Snowflake) Player
PlayerOnNode(name string, guildID snowflake.Snowflake) Player
Player(guildID snowflake.ID) Player
PlayerOnNode(name string, guildID snowflake.ID) Player
RestorePlayer(restoreState PlayerRestoreState) (Player, error)
ExistingPlayer(guildID snowflake.Snowflake) Player
RemovePlayer(guildID snowflake.Snowflake)
Players() map[snowflake.Snowflake]Player
ExistingPlayer(guildID snowflake.ID) Player
RemovePlayer(guildID snowflake.ID)
Players() map[snowflake.ID]Player

UserID() snowflake.Snowflake
SetUserID(userID snowflake.Snowflake)
UserID() snowflake.ID
SetUserID(userID snowflake.ID)

Close()

Expand All @@ -60,7 +60,7 @@ func New(opts ...ConfigOpt) Lavalink {
return &lavalinkImpl{
config: *config,
nodes: map[string]Node{},
players: map[snowflake.Snowflake]Player{},
players: map[snowflake.ID]Player{},
}
}

Expand All @@ -74,15 +74,15 @@ type lavalinkImpl struct {
nodes map[string]Node

playersMu sync.Mutex
players map[snowflake.Snowflake]Player
players map[snowflake.ID]Player
}

func (l *lavalinkImpl) Logger() log.Logger {
return l.config.Logger
}

func (l *lavalinkImpl) AddNode(ctx context.Context, config NodeConfig) (Node, error) {
if l.UserID() == "" {
if l.UserID() == 0 {
return nil, ErrNoUserID
}
node := &nodeImpl{
Expand Down Expand Up @@ -206,11 +206,11 @@ func (l *lavalinkImpl) DecodeTrack(str string) (AudioTrack, error) {
})
}

func (l *lavalinkImpl) Player(guildID snowflake.Snowflake) Player {
func (l *lavalinkImpl) Player(guildID snowflake.ID) Player {
return l.PlayerOnNode("", guildID)
}

func (l *lavalinkImpl) PlayerOnNode(name string, guildID snowflake.Snowflake) Player {
func (l *lavalinkImpl) PlayerOnNode(name string, guildID snowflake.ID) Player {
l.playersMu.Lock()
defer l.playersMu.Unlock()
if player, ok := l.players[guildID]; ok {
Expand Down Expand Up @@ -250,33 +250,33 @@ func (l *lavalinkImpl) RestorePlayer(restoreState PlayerRestoreState) (Player, e
return player, nil
}

func (l *lavalinkImpl) ExistingPlayer(guildID snowflake.Snowflake) Player {
func (l *lavalinkImpl) ExistingPlayer(guildID snowflake.ID) Player {
l.playersMu.Lock()
defer l.playersMu.Unlock()
return l.players[guildID]
}

func (l *lavalinkImpl) RemovePlayer(guildID snowflake.Snowflake) {
func (l *lavalinkImpl) RemovePlayer(guildID snowflake.ID) {
l.playersMu.Lock()
defer l.playersMu.Unlock()
delete(l.players, guildID)
}

func (l *lavalinkImpl) Players() map[snowflake.Snowflake]Player {
func (l *lavalinkImpl) Players() map[snowflake.ID]Player {
l.playersMu.Lock()
defer l.playersMu.Unlock()
players := make(map[snowflake.Snowflake]Player, len(l.players))
players := make(map[snowflake.ID]Player, len(l.players))
for guildID, player := range l.players {
players[guildID] = player
}
return players
}

func (l *lavalinkImpl) UserID() snowflake.Snowflake {
func (l *lavalinkImpl) UserID() snowflake.ID {
return l.config.UserID
}

func (l *lavalinkImpl) SetUserID(userID snowflake.Snowflake) {
func (l *lavalinkImpl) SetUserID(userID snowflake.ID) {
l.config.UserID = userID
}

Expand Down
Loading

0 comments on commit 77f89c6

Please sign in to comment.