royal-selfbot
Version:
A Discord self-bot library. USE WITH EXTREME CAUTION - AGAINST DISCORD TOS.
99 lines (80 loc) • 4.07 kB
JavaScript
// examples/voice.js
const { Client } = require('royal-selfbot');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus, NoSubscriberBehavior } = require('@discordjs/voice');
const { OpusEncoder } = require('@discordjs/opus'); // May need npm install @discordjs/opus
// const prism = require('prism-media'); // For more complex audio streaming
const client = new Client();
const TARGET_GUILD_ID = 'YOUR_SERVER_ID'; // Replace with the Guild ID
const TARGET_VOICE_CHANNEL_ID = 'YOUR_VOICE_CHANNEL_ID'; // Replace with the Voice Channel ID
client.on('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
const guild = client.guilds.cache.get(TARGET_GUILDID);
if (!guild) {
console.error(`Guild ${TARGET_GUILD_ID} not found in cache.`);
return;
}
const channel = guild.channels.cache.get(TARGET_VOICE_CHANNEL_ID);
if (!channel || !channel.isVoice()) { // Assuming isVoice() method exists
console.error(`Voice channel ${TARGET_VOICE_CHANNEL_ID} not found or not a voice channel.`);
return;
}
console.log(`Attempting to join voice channel: ${channel.name} in guild: ${guild.name}`);
try {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: guild.id,
adapterCreator: guild.voiceAdapterCreator, // This needs to be provided by RoyalSelfbot's Guild structure
selfDeaf: false, // User accounts usually can't self-deafen via API
selfMute: false, // User accounts usually can't self-mute via API
});
console.log(`Successfully joined voice channel ${channel.name}!`);
// --- Example: Play Silence (or simple audio) ---
const player = createAudioPlayer({
behaviors: {
// Play silence when nothing else is playing
noSubscriber: NoSubscriberBehavior.Play,
},
});
// Subscribe the connection to the player
connection.subscribe(player);
console.log('Audio player created and subscribed.');
// Create a dummy resource (e.g., silence) - more complex audio needs ffmpeg/prism
// This part is tricky and might require more setup depending on the audio source
// const resource = createAudioResource(/* Your audio stream/file here */);
// player.play(resource);
player.on(AudioPlayerStatus.Idle, () => {
console.log('Player is idle.');
// Optionally disconnect after playing or inactivity
// connection.destroy();
});
player.on('error', error => {
console.error(`Audio Player Error: ${error.message}`);
connection.destroy(); // Destroy connection on player error
});
connection.on('stateChange', (oldState, newState) => {
console.log(`Connection transitioned from ${oldState.status} to ${newState.status}`);
if (newState.status === 'disconnected') {
console.log('Voice connection disconnected.');
// Handle reconnection or cleanup
try {
player.stop(); // Stop the player if it's running
connection.destroy(); // Ensure connection is fully destroyed
} catch (e) {
console.error('Error during disconnection cleanup:', e);
}
}
});
// Keep the script running, or implement logic to disconnect later
// Example: Disconnect after 30 seconds
// setTimeout(() => {
// if (connection.state.status !== 'destroyed') {
// console.log('Disconnecting after timeout...');
// connection.destroy();
// }
// }, 30000);
} catch (error) {
console.error('Error joining voice channel:', error);
}
});
client.on('error', console.error);
client.login('DISCORD_USER_TOKEN');