performanc / voice Goto Github PK
View Code? Open in Web Editor NEWPerformanC's Discord Voice API client.
License: BSD 2-Clause "Simplified" License
PerformanC's Discord Voice API client.
License: BSD 2-Clause "Simplified" License
1.0.5
Using this on Windows makes the bot sound lag while there's no issue doing the same thing in djs/voice
Try the code provided below and these packages :
Using this package :
import { Client, GatewayIntentBits } from 'discord.js';
import perfcVoice from '@performanc/voice';
import prism from 'prism-media';
import ytstream from 'yt-stream';
import config from './config.js';
const ffmpeg = new prism.FFmpeg({
args: [
'-loglevel', '0',
'-analyzeduration', '0',
'-hwaccel', 'auto',
'-threads', 2,
'-filter_threads', 2,
'-filter_complex_threads', 2,
'-i', '-',
'-f', 's16le',
'-ar', '48000',
'-ac', '2',
'-crf', '0'
]
});
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });
const connection = perfcVoice.joinVoiceChannel({ guildId: config.guild_id, userId: config.bot_user_id, encryption: 'xsalsa20_poly1305_lite' });
client.on('raw', (data) => {
switch (data.t) {
case 'VOICE_STATE_UPDATE': {
if (data.d.guild_id == config.guild_id && data.d.member?.user.id == config.bot_user_id) {
connection.voiceStateUpdate({
session_id: data.d.session_id
});
}
break;
}
case 'VOICE_SERVER_UPDATE': {
if (data.d.guild_id == config.guild_id) {
connection.voiceServerUpdate({
token: data.d.token,
endpoint: data.d.endpoint
});
connection.connect();
}
break;
}
}
});
connection.on('stateChange', async (oldState, newState) => {
if (newState.status == 'ready') {
//console.log('Voice connection ready');
const stream = await ytstream.stream(`https://www.youtube.com/watch?v=dQw4w9WgXcQ`, {
quality: 'high',
type: 'audio',
highWaterMark: 1048576 * 32,
download: true
});
const result = stream.stream.pipe(ffmpeg).pipe(
new prism.opus.Encoder({
rate: 48000,
channels: 2,
frameSize: 960
}
));
connection.play(result);
}
});
client.on('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
const channel = client.channels.cache.get(config.channel_id);
client.guilds.cache.get(config.guild_id).shard.send(
{
op: 4,
d: {
guild_id: channel.guildId,
channel_id: channel.id,
self_mute: false,
self_deaf: false
}
});
});
client.login(config.token);
Using djs/voice :
import { Client, GatewayIntentBits } from 'discord.js';
import { joinVoiceChannel, createAudioPlayer, createAudioResource, NoSubscriberBehavior } from '@discordjs/voice';
import config from './config.js';
import ytstream from 'yt-stream';
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });
client.on('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
const channel = client.channels.cache.get(config.channel_id);
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
const player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Play,
},
});
const stream = await ytstream.stream(`https://www.youtube.com/watch?v=dQw4w9WgXcQ`, {
quality: 'high',
type: 'audio',
highWaterMark: 1048576 * 32,
download: true
});
const resource = createAudioResource(stream.stream);
connection.subscribe(player);
player.play(resource);
});
client.login(config.token);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.