-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
337 lines (336 loc) · 10.4 KB
/
bot.js
File metadata and controls
337 lines (336 loc) · 10.4 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// ------------------------ Loading ------------------------ //
// Modules
const Eris = require("eris")
// Music
const ytdl = require('ytdl-core');
const search = require('youtube-search-api');
const ffmpeg = require('fluent-ffmpeg');
//
const traverse = require('traverse');
const fs = require("fs")
const colors = require('colors');
// Files
var userdataFile = require("./userdata.json")
const auth = require("./auth/token.json")
const config = require("./config.json")
// Command contents
const copypasta = require ("./assets/copypasta.json");
// Bot object
const bot = new Eris(auth.token)
var users = userdataFile;
// Functions
// Function to play song
function play(url, addNewSongToQueue, voiceConnection, msg) {
ytdl.getBasicInfo(url)
.then(res => {
// Add the song to the queue
var queueInfo = {
user: msg.author.username,
url: url,
title: res.videoDetails.title,
duration: res.videoDetails.lengthSeconds
}
console.log(url)
if (addNewSongToQueue) {
connections[msg.member.guild.id].queue.push(queueInfo);
}
bot.createMessage(msg.channel.id, `playing **${res.videoDetails.title}**`);
let stream = ytdl(url, {
//quality: 'highestaudio',
filter: "audioonly"
})
if (!connections[msg.member.guild.id].active) {
voiceConnection.play(stream);
connections[msg.member.guild.id].active = true;
}
})
.catch(error => {
console.log(error)
})
}
// Bot ready
bot.on("ready", () => {
console.log("Cyfer is up and running.".brightGreen)
// ------------------------ User Data Handling ------------------------ //
// Set up default user structure
var defaultUser = {
version: 0,
// Levelling
level: 1,
exp: 0,
expMax: 1000,
lastMessageTime: 0,
}
// Loop through guilds and their members
bot.guilds.map((guild) => {
console.log(`Loading server: ${guild.name.green}`)
// Check if member exist in saves
guild.fetchMembers().then((members) => {
for (var member of members) {
if (!member.bot) {
console.log(`Loading member ${member.username.cyan} (${guild.name.green})`)
var user = users[member.id];
// Initialize data if user id is not founded in userdata
if ((user == undefined) || (user.version < defaultUser.version)) {
user = defaultUser;
} else {
// If found then verify structure
for (var key in defaultUser) {
// Check if key in defaultUser structure exist user structure
if (user[key] == undefined) {
// Update with new structure if not
user[key] = defaultUser[key]
}
}
}
users[member.id] = user;
}
}
console.log("Loading finished!")
})
})
})
// ------------------------ Main Bot ------------------------ //
// Music queues, separated by server
var connections = {};
// Message event
bot.on("messageCreate", (msg) => {
// Debug
console.log(`[${msg.channel.guild.name.yellow}] (${msg.channel.name.green}) ${msg.author.username.cyan}: ${msg.content.brightWhite}`);
if (msg.author.bot) return
// Check for prefix
var id = msg.author.id
if (msg.content.toLowerCase().startsWith(config.prefix)) {
// Setting up args and commands
var args = msg.content.substr(1).split(" ");
var cmd = args[0]
args.shift();
// Commands
switch(cmd) {
case "help":
bot.createMessage(msg.channel.id, "Cyfer is a bot that does whatever you think it does. Made by Azza.\nYou can view a list of commands here: https://github.com/AzzaDeveloper/cyfer-bot/wiki");
break;
case "profile":
case "p":
// Create vanity progress bar
var progress = Math.floor(users[id].exp / users[id].expMax * 10)
var progressDisplay = ""
for (var i = 0; i < 10; i++) {
i <= progress ? progressDisplay += "██" : progressDisplay += " ";
}
bot.createMessage(msg.channel.id, {
embed: {
author: {
name: msg.author.username + "'s profile",
icon_url: msg.author.avatarURL
},
title: `Level ${users[id].level}`,
fields: [
{
name: `[${progressDisplay}]`,
value: users[id].exp + "/" + users[id].expMax
}
],
color: 0,
}
})
break;
case "play":
var voiceId = msg.member.voiceState.channelID;
// If user not in a voice channel then exit
if (voiceId == undefined) {
bot.createMessage("dude join a voice channel first", msg.channel.id);
} else {
// Joins the voice channel
bot.joinVoiceChannel(voiceId)
// Stream the music once connection is established
.then(voiceConnection => {
// Check for connection in list
if (connections[msg.member.guild.id] == undefined) {
connections[msg.member.guild.id] = {
connection: voiceConnection,
queue: [],
active: false,
playing: 0,
loopMode: "none",
looping: false
}
voiceConnection.on("end", () => {
console.log("ended, starting new")
// Play the next song if available
if (connections[msg.member.guild.id].playing == -1) {
bot.createMessage(msg.channel.id, "queue's empty");
return
}
if (connections[msg.member.guild.id].playing + 1 < connections[msg.member.guild.id].queue.length) {
connections[msg.member.guild.id].active = false;
var url = connections[msg.member.guild.id].queue[connections[msg.member.guild.id].playing + 1].url;
connections[msg.member.guild.id].playing++;
play(url, false, connections[msg.member.guild.id].connection, msg)
} else {
bot.createMessage(msg.channel.id, "end of queue");
connections[msg.member.guild.id].active = false;
}
})
voiceConnection.on("error", (error) => {
console.log(error);
})
}
var url;
// Get the song, by position in queue or by the URL or get first youtube search result
if (!isNaN(parseInt(args[0]))) {
// If cant find pos, return bullshit
if (connections[msg.member.guild.id].queue[args[0] - 1] == undefined) {
bot.createMessage(msg.channel.id, "theres no song at that position in the queue are you blind");
return
} else {
url = connections[msg.member.guild.id].queue[args[0] - 1].url;
connections[msg.member.guild.id].queue[args[0] - 1].playing = args[0] - 1;
addNewSongToQueue = true;
// Play song
play(url, true, voiceConnection, msg)
}
// Playing a link
} else if (args[0].substr(0, 4) == "https") {
url = args[0]
play(url, true, voiceConnection, msg)
// Playing a search result
} else {
search.GetListByKeyword(args.join(" "))
.then(res => {
console.log(`https://www.youtube.com/watch?v=${res.items[0].id}`)
play(`https://www.youtube.com/watch?v=${res.items[0].id}`, true, voiceConnection, msg)
})
}
})
// Catch errors
.catch(message => {
console.log(message);
})
}
break;
case "q":
var queue;
if (connections[msg.member.guild.id] == undefined) {
bot.createMessage(msg.channel.id, "you need to start playing a song to start the queue");
} else {
queue = connections[msg.member.guild.id].queue
var text = "";
var i = 1;
queue.forEach(element => {
text = `${text}${i}. **${element.title}** - ${element.user}\n`
i++;
});
bot.createMessage(msg.channel.id, text);
}
break;
case "pause":
var connection = connections[msg.member.guild.id].connection;
connection.pause();
break;
case "skip":
// Play the next song
connections[msg.member.guild.id].connection.stopPlaying()
break;
case "stop":
var connection = connections[msg.member.guild.id].connection;
connections[msg.member.guild.id].playing = -1;
connection.stopPlaying()
connections[msg.member.guild.id] = undefined;
break;
case "resume":
var connection = connections[msg.member.guild.id].connection;
connection.resume();
break;
case "copypasta":
var content = copypasta[args[0]];
if (content != undefined) {
bot.createMessage(msg.channel.id, copypasta[args[0]]);
} else {
bot.createMessage(msg.channel.id, "No copypasta found.");
}
break;
}
}
// Levelling
// Check if its been 2 seconds since last message
if (msg.timestamp - users[id].lastMessageTime >= 2) {
// Give exp
users[id].exp += 10
users[id].lastMessageTime = msg.timestamp
// Level up and update max exp
if (users[id].exp >= users[id].expMax) {
users[id].level += 1;
users[id].exp = 0;
users[id].expMax = Math.floor(Math.pow(users[id].expMax, 1.1));
// Create embed
bot.createMessage(msg.channel.id, {
embed: {
author: {
name: `${msg.author.username} leveled up!`,
icon_url: msg.author.avatarURL
},
title: `${msg.author.username} is now level ${users[id].level}. `,
description: `${users[id].exp} / ${users[id].expMax}`,
color: 0,
}
})
}
}
})
// Connect the bot
bot.connect();
// -------------------------- Console ---------------------------- //
const readline = require("readline");
const { on } = require("events");
const { runInThisContext } = require("vm");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Sending messages
var servers = {
gensokyo: "749259194263273603"
}
var channels = {
hall: "749259195039350866",
poke: "854617364947402794",
rpg: "854545534027431946"
}
var activeServer = servers.gensokyo;
var activeChannel = "";
// Handling enters
rl.on("line", function(input) {
var splitted = input.split(" ");
var command = splitted[0];
splitted.shift();
var content = splitted.join(" ");
if (command == "server") {
activeServer = servers[content];
}
if (command == "channel") {
activeChannel = channels[content];
}
if (command == "say") {
// Find channel in that server
bot.createMessage(activeChannel, content);
}
// Saving
if (command == "save") {
var data = fs.readFileSync("./userdata.json")
fs.writeFileSync("./userdata.json", data)
console.log("User data have been saved. " + "Sure hope it isn't corrupted, huh?".red)
}
if (command == "exit") {
var data = fs.readFileSync("./userdata.json")
fs.writeFileSync("./userdata.json", data)
console.log("User data have been saved. " + "Sure hope it isn't corrupted, huh?".red)
process.exit()
}
});
rl.on("close", function() {
var data = fs.readFileSync("./userdata.json")
fs.writeFileSync("./userdata.json", data)
console.log("User data have been saved. " + "Sure hope it isn't corrupted, huh?".red)
process.exit()
});