-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
145 lines (111 loc) · 4.05 KB
/
main.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
const Discord = require('discord.js');
const {
prefix,
token,
} = require('./settings.json');
const ytdl = require('ytdl-core');
const client = new Discord.Client();
const queue = new Map();
client.on('ready', () => {
console.log(`${client.user.tag} ONLINE`);
client.user.setActivity('/play', {type: 'LISTENING'}).catch(console.error);
});
client.on('message', async message => {
if (!message.guild) return;
if (message.content.startsWith(`${prefix}satan`)) {
const user = message.mentions.users.first();
if (user) {
const member = message.guild.member(user);
if (member) {
message.channel.send
}
}
}
});
client.on('message', async message => {
if (!message.guild) return;
if (!message.content.startsWith(prefix)) return;
const serverQueue = queue.get(message.guild.id);
const lowerCaseMessage = message.content.toLowerCase();
if (lowerCaseMessage.startsWith(`${prefix}play`)) {
//Play command
execute(message, serverQueue);
return;
} else if (lowerCaseMessage.startsWith(`${prefix}skip`)) {
//Skip command
skip(message, serverQueue);
return;
} else if (lowerCaseMessage.startsWith(`${prefix}stop`)) {
//Stop command
stop(message, serverQueue);
return;
}
});
async function execute(message, serverQueue) {
const args = message.content.split(' ');
const voiceChannel = message.member.voiceChannel;
if (!voiceChannel) return message.channel.send(':bangbang:You should be on the voice channel:bangbang:');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) {
return message.channel.send(':bangbang:You dont have permission!');
}
const songInfo = await ytdl.getInfo(args[1]);
const song = {
title: songInfo.title,
url: songInfo.video_url,
};
if (!serverQueue) {
const queueContruct = {
textChannel: message.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: 5,
playing: true,
};
queue.set(message.guild.id, queueContruct);
queueContruct.songs.push(song);
try {
var connection = await voiceChannel.join();
queueContruct.connection = connection;
play(message.guild, queueContruct.songs[0]);
} catch (err) {
console.log(err);
queue.delete(message.guild.id);
return message.channel.send(err);
}
} else {
serverQueue.songs.push(song);
console.log(serverQueue.songs);
return message.channel.send(`:poop:${song.title} Queued.:poop:`);
}
}
function play(guild, song) {
const serverQueue = queue.get(guild.id);
if (!song) {
serverQueue.voiceChannel.leave();
queue.delete(guild.id);
return;
}
const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
.on('end', () => {
console.log('It s Over');
serverQueue.songs.shift();
play(guild, serverQueue.songs[0]);
})
.on('error', error => {
console.log(error);
});
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
}
function skip(message, serverQueue) {
if (!message.member.voiceChannel) return message.channel.send(':bangbang:You should be on the voice channel:bangbang:');
if (!serverQueue) return message.channel.send(':bangbang:There is no song you can skip:bangbang:');
serverQueue.connection.dispatcher.end();
}
function stop(message, serverQueue) {
if (!message.member.voiceChannel) return message.channel.send(':bangbang:You should be on the voice channel:bangbang:');
serverQueue.songs = [];
serverQueue.connection.dispatcher.end();
}
client.login(token);