From 9a2c522d5e5a9302e45d98fd98082cefe0b0e946 Mon Sep 17 00:00:00 2001 From: FAYStarNext Date: Tue, 10 Sep 2024 11:01:47 +0000 Subject: [PATCH] Refactor event listener and command handling in index.ts --- example/src/index.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/example/src/index.ts b/example/src/index.ts index 362024c..e5c1509 100644 --- a/example/src/index.ts +++ b/example/src/index.ts @@ -37,7 +37,7 @@ manager.on("NodeConnect", (node) => { manager.on("PlayerCreate", (player) => { console.log(`Player created in guild ${player.guild}`); }); -manager.on("NodeError" , (node, error) => { +manager.on("NodeError", (node, error) => { console.log(`Node ${node.options.host} has an error: ${error.message}`); }); @@ -45,32 +45,32 @@ manager.on("NodeError" , (node, error) => { async function handlePlayCommand(message: any, args: string[]) { if (!message.member?.voice.channel) return message.reply('you need to join a voice channel.'); if (!args.length) return message.reply('you need to give me a URL or a search term.'); - + const search = args.join(' '); const start = performance.now(); let res; let end; - + try { res = await searchTracks(search); end = `Time took: ${Math.round(performance.now() - start)}ms.`; } catch (err) { return message.reply(`there was an error while searching: ${err}`); } - + if (res.loadType === 'error') return message.reply('there was no tracks found with that query.'); - + const player = manager.create({ guild: message.guild?.id as string, voiceChannel: message.member?.voice.channel.id, textChannel: message.channel.id, volume: 100, }); - + player.connect(); player.queue.add(res.tracks[0]); if (!player.playing && !player.paused && !player.queue.size) player.play(); - + return message.reply(`enqueuing ${res.tracks[0].title}. ${end}`); } @@ -84,19 +84,20 @@ async function searchTracks(search: string) { client.on("messageCreate", async (message) => { if (message.author.bot) return; - + const [command, ...args] = message.content.slice(0).split(/\s+/g); switch (command) { case 'play': return handlePlayCommand(message, args); - case 'moveNode': + case 'moveNode': { let player = manager.players.get(message.guild?.id as string); player?.moveNode(args[0]); return message.reply(`Node ${player?.node.options.identifier} moved to ${args[0]}`); + } } }); client.on("raw", (data) => manager.updateVoiceState(data)); -client.on("ready" , () => { +client.on("ready", () => { manager.init(client.user?.id as string); console.log(`Logged in as ${client.user?.tag} | Memory usage: ${Math.round(process.memoryUsage().heapUsed / 1024 / 1024)}MB`); });