forked from markdegrootnl/dj-discord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
96 lines (82 loc) · 3.16 KB
/
start.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
const Discord = require('discord.js');
const TuneIn = require('node-tunein-radio');
const _ = require('lodash');
const axios = require('axios');
const ytdl = require('ytdl-core');
require('dotenv').config();
const tunein = new TuneIn();
const client = new Discord.Client();
client.once('ready', async () => {
console.log('DJ Discord at the wheels of steel');
const channel = client.channels.cache.get(process.env.CHANNEL_ID);
const connection = await channel.join();
const mention_tag = new RegExp(`<@[!&#]?${client.user.id}>`);
function play(url) {
connection.play(url, {volume: false, bitrate: 64, plp: 10, fec: true });
}
client.on('message', async message => {
if (message.author.bot) return;
if (!mention_tag.test(message.content) && !(message.channel instanceof Discord.DMChannel)) {
return;
}
let msg = message.content.replace(mention_tag, '').trim();
console.log('< ' + msg);
switch(msg.toLowerCase()) {
case 'stop':
case 'stahp':
case 'staahp':
case 'staaahp':
case 'staaaahp':
message.channel.send('Stopping');
if (connection.dispatcher) {
connection.dispatcher.end();
}
return;
case 'rr':
case 'dj bakvis':
msg = 'https://www.youtube.com/watch?v=TzXXHVhGXTQ';
break;
case '?':
case 'help':
message.channel.send('You can send me any stream or youtube URL or the name of a Tunein.FM radio station.');
return;
}
if (msg.startsWith('http')) {
ytdl.getInfo(msg, (err, info) => {
if (err) {
message.channel.send('Playing: ' + msg);
play(msg);
return;
}
message.channel.send('Playing: ' + info.title);
play(ytdl(info.video_url));
});
return;
}
tunein.search(msg).then(function(results) {
const res = _.filter(results.body, function(item) { return item.item == 'station' });
const single = _.filter(res, function(item) { return item.text.toUpperCase() === msg.toUpperCase() });
if (res.length == 1 && single.length != 1) {
single[0] = res[0];
}
if (single.length == 1) {
message.channel.send('Playing: ' + single[0].text);
axios.get(single[0].URL).then(res => {
const url = res.data.split("\n")[0].trim();
play(url);
});
return;
}
if (res.length > 1) {
message.channel.send("To many results ("+res.length+"), please specify");
if (res.length < 10) {
message.channel.send(_.map(res, item => `• ${item.text}`).join('\n'));
}
}
if (res.length < 1) {
message.channel.send("No results");
}
});
});
});
client.login(process.env.CLIENT_ID);