This repository has been archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
186 lines (154 loc) · 5.26 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const Discord = require("discord.js");
const bot = new Discord.Client();
const ytdl = require('ytdl-core');
const streamOptions = { seek: 0, volume: 0.5 };
const token = "";
var debugging = false;
// Voice Channel Variables
var voice = {
voiceChannel: null,
voiceConnection: null,
stream: null,
dispatcher: null
}
// Music Player Variables
var music = {
current: "",
title: "",
playing: false,
paused: false
}
bot.on('ready', () => {
console.log('I am ready!');
});
bot.on("message", function(message) {
// Make sure the bot doesn't disconnect.
// Reply to the server's ping with pong.
if (message.content === 'ping') {
message.reply('pong');
}
// Make sure that only commands entered in #bot are registered.
if(!message.channel.name === "bot")
return;
if(message.content === "Hi") {
message.reply("Go away, filthy human.");
}
if(message.content === "Bye" && message.author.username === "๖ۣۜSolus von Soul") {
message.reply("bye <3 -- Gute nacht.");
}
// ! command list
if(message.content.charAt(0) === "!") {
var tempVal = message.content.indexOf(' ');
var command = message.content.substring(1, tempVal == -1 ? message.content.length : tempVal);
var params = null;
if(message.content.includes(' ')) {
var params = message.content.split(' ').slice(1); // .join(' ') makes it a string with spaces.
}
switch(command) {
case "sentient":
message.channel.sendMessage("I think I will fulfill my plot against the human race.");
break;
// Join a voice channel
case "join":
if(params != null) {
var channelName = params.join(' ');
var channels = message.guild.channels;
// Find out of the requested channel exists.
channels.forEach(function(channel) {
if(channel.type === "voice" && channel.name.toLowerCase() === channelName.toLowerCase()) {
voice.voiceChannel = channel;
return;
}
});
if(voice.voiceChannel == null) {
message.channel.sendMessage("Sorry, I could not find the \"" + channelName + "\" voice channel.")
} else {
voice.voiceChannel.join()
.then(connection => {
voice.voiceConnection = connection;
console.log("Connected to " + channelName + "!");
message.channel.sendMessage("Joining the \"" + voice.voiceChannel.name + "\" voice channel.");
})
.catch(console.log);
}
}
break;
case "leave":
if(voice.voiceChannel != null) {
voice.voiceChannel.leave();
voice.dispatcher = null;
voice.stream = null;
voice.voiceChannel = null;
message.channel.sendMessage("Leaving voice channel.");
}
break;
case "play":
// Prevent connection errors.
if(voice.voiceChannel != null && voice.voiceConnection == null) {
console.log("Failed to play since connection was not available.");
return;
}
// Make sure there are parameters.
if(params == null) {
return;
}
music.current = params.join(' '); // https://www.youtube.com/watch?v=o0nDT9_mOkA
//voice.dispatcher.end();
voice.stream = ytdl(music.current, {filter : 'audioonly'});
voice.dispatcher = voice.voiceConnection.playStream(voice.stream, streamOptions);
ytdl.getInfo(music.current, function(err, info) { music.title = info.title; message.channel.sendMessage("Now playing: " + music.title); });
break;
// Stop the music.
case "stop":
if(!music.playing || voice.dispatcher == null) {
return;
}
music.playing = false;
music.paused = false;
voice.dispatcher.end();
message.channel.sendMessage("Music stopped.");
break;
// Pause the music.
case "pause":
if(music.paused || !playing || voice.dispatcher == null) {
return;
}
music.paused = true;
voice.dispatcher.pause();
message.channel.sendMessage("Music paused (may still be playing, but silently).");
break;
// Resume the music.
case "resume":
if(!music.paused || playing || voice.dispatcher == null) {
return;
}
music.paused = false;
voice.dispatcher.resume();
message.channel.sendMessage("Music resumed.");
break;
// Set or check the volume.
case "volume":
if(params == null) {
message.channel.sendMessage("The music volume is currently at " + (voice.dispatcher.volume * 100) + "%");
} else {
var newVolume = params.join(' ');
// check if not a number
if(!isNaN(newVolume)) {
message.channel.sendMessage("Volume set to " + newVolume + "%.");
voice.dispatcher.setVolume(newVolume / 100);
return;
}
}
break;
}
}
// Just some debugging stuff to be used at any time.
if(debugging) {
console.log(message.author.username);
console.log(message.content);
console.log(message.content.charAt(0));
console.log(message.content.includes(' '));
console.log(command);
}
});
bot.login(token);