Skip to content

Commit

Permalink
Merge pull request #8 from Code-Sounds/feature/manager_bot_realtime
Browse files Browse the repository at this point in the history
feat: finished get actual music playing
  • Loading branch information
edmilson-dk authored Oct 24, 2021
2 parents 44143b3 + 233eae8 commit ce8e0d6
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 17 deletions.
45 changes: 43 additions & 2 deletions data/musics-infos.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
3 changes: 3 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
19 changes: 11 additions & 8 deletions src/core/discord-music.ts
Original file line number Diff line number Diff line change
@@ -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();
}
Expand All @@ -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}`);
Expand All @@ -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);
});
}
Expand Down
7 changes: 2 additions & 5 deletions src/core/sockets-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/sockets/index.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit ce8e0d6

Please sign in to comment.