Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor event listener and command handling in index.ts #35

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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