const { SlashCommandBuilder } = require('discord.js');
const {
joinVoiceChannel,
createAudioPlayer,
createAudioResource,
AudioPlayerStatus,
VoiceConnectionStatus,
entersState,
} = require('@discordjs/voice');
const { join } = require('node:path');
const fs = require('node:fs');
module.exports = {
data: new SlashCommandBuilder()
.setName('nya')
.setDescription('test me nya!'),
async execute(interaction) {
const channel = interaction.member.voice.channel;
if (!channel) {
return interaction.reply({ content: 'ā You must be in a voice channel!', ephemeral: true });
}
await interaction.reply('Nyaa~ š±');
// Join voice channel
console.log('š§ Connecting to voice channel...');
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
// selfDeaf: false,
});
connection.on('stateChange', (oldState, newState) => {
console.log(`š Voice connection: ${oldState.status} -> ${newState.status}`);
});
// Try to wait until connection is ready (handle timeout gracefully)
try {
await entersState(connection, VoiceConnectionStatus.Ready, 15_000);
console.log('ā
Voice connection is ready!');
} catch (error) {
console.warn('ā ļø Voice connection is taking too long. Continuing anyway...');
}
// Prepare audio file
const filePath = join(__dirname, 'nya.wav');
console.log('š Resolved audio path:', filePath);
if (!fs.existsSync(filePath)) {
console.error('ā Audio file not found:', filePath);
return interaction.followUp({ content: 'ā Audio file not found.', ephemeral: true });
}
const resource = createAudioResource(filePath, { inlineVolume: true });
resource.volume.setVolume(0.5);
// Create player
const player = createAudioPlayer();
player.on('stateChange', (oldState, newState) => {
console.log(`šµ Audio player: ${oldState.status} -> ${newState.status}`);
if (newState.status === AudioPlayerStatus.AutoPaused) {
console.warn('ā ļø Audio auto-paused! Probably alone in voice channel.');
const members = channel.members.filter(member => !member.user.bot);
console.log(`š„ Non-bot members in VC: ${members.size}`);
}
if (newState.status === AudioPlayerStatus.Idle && oldState.status !== AudioPlayerStatus.Idle) {
console.log('š Playback finished. Leaving voice channel.');
connection.destroy();
}
});
player.on(AudioPlayerStatus.Playing, () => {
console.log('ā
Audio is playing!');
});
player.on('error', error => {
console.error('ā Audio player error:', error.message);
});
connection.subscribe(player);
console.log('ā¶ļø Subscribed to player. Starting playback...');
player.play(resource);
},
};
const { SlashCommandBuilder } = require('discord.js');
const {
joinVoiceChannel,
createAudioPlayer,
createAudioResource,
AudioPlayerStatus,
VoiceConnectionStatus,
entersState,
} = require('@discordjs/voice');
const { join } = require('node:path');
const fs = require('node:fs');
module.exports = {
data: new SlashCommandBuilder()
.setName('nya')
.setDescription('test me nya!'),
async execute(interaction) {
const channel = interaction.member.voice.channel;
if (!channel) {
return interaction.reply({ content: 'ā You must be in a voice channel!', ephemeral: true });
}
await interaction.reply('Nyaa~ š±');
// Join voice channel
console.log('š§ Connecting to voice channel...');
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
// selfDeaf: false,
});
connection.on('stateChange', (oldState, newState) => {
console.log(`š Voice connection: ${oldState.status} -> ${newState.status}`);
});
// Try to wait until connection is ready (handle timeout gracefully)
try {
await entersState(connection, VoiceConnectionStatus.Ready, 15_000);
console.log('ā
Voice connection is ready!');
} catch (error) {
console.warn('ā ļø Voice connection is taking too long. Continuing anyway...');
}
// Prepare audio file
const filePath = join(__dirname, 'nya.wav');
console.log('š Resolved audio path:', filePath);
if (!fs.existsSync(filePath)) {
console.error('ā Audio file not found:', filePath);
return interaction.followUp({ content: 'ā Audio file not found.', ephemeral: true });
}
const resource = createAudioResource(filePath, { inlineVolume: true });
resource.volume.setVolume(0.5);
// Create player
const player = createAudioPlayer();
player.on('stateChange', (oldState, newState) => {
console.log(`šµ Audio player: ${oldState.status} -> ${newState.status}`);
if (newState.status === AudioPlayerStatus.AutoPaused) {
console.warn('ā ļø Audio auto-paused! Probably alone in voice channel.');
const members = channel.members.filter(member => !member.user.bot);
console.log(`š„ Non-bot members in VC: ${members.size}`);
}
if (newState.status === AudioPlayerStatus.Idle && oldState.status !== AudioPlayerStatus.Idle) {
console.log('š Playback finished. Leaving voice channel.');
connection.destroy();
}
});
player.on(AudioPlayerStatus.Playing, () => {
console.log('ā
Audio is playing!');
});
player.on('error', error => {
console.error('ā Audio player error:', error.message);
});
connection.subscribe(player);
console.log('ā¶ļø Subscribed to player. Starting playback...');
player.play(resource);
},
};