From 233eae84e54b09ededd6e05e21025e6f7547d94b Mon Sep 17 00:00:00 2001 From: edmilson-dk Date: Sun, 24 Oct 2021 12:26:32 -0300 Subject: [PATCH] feat: finished get actual music playing --- data/musics-infos.json | 45 +++++++++++++++++++++++++++++++++++-- src/constants/index.ts | 3 +++ src/core/discord-music.ts | 19 +++++++++------- src/core/sockets-manager.ts | 7 ++---- src/index.ts | 9 +++++++- src/sockets/index.ts | 2 +- 6 files changed, 68 insertions(+), 17 deletions(-) diff --git a/data/musics-infos.json b/data/musics-infos.json index 2ce5d7f..c35c38c 100644 --- a/data/musics-infos.json +++ b/data/musics-infos.json @@ -1,3 +1,44 @@ { - "musics": [] -} + "musics": [ + { + "id": "379353fb-1b82-4aef-91e6-87fd36b75e06", + "name": "Alok, Bruno Martini feat. Zeeba - Hear Me Now (Official Music Video)", + "artist": "Alok", + "url": "https://www.youtube.com/watch?v=JVpTp8IHdEg", + "position": 0, + "createdAt": "2021-10-16T20:15:12.471Z", + "updatedAt": "2021-10-16T20:15:12.471Z", + "duration": 265 + }, + { + "id": "7a28b1fe-6e16-4263-96c8-cff0e5e0b3e3", + "name": "Hozier - Take Me To Church (Official Video)", + "artist": "Hozier", + "url": "https://www.youtube.com/watch?v=PVjiKRfKpPI", + "position": 1, + "createdAt": "2021-10-16T22:18:38.464Z", + "updatedAt": "2021-10-16T22:18:38.464Z", + "duration": 257 + }, + { + "id": "790722db-3bf1-4cbe-a930-f9cb51681088", + "name": "Numb [Official Music Video] - Linkin Park", + "artist": "Linkin Park", + "url": "https://www.youtube.com/watch?v=kXYiU_JCYtU", + "position": 2, + "createdAt": "2021-10-16T22:20:55.624Z", + "updatedAt": "2021-10-16T22:20:55.624Z", + "duration": 187 + }, + { + "id": "e5a00e87-150b-46b8-a2db-102bff22af14", + "name": "Alan Walker - Faded", + "artist": "Alan Walker", + "url": "https://www.youtube.com/watch?v=60ItHLz5WEA&list=PLiSzxQJ4pCKzj_rCvdJ5xRIzq02ROG5bh&index=4", + "position": 3, + "createdAt": "2021-10-19T19:10:19.508Z", + "updatedAt": "2021-10-19T19:10:19.508Z", + "duration": 213 + } + ] +} \ No newline at end of file diff --git a/src/constants/index.ts b/src/constants/index.ts index c20ccb2..d748d8d 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -3,4 +3,7 @@ export const SOCKET_EVENTS = { droppedMusic: "droppedMusic", addedNewMusic: "addedNewMusic", updateServerMusics: "update_server_musics", + musicIsPlaying: "music_is_playing", + getActualMusicPlaying: "get_actual_music_playing", + loopMusicIsPlaying: "loop_music_is_playing", }; diff --git a/src/core/discord-music.ts b/src/core/discord-music.ts index 4e6dd58..14d97a8 100644 --- a/src/core/discord-music.ts +++ b/src/core/discord-music.ts @@ -1,16 +1,22 @@ -import Discord, { VoiceBroadcast } from "discord.js"; +import Discord, { MessageEmbed, VoiceBroadcast } from "discord.js"; import fs from "fs"; import { logger } from "../helpers/logger"; -import { socket } from "../sockets"; import { MusicInfoType } from "../types"; import { ManagerData } from "./manager-data"; +import { SocketsManager } from "./sockets-manager"; export class DiscordMusic { - private readonly managerData: ManagerData = new ManagerData(); - private readonly messageEmbed = new Discord.MessageEmbed(); + private readonly managerData: ManagerData; + private readonly socketManager: SocketsManager; + private readonly messageEmbed: MessageEmbed = new Discord.MessageEmbed(); private actualMusic: MusicInfoType = {} as MusicInfoType; + constructor(managerData: ManagerData, socketManager: SocketsManager) { + this.managerData = managerData; + this.socketManager = socketManager; + } + getMusics() { return this.managerData.getMusicsInfos(); } @@ -30,6 +36,7 @@ export class DiscordMusic { playMusic(broadcast: VoiceBroadcast, musicIndex: number) { const actualSongFile = this.getActualMusicFile(musicIndex); + this.socketManager.emitMusicPlaying(this.actualMusic); logger.info(`Playing ${actualSongFile}`); const stream = fs.createReadStream(`./musics/${actualSongFile}`); @@ -40,10 +47,6 @@ export class DiscordMusic { const musics = this.getMusics(); const index = musics[musicIndex + 1] ? musicIndex + 1 : 0; - // if (index === musics.length - 1) { - // socket.emit("updated_musics", index); - // } - this.playMusic(broadcast, index); }); } diff --git a/src/core/sockets-manager.ts b/src/core/sockets-manager.ts index c86cde6..aa4097b 100644 --- a/src/core/sockets-manager.ts +++ b/src/core/sockets-manager.ts @@ -21,11 +21,8 @@ export class SocketsManager { this.managerSystem.onMusicRemoved(data); } - public emitUpdateMusicsList() { - return () => { - console.log("chamou"); - socket.emit(SOCKET_EVENTS.updateServerMusics, { teste: true }); - }; + public emitMusicPlaying(data: MusicInfoType) { + socket.emit(SOCKET_EVENTS.musicIsPlaying, data); } onEvents() { diff --git a/src/index.ts b/src/index.ts index f9b44c6..6bde2dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,8 @@ import { ManagerSystem } from "./core/manager-system"; import { SocketsManager } from "./core/sockets-manager"; import { GenericCommands } from "./core/generic-commands"; import { ManagerCronsJobs } from "./core/manager-crons-jobs"; +import { socket } from "./sockets"; +import { ManagerData } from "./core/manager-data"; const client = new Client({ retryLimit: 3, @@ -21,16 +23,21 @@ const TOKEN = process.env.BOT_SECRET_TOKEN; const discordServers = new DiscordServers(); const serverEvents = new ServerEvents(); const managerSystem = new ManagerSystem(); -const discordMusic = new DiscordMusic(); const socketsManager = new SocketsManager(managerSystem); const genericCommands = new GenericCommands(); const jobsManager = new ManagerCronsJobs(); +const managerData = new ManagerData(); +const discordMusic = new DiscordMusic(managerData, socketsManager); client.once("ready", () => { console.log("Ready!"); managerSystem.onBotStart(); socketsManager.onEvents(); jobsManager.startUpdatedMusicsJob(); + + socket.on("teste", (data) => { + console.log(data); + }); }); const broadcast = client.voice?.createBroadcast(); diff --git a/src/sockets/index.ts b/src/sockets/index.ts index 541b8d8..778381a 100644 --- a/src/sockets/index.ts +++ b/src/sockets/index.ts @@ -1,3 +1,3 @@ -import { io } from "socket.io-client"; +import io from "socket.io-client"; export const socket = io(process.env.BASE_SOCKET_URL as string);