-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
121 lines (108 loc) · 3.12 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
const Discord = require('discord.js')
const client = new Discord.Client()
const ytdl = require('ytdl-core');
const prefix = '!';
const fetch = require("node-fetch");
const api = "";
const bot_secret_token = ""
client.login(bot_secret_token)
let queue = [];
let connection
client.on('message', async message => {
if(connection == undefined)
connection = await message.member.voice.channel.join();
if(message.content.startsWith(`${prefix}play`)){
insert(message,connection);
}
else if(message.content.startsWith(`${prefix}stop`)){
connection.dispatcher.destroy();
queue = [];
}
else if(message.content.startsWith(`${prefix}skip`)){
skip(connection,message)
}
else if(message.content.startsWith(`${prefix}pause`)){
connection.dispatcher.pause();
}
else if(message.content.startsWith(`${prefix}resume`)){
connection.dispatcher.resume();
}
else if(message.content.startsWith(`${prefix}random`)){
rinsert(message,connection);
}
});
const rinsert = (message,connection) => {
let lang = message.content.substring(8);
let region = "IN"
let n = Math.floor(Math.random() * 8); //max number of results
if(lang.toLowerCase().includes("eng") ){
region = "US"
}
url = `https://www.googleapis.com/youtube/v3/videos?part=contentDetails&chart=mostPopular&videoCategoryId=10®ionCode=${region}&maxResults=25&key=${api}`
fetch(url)
.then(response => response.json())
.then(async data => {
console.log(data.items[n].id);
let vid = data.items[n].id;
songInfo(vid,message,connection);
});
}
const insert = (message,connection) => {
let song_name = message.content.substring(6);
song_name = song_name.replace(/ /g,"%20");
console.log(song_name);
url = `https://www.googleapis.com/youtube/v3/search?q=${song_name}&key=${api}`;
console.log(url);
fetch(url)
.then(response => response.json())
.then(async data => {
console.log(data.items[0].id.videoId);
let vid = data.items[0].id.videoId;
songInfo(vid,message,connection);
});
}
async function songInfo(vid,message,connection){
let songInfo = await ytdl.getInfo(vid);
let title = songInfo.title;
let link = `https://www.youtube.com/watch?v=${vid}`;
if(!queue.find(obj => obj.title == title)){
let item = {};
item.title = title;
item.link = link;
queue.push(item);
console.log(queue);
if(queue.length == 1){
play(queue,connection,message);
}
else{
message.channel.send(title + " Added to queue");
}
}
else{
message.channel.send(title + " Already in queue");
}
}
const play = (queue,connection,message) => {
console.log("started");
let link = queue[0].link;
message.channel.send("Currently Playing: " + queue[0].title);
const dispatcher = connection.play(ytdl(link, { filter: 'audioonly',highWaterMark: 1<<25 } , { highWaterMark: 1 },{ bitrate: "auto" }));
dispatcher.on('finish', () => {
queue.shift();
if(queue[0] !== undefined){
play(queue,connection);
}
else{
connection.dispatcher.destroy();
}
});
}
const skip = (connection,message) =>{
queue.shift();
if(queue[0] !== undefined){
play(queue,connection,message);
}
else{
message.channel.send("Please add more songs");
}
}