Skip to content

Commit

Permalink
Update songs list when files change
Browse files Browse the repository at this point in the history
  • Loading branch information
Babbiorsetto committed Jul 24, 2022
1 parent 0c61fba commit 74f7f3c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 36 deletions.
41 changes: 6 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"@discordjs/opus": "^0.8.0",
"@discordjs/voice": "^0.11.0",
"chokidar": "^3.5.3",
"discord.js": "^14.0.3",
"ffmpeg-static": "^4.4.1",
"libsodium-wrappers": "^0.7.10"
Expand Down
17 changes: 16 additions & 1 deletion src/PlayNowApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import {DISCORD_BOT_TOKEN} from "./config";
import {SongList} from "./entities/SongList"
import {FileSystemSongListBuilder} from "./builders/SongListBuilder"
import { AudioPlayer } from "@discordjs/voice";
import chokidar from "chokidar";

export class PlayNowApplication {
private client: Client;
public static commandDir = path.join(__dirname, "commands");
public static songsDir = path.join(process.cwd(), "songs");
private commands: Map<string, Command>;
private songList: SongList;
private audioPlayers: Map<Snowflake, AudioPlayer>;
Expand All @@ -22,7 +24,7 @@ export class PlayNowApplication {
GatewayIntentBits.GuildVoiceStates
]
});
this.songList = new SongList(new FileSystemSongListBuilder(path.join(process.cwd(), "songs")));
this.songList = new SongList(new FileSystemSongListBuilder(PlayNowApplication.songsDir));
this.commands = new Map<string, Command>();
this.audioPlayers = new Map();
}
Expand Down Expand Up @@ -82,5 +84,18 @@ export class PlayNowApplication {
}

this.client.on("interactionCreate", handleInteraction.bind(this));

async function onSongsChange(change) {
this.songList.rebuild();
console.log(`Song directory change detected ${(path.basename(change))}`)
}
chokidar.watch(PlayNowApplication.songsDir, {
awaitWriteFinish: true,
usePolling: true,
interval: 1000,
})
.on("add", onSongsChange.bind(this))
.on("change", onSongsChange.bind(this))
.on("unlink", onSongsChange.bind(this))
}
}

0 comments on commit 74f7f3c

Please sign in to comment.