-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
153 lines (116 loc) · 3.95 KB
/
bot.js
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
var fs = require("fs");
var request = require("request");
var ytdl = require("ytdl-core");
var bot = module.exports = {
/* R-a-d.io constants*/
RADIO_LINK: "https://relay0.r-a-d.io/main.mp3",
API_LINK: "https://r-a-d.io/api",
CONFIG: "",
RADIO_CHANNELS: [],
CHANNELS_JOINED: [],
// CONFIG_JSON: {},
PLAYING_RADIO: true,
/* Youtube queue constants */
YT_QUEUE: [],
CURRENT_YT: "",
init: function(config_file, client) {
fs.readFile(config_file, "utf-8", function(error, data) {
if (error) {
console.log(error);
}
config_json = JSON.parse(data);
bot.RADIO_CHANNELS = config_json["voice_channels"];
client.login(config_json["api_key"]);
});
},
/* R-a-d.io functions */
play_radio: function(voice_connection) {
bot.PLAYING_RADIO = true;
var dispatcher = voice_connection.playStream(request(bot.RADIO_LINK), {seek: 0, volume: 1});
},
/**
* Go through channels to find a voice channel with the given channel_id
*/
get_radio_channel: function(channel_id, channels) {
for (var i = 0; i < channels.length; i++) {
var channel = channels[i];
if (channel.type == "voice" && channel.id == channel_id) {
return channel;
}
}
},
/**
* Join all voice channels specified in the config file.
*/
join_init_channels: function(client_channels) {
for (var i = 0; i < bot.RADIO_CHANNELS.length; i++) {
var channel_id = bot.RADIO_CHANNELS[i].id;
var channel = bot.get_radio_channel(channel_id, client_channels);
if (channel != undefined) {
bot.join_channel(channel);
}
}
},
/**
* Join the given channel and start playing the radio.
*/
join_channel: function(channel) {
console.log("Joined voice channel " + channel.name);
channel.join().then(bot.play_radio);
},
/**
* Move to the given channel. Does not restart playing the stream.
* If the bot is already playing, it should continue to play.
*/
move_channel: function(channel) {
console.log("Moved to voice channel " + channel.name);
channel.move();
},
/**
* Gets the channel using channel_id from the client.
* Then calls the callback function with the found channel.
*/
get_channel: function(channel_id, client_channels, callback) {
var channel = bot.get_radio_channel(channel_id, client_channels);
if (channel != null) {
callback(channel);
}
},
get_api_info: function(callback) {
request(bot.API_LINK, callback);
},
send_playing: function(channel) {
if (bot.PLAYING_RADIO) {
bot.get_api_info(function(err, res, body) {
var info = JSON.parse(body)["main"]["np"];
channel.sendMessage("Now playing: " + info);
});
}
},
send_next: function(channel) {
bot.get_api_info(function(err, res, body) {
var info = JSON.parse(body)["main"]["queue"][0]["meta"];
channel.sendMessage("Next song: " + info);
})
},
send_queue: function(channel) {
bot.get_api_info(function(err, res, body) {
var queue = JSON.parse(body)["main"]["queue"];
var message = "Queue: "
for (var i = 0; i < queue.length; i++) {
message += "\n" + (i+1) + ". " + queue[i]["meta"];
}
channel.sendMessage(message);
});
},
send_dj: function(channel) {
bot.get_api_info(function(err, res, body) {
info = JSON.parse(body);
dj = info["main"]["dj"]["djname"];
channel.sendMessage("DJ: " + dj);
});
},
send_schedule: function(channel) {
channel.sendFile("./schedule.png", "schedule.png");
}
}