-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (95 loc) 路 2.3 KB
/
main.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
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"os"
"strconv"
"strings"
)
var (
client *discordgo.Session
vconn *discordgo.VoiceConnection
youtubeKey string
discordKey string
chans string
channel string
guildId string
limitVids int64
bitRate int64
)
func init() {
channel = os.Getenv("CHANNEL_ID")
guildId = os.Getenv("GUILD_ID")
youtubeKey = os.Getenv("YOUTUBE")
discordKey = os.Getenv("DISCORD")
chans = os.Getenv("CHANNELS")
lim := os.Getenv("LIMIT")
limitVids, _ = strconv.ParseInt(lim, 10, 64)
if limitVids == 0 {
limitVids = 20
}
bitr := os.Getenv("BITRATE")
bitRate, _ = strconv.ParseInt(bitr, 10, 64)
if bitRate == 0 {
bitRate = 64
}
}
func updateList() {
for _, v := range strings.Split(chans, ",") {
out, err := Channel(v)
fmt.Printf("Found %v videos for Channel: %s\n", len(out.Items), v)
if err != nil {
panic(err)
}
playableChannels = append(playableChannels, out)
}
}
func main() {
updateList()
var err error
client, err = discordgo.New("Bot " + discordKey)
if err != nil {
panic(err)
}
if err = client.Open(); err != nil {
panic(err)
}
vconn, err = client.ChannelVoiceJoin(guildId, channel, false, true)
if err != nil {
panic(err)
}
defer Close()
playNext(onChannel, onIndex)
}
func Close() {
vconn.Close()
client.Close()
}
func playNext(chanIndex, vidIndex int) {
chanObj := playableChannels[chanIndex]
video := chanObj.Items[vidIndex]
if video == nil {
fmt.Printf("video %v not found\n", vidIndex)
updateList()
chanObj = playableChannels[0]
video = chanObj.Items[vidIndex]
}
fmt.Printf("Now streaming: %s - %s - https://www.youtube.com/watch?v=%s\n", video.Snippet.ChannelTitle, video.Snippet.Title, video.ID.VideoID)
if err := client.UpdateStatus(0, fmt.Sprintf("%s - %s", video.Snippet.ChannelTitle, video.Snippet.Title)); err != nil {
fmt.Println(err)
}
PlayAudioFile(vconn, fmt.Sprintf("https://www.youtube.com/watch?v=%s", video.ID.VideoID), make(chan bool))
fmt.Printf("playing next channel %v item %v\n", chanIndex, vidIndex)
vidIndex++
if vidIndex >= len(chanObj.Items) {
vidIndex = 0
chanIndex++
}
if chanIndex >= len(playableChannels) {
fmt.Println("updating list from channel: ", chanIndex)
updateList()
chanIndex = 0
vidIndex = 0
}
playNext(chanIndex, vidIndex)
}