Version 0.6.0 - Ajout des playlists

This commit is contained in:
Raphix
2023-08-28 23:01:37 +02:00
parent e34134537e
commit d5651f8cef
14 changed files with 861 additions and 123 deletions

View File

@ -16,7 +16,8 @@ const __glob = {
PACKAGE: root + path.sep + "package.json",
DATA: root + path.sep + "data" + path.sep,
NODES: root + path.sep + "data" + path.sep + "nodes.json",
README: root + path.sep + "README.md"
README: root + path.sep + "README.md",
PLAYLIST: root + path.sep + "data" + path.sep + "playlist.json",
};
const webroot = __glob.WEB_DIR + path.sep

View File

@ -4,7 +4,6 @@ const { LogType } = require("./sub-log");
var { List } = require("./sub-list")
const discord = require("./discord-bot")
var ytfps = require("ytfps");
const { use } = require("../web/routes/login");
const packageJson = require(__glob.PACKAGE);
@ -656,7 +655,7 @@ module.exports.updateMusicState = function (client, action) {
data["isOnline"] = false
}
clog.log("Actualisation de tous les clients - Titre : " + currentTitle)
clog.log("Actualisation de tous les clients - Titre : " + currentTitle + " - Loop : " + data.loop + " - Shuffle : " + data.shuffle + " - Playing : " + data.playing + " - Volume : " + Math.trunc(data.volume / 10) )
return data

103
src/modules/sub-playlist.js Normal file
View File

@ -0,0 +1,103 @@
const { SlashCommandBuilder, EmbedBuilder, DefaultWebSocketManagerOptions, discordSort } = require("discord.js");
const { __glob } = require("../modules/global-variables");
const { LogType } = require("./sub-log");
var { List } = require("./sub-list")
const discord = require("./discord-bot")
const subplayer = require("./sub-player")
const fs = require("fs")
var playlists = {}
const plog = new LogType("Playlist-Manager")
check()
module.exports.getUser = function (id) {
check()
if(!playlists[id]) {
plog.log("Ajout de l'utilisateur \"" + id + "\" dans la base de donnée Playlist !")
playlists[id] = {}
fs.writeFileSync(__glob.PLAYLIST, JSON.stringify(playlists, null, 2))
return playlists[id]
} else {
plog.log("L'utilisateur \"" + id + "\" existe déjà dans la base de donnée Playlist !")
return playlists[id]
}
}
module.exports.addPlaylist = function (id, name) {
check()
if(!playlists[id][name]) {
plog.log("Ajout de la playlist à l'utilisateur \"" + id + "\" dans la base de donnée Playlist !")
playlists[id][name] = []
fs.writeFileSync(__glob.PLAYLIST, JSON.stringify(playlists, null, 2))
} else {
plog.log("L'utilisateur \"" + id + "\" à déjà une playlist avec le nom "+ name + " dans la base de donnée Playlist !")
}
}
module.exports.removePlaylist = function (id, name) {
check()
if(playlists[id][name]) {
plog.log("Supression de la playlist à l'utilisateur \"" + id + "\" dans la base de donnée Playlist !")
delete playlists[id][name]
fs.writeFileSync(__glob.PLAYLIST, JSON.stringify(playlists, null, 2))
} else {
plog.log("L'utilisateur \"" + id + "\" n'a pas une playlist avec le nom "+ name + " dans la base de donnée Playlist !")
}
}
module.exports.addSong = function (id, name, song) {
check()
if(playlists[id][name]) {
plog.log("Ajout d'une chanson dans la playlist '" + name + "' à l'utilisateur \"" + id + "\" dans la base de donnée Playlist !")
playlists[id][name].push(song)
fs.writeFileSync(__glob.PLAYLIST, JSON.stringify(playlists, null, 2))
} else {
plog.log("L'utilisateur \"" + id + "\" n'a pas une playlist avec le nom "+ name + " dans la base de donnée Playlist !")
}
}
module.exports.removeSong = function (id, name, song ) {
check()
if(playlists[id][name]) {
plog.log("Supression d'une chanson dans la playlist '" + name + "' à l'utilisateur \"" + id + "\" dans la base de donnée Playlist !")
playlists[id][name].splice(playlists[id][name].indexOf(song), 1)
fs.writeFileSync(__glob.PLAYLIST, JSON.stringify(playlists, null, 2))
} else {
plog.log("L'utilisateur \"" + id + "\" n'a pas une playlist avec le nom "+ name + " dans la base de donnée Playlist !")
}
}
function check() {
if(fs.existsSync(__glob.PLAYLIST)) {
playlists = JSON.parse(fs.readFileSync(__glob.PLAYLIST))
} else {
fs.writeFileSync(__glob.PLAYLIST, JSON.stringify(playlists, null, 2))
}
}

View File

@ -1,5 +1,5 @@
const internal = require("stream");
const { __glob, __web } = require("../modules/global-variables");
const { __glob, __web } = require("./global-variables");
const { LogType } = require("./sub-log");
const log = require("./sub-log");
const auth = require("./sub-auth");
@ -7,6 +7,9 @@ const cook = require("cookie")
const wlog = new LogType("Web")
const subplayer = require(__glob.SUBPLAYER);
const { List } = require("./sub-list")
const subplaylist = require("./sub-playlist")
module.exports.WebServer = class {
constructor() {
@ -196,6 +199,20 @@ function IOConnection(io) {
io.sockets.emit("/ALWAYS/MUSIC_STATE", data)
})
GetRequest(io, socket, "PLAYLIST", () => {
var cookies = socket.handshake.headers.cookie
cookies = cook.parse(cookies)
var token = cookies.token
const user = auth.getUser(token)
const playlistResult = subplaylist.getUser(user.user.id)
socket.emit("ANSWER/GET/PLAYLIST", playlistResult)
})
@ -243,6 +260,15 @@ function IOConnection(io) {
})
GetRequest(io, socket, "RESTART", () => {
const pm2 = require('pm2');
pm2.restart('SubSonics - Bot Discord')
})
GetRequest(io, socket, "SPECIAL/MJ", () => {
@ -502,6 +528,152 @@ function IOConnection(io) {
})
socket.on("SEND/CREATE_PLAYLIST", async (data) => {
var cookies = socket.handshake.headers.cookie
if(cookies) {
cookies = cook.parse(cookies)
var token = cookies.token
if(auth.checkUser(token)) {
var user = auth.getUser(token)
var userId = user.user.id
subplaylist.addPlaylist(userId, data)
io.emit("DO_UPDATE_PLAYLIST")
io.emit("ANSWER/SEND/CREATE_PLAYLIST/OK")
} else {
io.emit("ANSWER/SEND/CREATE_PLAYLIST", {"error":"USER_DONT_EXIST"})
}
} else {
io.emit("ANSWER/SEND/CREATE_PLAYLIST", {"error":"TOKEN_NOT_FINDED"})
}
})
socket.on("SEND/DELETE_PLAYLIST", async (data) => {
var cookies = socket.handshake.headers.cookie
if(cookies) {
cookies = cook.parse(cookies)
var token = cookies.token
if(auth.checkUser(token)) {
var user = auth.getUser(token)
var userId = user.user.id
subplaylist.removePlaylist(userId, data)
io.emit("DO_UPDATE_PLAYLIST")
io.emit("ANSWER/SEND/DELETE_PLAYLIST/OK")
} else {
io.emit("ANSWER/SEND/DELETE_PLAYLIST", {"error":"USER_DONT_EXIST"})
}
} else {
io.emit("ANSWER/SEND/DELETE_PLAYLIST", {"error":"TOKEN_NOT_FINDED"})
}
})
socket.on("SEND/ADD_SONG_TO_PLAYLIST", async (data, song) => {
var cookies = socket.handshake.headers.cookie
if(cookies) {
cookies = cook.parse(cookies)
var token = cookies.token
if(auth.checkUser(token)) {
var user = auth.getUser(token)
var userId = user.user.id
subplaylist.addSong(userId, data, song)
io.emit("DO_UPDATE_PLAYLIST")
io.emit("ANSWER/SEND/ADD_SONG_TO_PLAYLIST/OK")
} else {
io.emit("ANSWER/SEND/ADD_SONG_TO_PLAYLIST", {"error":"USER_DONT_EXIST"})
}
} else {
io.emit("ANSWER/SEND/ADD_SONG_TO_PLAYLIST", {"error":"TOKEN_NOT_FINDED"})
}
})
socket.on("SEND/DELETE_SONG_TO_PLAYLIST", async (data, song) => {
var cookies = socket.handshake.headers.cookie
if(cookies) {
cookies = cook.parse(cookies)
var token = cookies.token
if(auth.checkUser(token)) {
var user = auth.getUser(token)
var userId = user.user.id
subplaylist.removeSong(userId, data, song)
io.emit("DO_UPDATE_PLAYLIST")
io.emit("ANSWER/SEND/DELETE_SONG_TO_PLAYLIST/OK")
} else {
io.emit("ANSWER/SEND/DELETE_SONG_TO_PLAYLIST", {"error":"USER_DONT_EXIST"})
}
} else {
io.emit("ANSWER/SEND/DELETE_SONG_TO_PLAYLIST", {"error":"TOKEN_NOT_FINDED"})
}
})
@ -528,6 +700,8 @@ function GetRequest (io, socket, name, func) {
if(auth.checkUser(token)) {
const user = auth.getUser(token)
wlog.log("Requête de " + user.user.username + " avec l'information \"" + name + "\"")
func()
} else {