trixxy-youtube
v1.0.0
Published
A package for playing YouTube audio in Discord bots.
Readme
trixxy-youtube
Installation
npm install trixxy-youtubeDependencies
This package relies on the following npm packages:
discord.js@discordjs/voiceytdl-coreytsr
Make sure these are installed in your project.
Usage
Here's how to use the TrixxyYouTube class in your Discord bot:
const { Client, GatewayIntentBits } = require('discord.js');
const TrixxyYouTube = require('trixxy-youtube');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
],
});
const youtubePlayer = new TrixxyYouTube();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'play') {
await interaction.deferReply();
const query = interaction.options.getString('query');
try {
await youtubePlayer.addAndPlay(query, interaction);
} catch (error) {
console.error(error);
await interaction.followUp(`There was an error trying to play music: ${error.message}`);
}
} else if (commandName === 'skip') {
await interaction.deferReply();
try {
await youtubePlayer.skip(interaction);
} catch (error) {
console.error(error);
await interaction.followUp(`There was an error trying to skip music: ${error.message}`);
}
} else if (commandName === 'stop') {
await interaction.deferReply();
try {
await youtubePlayer.stop(interaction);
} catch (error) {
console.error(error);
await interaction.followUp(`There was an error trying to stop music: ${error.message}`);
}
}
});
client.login('YOUR_BOT_TOKEN');
// Example of how to register slash commands (run once)
// This is a simplified example, you should register your commands properly
/*
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const commands = [
{
name: 'play',
description: 'Plays a song from YouTube.',
options: [
{
name: 'query',
type: 3, // STRING
description: 'The YouTube URL or search query.',
required: true,
},
],
},
{
name: 'skip',
description: 'Skips the current song.',
},
{
name: 'stop',
description: 'Stops the music and clears the queue.',
},
];
const rest = new REST({ version: '9' }).setToken('YOUR_BOT_TOKEN');
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands('YOUR_CLIENT_ID', 'YOUR_GUILD_ID'),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
*/TrixxyYouTube Class Methods
constructor()
Initializes the YouTube player, queue, and voice connection. Sets up event listeners for idle and error states of the audio player.
join(member)
Joins the voice channel of the provided GuildMember.
member: TheGuildMemberobject whose voice channel to join.
Throws: Error if the member is not in a voice channel.
play(url, interaction)
Plays a YouTube audio stream from the given URL. Automatically joins the voice channel if not already connected.
url: The YouTube video URL to play.interaction: The DiscordInteractionobject to send follow-up messages.
addAndPlay(query, interaction)
Adds a song to the queue. If the query is not a valid YouTube URL, it will search YouTube for the query and add the first result. If nothing is currently playing, it will start playback immediately.
query: A YouTube URL or a search query (e.g., "Never Gonna Give You Up").interaction: The DiscordInteractionobject to send follow-up messages.
skip(interaction)
Skips the current song and plays the next one in the queue. If only one song is in the queue, it will stop playback.
interaction: The DiscordInteractionobject to send follow-up messages.
stop(interaction)
Stops the current playback, clears the entire queue, and disconnects from the voice channel.
interaction: The DiscordInteractionobject to send follow-up messages.
