added search

This commit is contained in:
Patryk Koreń
2025-12-28 11:31:45 +01:00
parent b551cce62a
commit 03c2d35bf0
4 changed files with 57 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ import { CacheType, ChatInputCommandInteraction, SlashCommandBuilder } from 'dis
import { connectToChannelByInteraction } from '../util/helpers';
import { forceRequestSong } from '../playback';
import { getPlayMsg } from '../messages';
import { search } from '../util/downloader';
const name = "forceplay"
@@ -16,16 +17,27 @@ function register() {
async function execute(interaction: ChatInputCommandInteraction<CacheType>) {
try {
await interaction.deferReply()
connectToChannelByInteraction(interaction)
const url = interaction.options.getString("url")!;
const input = interaction.options.getString("url")!;
let url: string;
if (input.startsWith("http")) {
url = input
} else {
url = await search(input)
await interaction.editReply(`searching for: ${input}`);
console.log(input, url)
}
await forceRequestSong(interaction, url)
const msg = getPlayMsg(url)
await interaction.reply(msg);
await interaction.editReply(msg);
} catch (error) {
console.error(error);
await interaction.reply('Coś poszło nie tak :/');
await interaction.editReply('Coś poszło nie tak :/');
}
}

View File

@@ -2,6 +2,7 @@ import { CacheType, ChatInputCommandInteraction, SlashCommandBuilder } from 'dis
import { connectToChannelByInteraction } from '../util/helpers';
import { requestSong } from '../playback';
import { getPlayMsg } from '../messages';
import { search } from '../util/downloader';
const name = "play"
@@ -10,22 +11,33 @@ function register() {
.setName(name)
.setDescription('YT')
.addStringOption((option) => option.setName("url")
.setDescription("YT url")
.setDescription("YT url/search")
.setRequired(true))
}
async function execute(interaction: ChatInputCommandInteraction<CacheType>) {
try {
await interaction.deferReply()
connectToChannelByInteraction(interaction)
const url = interaction.options.getString("url")!;
const input = interaction.options.getString("url")!;
let url: string;
if (input.startsWith("http")) {
url = input
} else {
url = await search(input)
await interaction.editReply(`searching for: ${input}`);
console.log(input, url)
}
await requestSong(interaction, url)
const msg = getPlayMsg(url)
await interaction.reply(msg);
await interaction.editReply(msg);
} catch (error) {
console.error(error);
await interaction.reply('Coś poszło nie tak :/');
await interaction.editReply('Coś poszło nie tak :/');
}
}

View File

@@ -25,6 +25,7 @@ export async function forceRequestSong(
const path = await getAudioFile(url)
queue.songList.push(path)
queue.current = path
playSong(player, path);
}
@@ -36,6 +37,7 @@ export async function updatePlayer() {
queue.current = null
return
};
queue.current = nextSong;
playSong(player, nextSong);
}
}

View File

@@ -63,3 +63,27 @@ export async function findFileById(id: string): Promise<string> {
});
}
export async function search(input: string) {
return await new Promise<string>((resolve, reject) => {
const ytDlpBin = process.env.YT_DLP_BIN_PATH! ?? "yt-dlp";
console.log(`search for ${input}`)
const search_url = spawn(ytDlpBin, [
"--skip-download",
"--print", "%(webpage_url)s",
`ytsearch: ${input}`
]);
let out = "";
search_url.stdout.on("data", d => out += d.toString());
search_url.stderr.on("data", d => process.stderr.write(d));
search_url.on("close", code => {
if (code !== 0 || !out.trim()) {
reject(new Error("Search failed"));
} else {
resolve(out.trim());
}
});
});
}