added rzeczy

This commit is contained in:
Patryk Koreń
2025-12-26 20:45:38 +01:00
parent 375779de9c
commit b551cce62a
14 changed files with 326 additions and 91 deletions

View File

@@ -1,51 +1,65 @@
import { spawn } from "node:child_process";
import { DATA_DIR } from "../main";
export async function get_audio(url: string): Promise<string> {
const ytDlpBin = process.env.YT_DLP_BIN_PATH! ?? "yt-dlp";
const dataDir = "./data";
export async function getAudioFile(url: string): Promise<string> {
const id = extractId(url)
const idMatch = /[?&]v=([a-zA-Z0-9_-]{11})/.exec(url);
if (!idMatch) throw new Error("Cannot extract video ID");
const id = idMatch[1];
console.log(`ID: ${id}`);
// ---- run yt-dlp ----
await new Promise<void>((resolve, reject) => {
const p = spawn(ytDlpBin, [
"-x",
"--audio-format", "opus",
"--audio-quality", "0",
"--no-playlist",
"--paths", dataDir,
url,
]);
p.stderr.on("data", d => process.stderr.write(d));
p.on("close", code => code === 0 ? resolve() : reject(new Error("yt-dlp failed")));
});
// ---- find the file ----
return await new Promise<string>((resolve, reject) => {
const find = spawn("find", [
dataDir,
"-type", "f",
"-iname", `*${id}*.opus`,
"-exec", "readlink", "-f", "{}", ";",
]);
let out = "";
find.stdout.on("data", d => out += d.toString());
find.stderr.on("data", d => process.stderr.write(d));
find.on("close", code => {
if (code !== 0 || !out.trim()) {
reject(new Error("Audio file not found"));
} else {
resolve(out.trim());
}
});
});
let path: string;
try {
path = await findFileById(id)
} catch (e) {
await downloadYTVideo(url)
path = await findFileById(id)
}
return path
}
export function extractId(url: string): string {
const idMatch = /[?&]v=([a-zA-Z0-9_-]{11})/.exec(url);
if (!idMatch) throw new Error("Cannot extract video ID");
const id = idMatch[1];
console.log(`ID: ${id}`);
return id
}
export async function downloadYTVideo(url: string): Promise<void> {
const ytDlpBin = process.env.YT_DLP_BIN_PATH! ?? "yt-dlp";
await new Promise<void>((resolve, reject) => {
const p = spawn(ytDlpBin, [
"-x",
"--audio-format", "opus",
"--audio-quality", "0",
"--no-playlist",
"--paths", DATA_DIR,
url,
]);
p.stderr.on("data", d => process.stderr.write(d));
p.on("close", code => code === 0 ? resolve() : reject(new Error("yt-dlp failed")));
});
}
export async function findFileById(id: string): Promise<string> {
return await new Promise<string>((resolve, reject) => {
const find = spawn("find", [
DATA_DIR,
"-type", "f",
"-iname", `*${id}*.opus`,
"-exec", "readlink", "-f", "{}", ";",
]);
let out = "";
find.stdout.on("data", d => out += d.toString());
find.stderr.on("data", d => process.stderr.write(d));
find.on("close", code => {
if (code !== 0 || !out.trim()) {
reject(new Error("Audio file not found"));
} else {
resolve(out.trim());
}
});
});
}

View File

@@ -7,8 +7,18 @@ import {
joinVoiceChannel,
type AudioPlayer,
} from '@discordjs/voice';
import type { VoiceBasedChannel } from 'discord.js';
import type { CacheType, ChatInputCommandInteraction, VoiceBasedChannel } from 'discord.js';
import { createDiscordJSAdapter } from './adapter.js';
import { player } from '../main.js';
export async function connectToChannelByInteraction(interaction: ChatInputCommandInteraction<CacheType>): Promise<void> {
const member = await interaction.guild?.members.fetch(interaction.user.id);
if (!member) throw new Error("ale chuj")
const voiceChannel = member.voice.channel;
if (!voiceChannel) throw new Error("ale chuj 2")
const connection = await connectToChannel(voiceChannel);
connection.subscribe(player);
}
export async function connectToChannel(channel: VoiceBasedChannel) {
/**