This repository has been archived by the owner on Dec 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playCommand.go
145 lines (129 loc) · 4 KB
/
playCommand.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
package commands
import (
"github.com/PaulekOfficial/RocketDiscord/cache"
"github.com/PaulekOfficial/RocketDiscord/utils"
"github.com/andersfylling/disgord"
"github.com/sirupsen/logrus"
"strconv"
"time"
)
func init() {
command := NewCommand("play", false, false, onPlayCommand)
command.SetArgumentsRequirements(true, 1, -1)
command.SetHelpMessage("Polecenia pozwalajace na dolaczenie bota do kanalu glosowego przez ktorego mozna puscic muzykne na kanale. Użycie: /play <link z youtube lub innego serwisu z muzyką>")
command.Register()
}
func onPlayCommand(session disgord.Session, event *disgord.MessageCreate, guild *disgord.Guild, args []string) (err error) {
member := event.Message.Member
musicState := cache.GetVoiceState(guild.ID)
channelId := event.Message.ChannelID
channel, err := session.Channel(channelId).Get()
if err != nil {
return err
}
if musicState == nil {
voiceState := cache.GetUserVoiceState(member.UserID)
if voiceState == nil {
_, err := session.SendMsg(channelId, ":cloud_tornado: Abym mógł puścić dla ciebie muzykę, musisz znajdować się na kanale głosowym. (Jeżeli jesteś na kanale głosowym poprostu wyjdz i wejdz ponownie na kanał)")
return err
}
voiceChannelId := voiceState.ChannelID
if voiceChannelId.IsZero() {
_, err := session.SendMsg(channelId, ":cloud_tornado: Abym mógł puścić dla ciebie muzykę, musisz znajdować się na kanale głosowym. (Jeżeli jesteś na kanale głosowym poprostu wyjdz i wejdz ponownie na kanał)")
return err
}
musicState = &cache.MusicBotState{
GuildId: guild.ID,
VoiceChannelId: voiceChannelId,
Voice: nil,
Tracks: make(map[int]*cache.MusicBotTrack),
LoopTrack: false,
LoopPlayList: false,
Paused: false,
Running: false,
LastPlay: time.Now(),
}
musicState = cache.PutGuildMusicState(guild.ID, musicState)
}
track, err := utils.GetYoutubeStreamMemory(args, session, channelId)
if err != nil || track == nil {
_, err := session.SendMsg(channelId, ":sob: Nie znaleziono takiego utworu video/audio.")
return err
}
musicState.Tracks[len(musicState.Tracks)] = track
if len(musicState.Tracks) == 1 {
_, err = session.SendMsg(channelId, ":beers: No i jest grane.")
if err != nil {
return err
}
err = startPlaying(session, musicState, channelId)
if err != nil {
return err
}
} else {
user, err := session.User(member.UserID).Get()
if err != nil {
return err
}
avatarUrl, err := user.AvatarURL(32, false)
if err != nil {
return err
}
author := &disgord.EmbedAuthor{
Name: "Dodano do kolejki",
URL: "https://paulek.pro/",
IconURL: avatarUrl,
ProxyIconURL: "",
}
fields := []*disgord.EmbedField{
{
Name: "Kanal",
Value: channel.Name,
Inline: true,
},
{
Name: "Czas trwania utworu",
Value: track.Duration.String(),
Inline: true,
},
{
Name: "Pozycja w kolejce",
Value: strconv.Itoa(len(musicState.Tracks)),
Inline: false,
},
}
_, err = session.Channel(channelId).CreateMessage(&disgord.CreateMessageParams{
Content: "",
Tts: false,
Embed: &disgord.Embed{
Title: "",
Author: author,
Description: track.URL,
Timestamp: disgord.Time{},
Fields: fields,
},
})
if err != nil {
return err
}
}
return
}
func startPlaying(session disgord.Session, musicState *cache.MusicBotState, channelId disgord.Snowflake) error {
voice, err := session.Guild(musicState.GuildId).VoiceChannel(musicState.VoiceChannelId).Connect(false, true)
if err != nil {
return err
}
musicState.Voice = voice
go func() {
err = utils.PlayDCAAudio(musicState.GuildId, channelId, session)
if err != nil {
_, err = session.SendMsg(channelId, ":boom: Oj cos poszlo nie tak, nie mogę puścić muzyki :(.")
logrus.WithError(err).WithFields(logrus.Fields{
"music-state": musicState,
}).Error("Failed to play songs")
}
musicState.Running = false
}()
return nil
}