mirror of
https://github.com/musix-org/musix-oss
synced 2025-06-16 22:06:01 +00:00
Fix code to work on this decade 1/x
This commit is contained in:
34
src/funcs/check.js
Normal file
34
src/funcs/check.js
Normal file
@ -0,0 +1,34 @@
|
||||
module.exports = function (client, msg, command) {
|
||||
const queue = client.queue.get(msg.guild.id);
|
||||
const permissions = msg.channel.permissionsFor(msg.author);
|
||||
if (!queue || !queue.playing && command.name !== "stop") {
|
||||
msg.channel.send(client.messages.noServerQueue);
|
||||
return false;
|
||||
}
|
||||
if (msg.author.id !== client.config.devId) {
|
||||
if (msg.member.voice.channel !== queue.voiceChannel) {
|
||||
msg.channel.send(client.messages.wrongVoiceChannel);
|
||||
return false;
|
||||
}
|
||||
if (client.global.db.guilds[msg.guild.id].permissions === true) {
|
||||
if (client.global.db.guilds[msg.guild.id].dj) {
|
||||
if (
|
||||
!msg.member.roles.cache.has(
|
||||
client.global.db.guilds[msg.guild.id].djrole
|
||||
)
|
||||
) {
|
||||
msg.channel.send(client.messages.noDj);
|
||||
return false;
|
||||
} else return true;
|
||||
} else if (!permissions.has(command.permission)) {
|
||||
let message;
|
||||
message = client.messages.noPerms.replace(
|
||||
"%PERMS%",
|
||||
command.permissions
|
||||
);
|
||||
msg.channel.send(message);
|
||||
return false;
|
||||
} else return true;
|
||||
} else return true;
|
||||
} else return true;
|
||||
};
|
52
src/funcs/checkDB.js
Normal file
52
src/funcs/checkDB.js
Normal file
@ -0,0 +1,52 @@
|
||||
module.exports = async function (client) {
|
||||
client.guilds.cache.forEach((guild) => {
|
||||
if (!client.global.db.guilds[guild.id]) {
|
||||
client.db.collection("guilds").doc(guild.id).set({
|
||||
prefix: client.config.prefix,
|
||||
defaultVolume: client.config.defaultVolume,
|
||||
permissions: client.config.permissions,
|
||||
dj: client.config.dj,
|
||||
djrole: client.config.djrole,
|
||||
startPlaying: client.config.startPlaying,
|
||||
bass: client.config.bass,
|
||||
blacklist: [],
|
||||
premium: false,
|
||||
autoPlay: client.config.autoPlay,
|
||||
});
|
||||
client.global.db.guilds[guild.id] = {
|
||||
prefix: client.config.prefix,
|
||||
defaultVolume: client.config.defaultVolume,
|
||||
permissions: client.config.permissions,
|
||||
dj: client.config.dj,
|
||||
djrole: client.config.djrole,
|
||||
startPlaying: client.config.startPlaying,
|
||||
bass: client.config.bass,
|
||||
blacklist: [],
|
||||
premium: false,
|
||||
autoPlay: client.config.autoPlay,
|
||||
};
|
||||
return;
|
||||
}
|
||||
if (!client.global.db.guilds[guild.id].prefix)
|
||||
client.global.db.guilds[guild.id].prefix = client.config.prefix;
|
||||
if (!client.global.db.guilds[guild.id].defaultVolume)
|
||||
client.global.db.guilds[guild.id].defaultVolume =
|
||||
client.config.defaultVolume;
|
||||
if (!client.global.db.guilds[guild.id].permissions)
|
||||
client.global.db.guilds[guild.id].permissions = client.config.permissions;
|
||||
if (!client.global.db.guilds[guild.id].dj)
|
||||
client.global.db.guilds[guild.id].dj = client.config.dj;
|
||||
if (!client.global.db.guilds[guild.id].djrole)
|
||||
client.global.db.guilds[guild.id].djrole = client.config.djrole;
|
||||
if (!client.global.db.guilds[guild.id].startPlaying)
|
||||
client.global.db.guilds[guild.id].startPlaying =
|
||||
client.config.startPlaying;
|
||||
if (!client.global.db.guilds[guild.id].bass)
|
||||
client.global.db.guilds[guild.id].bass = client.config.bass;
|
||||
if (!client.global.db.guilds[guild.id].blacklist)
|
||||
client.global.db.guilds[guild.id].blacklist = [];
|
||||
if (!client.global.db.guilds[guild.id].premium)
|
||||
client.global.db.guilds[guild.id].premium = false;
|
||||
if (!client.global.db.guilds[guild.id].autoPlay) client.global.db.guilds[guild.id].autoPlay = client.config.autoPlay
|
||||
});
|
||||
};
|
22
src/funcs/dbget.js
Normal file
22
src/funcs/dbget.js
Normal file
@ -0,0 +1,22 @@
|
||||
module.exports = async function (collection, doc, client) {
|
||||
if (doc) {
|
||||
let d = await client.db.collection(collection).doc(doc).get().catch(err => {
|
||||
console.log('Error getting document', err);
|
||||
return 'error';
|
||||
});
|
||||
return d.data();
|
||||
} else {
|
||||
let d = await client.db.collection(collection).get().catch(err => {
|
||||
console.log('Error getting document', err);
|
||||
return 'error';
|
||||
});
|
||||
let finalD = [];
|
||||
d.forEach(doc => {
|
||||
finalD.push({
|
||||
id: doc.id,
|
||||
d: doc.data(),
|
||||
});
|
||||
});
|
||||
return finalD;
|
||||
}
|
||||
};
|
12
src/funcs/end.js
Normal file
12
src/funcs/end.js
Normal file
@ -0,0 +1,12 @@
|
||||
module.exports = async function (client, msg, pos, command) {
|
||||
const seek = parseInt(pos);
|
||||
const queue = client.queue.get(msg.guild.id);
|
||||
if (command.name === "seek") {
|
||||
queue.time = seek * 1000;
|
||||
} else {
|
||||
queue.time = queue.connection.dispatcher.streamTime + queue.time;
|
||||
}
|
||||
queue.connection.dispatcher.end();
|
||||
queue.endReason = "seek";
|
||||
client.funcs.play(msg.guild, queue.songs[0], client, seek, false);
|
||||
};
|
20
src/funcs/exe.js
Normal file
20
src/funcs/exe.js
Normal file
@ -0,0 +1,20 @@
|
||||
module.exports = function (msg, args, client, command) {
|
||||
const permissions = msg.channel.permissionsFor(client.user);
|
||||
if (!permissions.has("EMBED_LINKS"))
|
||||
return msg.channel.send(client.messages.noPermsEmbed);
|
||||
if (!permissions.has("USE_EXTERNAL_EMOJIS"))
|
||||
return msg.channel.send(client.messages.noPermsUseExternalEmojis);
|
||||
if (
|
||||
command.category === "music" &&
|
||||
client.global.db.guilds[msg.guild.id].blacklist.includes(msg.channel.id)
|
||||
)
|
||||
return msg.channel.send(client.messages.musicCommandsDisabled);
|
||||
try {
|
||||
command.uses++;
|
||||
command.execute(msg, args, client, command);
|
||||
} catch (error) {
|
||||
msg.reply(client.messages.errorExe);
|
||||
console.log(error.toString());
|
||||
console.log(error.stack.replace(/at /g, "**at **"));
|
||||
}
|
||||
};
|
28
src/funcs/getSpotifyKey.js
Normal file
28
src/funcs/getSpotifyKey.js
Normal file
@ -0,0 +1,28 @@
|
||||
module.exports = async function (client) {
|
||||
|
||||
const request = require("request");
|
||||
|
||||
const refresh_token = client.config.spotify_refresh_token;
|
||||
const authOptions = {
|
||||
url: "https://accounts.spotify.com/api/token",
|
||||
headers: {
|
||||
Authorization: "Basic " +
|
||||
new Buffer(client.config.spotify_client_id + ":" + client.config.spotify_client_secret).toString("base64"),
|
||||
},
|
||||
form: {
|
||||
grant_type: "refresh_token",
|
||||
refresh_token: refresh_token,
|
||||
},
|
||||
json: true,
|
||||
};
|
||||
|
||||
request.post(authOptions, function (error, response, body) {
|
||||
if (!error && response.statusCode === 200) {
|
||||
client.spotify.setAccessToken(body.access_token);
|
||||
client.config.spotify_access_key = body.access_token
|
||||
if (client.config.devMode) console.log("- Spotify access token set -");
|
||||
} else {
|
||||
console.log("An error occured whilst getting spotify access key");
|
||||
}
|
||||
});
|
||||
};
|
69
src/funcs/handleVideo.js
Normal file
69
src/funcs/handleVideo.js
Normal file
@ -0,0 +1,69 @@
|
||||
const ytdl = require("ytdl-core");
|
||||
|
||||
module.exports = async function (
|
||||
resource,
|
||||
msg,
|
||||
voiceChannel,
|
||||
client,
|
||||
playlist,
|
||||
type,
|
||||
spotifyTrackData
|
||||
) {
|
||||
const songInfo = await ytdl.getInfo(resource).catch(err => console.log(err));
|
||||
const song = {
|
||||
title: songInfo.videoDetails.title,
|
||||
url: resource,
|
||||
author: msg.author,
|
||||
type: type,
|
||||
info: songInfo.videoDetails,
|
||||
track: spotifyTrackData
|
||||
};
|
||||
|
||||
const queue = client.queue.get(msg.guild.id);
|
||||
|
||||
if (queue) {
|
||||
queue.songs.push(song);
|
||||
queue.textChannel = msg.channel;
|
||||
if (playlist) return;
|
||||
let message;
|
||||
message = client.messages.songAdded.replace("%TITLE%", song.title);
|
||||
return msg.channel.send(message);
|
||||
}
|
||||
|
||||
const construct = {
|
||||
textChannel: msg.channel,
|
||||
voiceChannel: voiceChannel,
|
||||
connection: null,
|
||||
songs: [],
|
||||
prevSongs: [],
|
||||
volume: client.global.db.guilds[msg.guild.id].defaultVolume,
|
||||
bass: client.global.db.guilds[msg.guild.id].bass,
|
||||
nightCore: false,
|
||||
playing: false,
|
||||
paused: false,
|
||||
looping: false,
|
||||
songLooping: false,
|
||||
votes: 0,
|
||||
voters: [],
|
||||
votesNeeded: null,
|
||||
time: 0,
|
||||
endReason: null,
|
||||
exists: true
|
||||
};
|
||||
|
||||
construct.songs.push(song);
|
||||
|
||||
client.queue.set(msg.guild.id, construct);
|
||||
|
||||
try {
|
||||
const connection = await voiceChannel.join();
|
||||
construct.connection = connection;
|
||||
require("../../events/connectionEvents/handler")(client, connection);
|
||||
client.funcs.play(msg.guild, construct.songs[0], client, 0, true);
|
||||
} catch (error) {
|
||||
client.queue.delete(msg.guild.id);
|
||||
console.log(error);
|
||||
return msg.channel.send(client.messages.error + error);
|
||||
}
|
||||
return;
|
||||
};
|
17
src/funcs/msToTime.js
Normal file
17
src/funcs/msToTime.js
Normal file
@ -0,0 +1,17 @@
|
||||
module.exports = function msToTime(duration, format) {
|
||||
var seconds = Math.floor((duration / 1000) % 60),
|
||||
minutes = Math.floor((duration / (1000 * 60)) % 60),
|
||||
hours = Math.floor((duration / (1000 * 60 * 60)) % 24),
|
||||
days = Math.floor((duration / (1000 * 60 * 60 * 24)) % 24);
|
||||
|
||||
days = (days < 10) ? "0" + days : days;
|
||||
hours = (hours < 10) ? "0" + hours : hours;
|
||||
minutes = (minutes < 10) ? "0" + minutes : minutes;
|
||||
seconds = (seconds < 10) ? "0" + seconds : seconds;
|
||||
|
||||
if (format === "hh:mm:ss") {
|
||||
return `${hours}:${minutes}:${seconds}`;
|
||||
} else if (format === "dd:hh:mm:ss") {
|
||||
return `${days}:${hours}:${minutes}:${seconds}`;
|
||||
}
|
||||
}
|
94
src/funcs/play.js
Normal file
94
src/funcs/play.js
Normal file
@ -0,0 +1,94 @@
|
||||
const {
|
||||
Readable: ReadableStream
|
||||
} = require("stream");
|
||||
const ytdl = require("ytdl-core");
|
||||
const {
|
||||
streamConfig
|
||||
} = require("../config/config.js");
|
||||
const prism = require("prism-media");
|
||||
const { EmbedBuilder } = require("discord.js");
|
||||
|
||||
module.exports = async function (guild, song, client, seek, play) {
|
||||
const queue = client.queue.get(guild.id);
|
||||
if (!song) {
|
||||
queue.voiceChannel.leave();
|
||||
queue.exists = false;
|
||||
client.queue.delete(guild.id);
|
||||
return;
|
||||
}
|
||||
setTimeout(() => {
|
||||
if (!queue.playing && queue.exists) {
|
||||
queue.textChannel.send(client.messages.tookTooLong);
|
||||
queue.voiceChannel.leave();
|
||||
client.queue.delete(guild.id);
|
||||
return;
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
streamConfig.options.seek = seek;
|
||||
|
||||
let input = song.url;
|
||||
if (song.type === "ytdl" || song.type === "spotify")
|
||||
input = ytdl(song.url, streamConfig.ytdlOptions)
|
||||
//.on('info', (info, format) => console.log(format))
|
||||
.on("error", (error) => {
|
||||
console.log(error)
|
||||
queue.voiceChannel.leave();
|
||||
client.queue.delete(guild.id);
|
||||
queue.textChannel.send(client.messages.videoUnavailable)
|
||||
});
|
||||
|
||||
const ffmpegArgs = [
|
||||
"-analyzeduration",
|
||||
"0",
|
||||
"-loglevel",
|
||||
"0",
|
||||
"-f",
|
||||
"s16le",
|
||||
"-ar",
|
||||
"48000",
|
||||
"-ac",
|
||||
"2",
|
||||
"-af",
|
||||
`bass=g=${queue.bass}`,
|
||||
];
|
||||
client.funcs.sleep(500);
|
||||
if (queue.nightCore) {
|
||||
ffmpegArgs.push("-af");
|
||||
ffmpegArgs.push("asetrate=52920");
|
||||
}
|
||||
|
||||
const isStream = input instanceof ReadableStream;
|
||||
|
||||
const args = isStream ? ffmpegArgs.slice() : ["-i", input, ...ffmpegArgs];
|
||||
args.unshift("-ss", String(seek));
|
||||
|
||||
const transcoder = new prism.FFmpeg({
|
||||
args: args,
|
||||
});
|
||||
|
||||
const stream = input.pipe(transcoder).on("error", (error) => {
|
||||
console.log(error);
|
||||
});
|
||||
|
||||
const dispatcher = queue.connection.play(stream, streamConfig.options)
|
||||
|
||||
dispatcher.setVolume(queue.volume / 100);
|
||||
|
||||
require("../events/dispatcherEvents/handler")(client, dispatcher, queue, guild);
|
||||
|
||||
if ((client.global.db.guilds[guild.id].startPlaying && play) || play) {
|
||||
if (song.type !== "ytdl" && song.type !== "spotify") return;
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle(`${client.messages.startPlaying}**${song.title}**`)
|
||||
.setDescription(
|
||||
`Song duration: \`${client.funcs.msToTime(
|
||||
queue.songs[0].info.lengthSeconds * 1000,
|
||||
"hh:mm:ss"
|
||||
)}\``
|
||||
)
|
||||
.setColor(client.config.embedColor);
|
||||
queue.textChannel.send(embed);
|
||||
}
|
||||
queue.playing = true;
|
||||
};
|
19
src/funcs/saveDB.js
Normal file
19
src/funcs/saveDB.js
Normal file
@ -0,0 +1,19 @@
|
||||
module.exports = async function (client) {
|
||||
if (client.config.saveDB && !client.config.devMode) {
|
||||
//console.log('DB saved');
|
||||
client.guilds.cache.forEach((guild) => {
|
||||
client.db.collection("guilds").doc(guild.id).set({
|
||||
prefix: client.global.db.guilds[guild.id].prefix,
|
||||
defaultVolume: client.global.db.guilds[guild.id].defaultVolume,
|
||||
permissions: client.global.db.guilds[guild.id].permissions,
|
||||
dj: client.global.db.guilds[guild.id].dj,
|
||||
djrole: client.global.db.guilds[guild.id].djrole,
|
||||
startPlaying: client.global.db.guilds[guild.id].startPlaying,
|
||||
bass: client.global.db.guilds[guild.id].bass,
|
||||
blacklist: client.global.db.guilds[guild.id].blacklist,
|
||||
premium: client.global.db.guilds[guild.id].premium,
|
||||
autoPlay: client.global.db.guilds[guild.id].autoPlay
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
11
src/funcs/shuffle.js
Normal file
11
src/funcs/shuffle.js
Normal file
@ -0,0 +1,11 @@
|
||||
module.exports = function (a) {
|
||||
for (let i = a.length - 1; i > 1; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
if (i === 0 || j === 0) {
|
||||
// J or I is 0. It works like this so hands off!
|
||||
} else {
|
||||
[a[i], a[j]] = [a[j], a[i]];
|
||||
}
|
||||
}
|
||||
return a;
|
||||
};
|
3
src/funcs/sleep.js
Normal file
3
src/funcs/sleep.js
Normal file
@ -0,0 +1,3 @@
|
||||
module.exports = function (milliseconds) {
|
||||
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
||||
};
|
17
src/funcs/urlMatch.js
Normal file
17
src/funcs/urlMatch.js
Normal file
@ -0,0 +1,17 @@
|
||||
module.exports = async function (client, msg, youtube, voiceChannel, url) {
|
||||
if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
|
||||
const lmsg = await msg.channel.send(client.messages.loadingSongs);
|
||||
const playlist = await youtube.getPlaylist(url);
|
||||
const videos = await playlist.getVideos();
|
||||
for (const video of Object.values(videos)) {
|
||||
const video2 = await youtube.getVideoByID(video.id);
|
||||
await client.funcs.handleVideo(video2.url, msg, voiceChannel, client, true);
|
||||
}
|
||||
let message;
|
||||
message = client.messages.playlistAdded.replace("%TITLE%", playlist.title);
|
||||
lmsg.edit(message);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user