Skip to content

Commit

Permalink
Merge pull request #35 from EvarinDev/dev
Browse files Browse the repository at this point in the history
Refactor event listener and command handling in index.ts
  • Loading branch information
EvarinDev authored Sep 10, 2024
2 parents 7384e9e + 9a2c522 commit be68647
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,40 @@ 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}`);
});

// Helper function to handle the 'play' command
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}`);
}

Expand All @@ -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`);
});
Expand Down

0 comments on commit be68647

Please sign in to comment.