update yt_download and discordeno-audio-plugin with a retry function
This commit is contained in:
parent
717fd16adc
commit
c7c049a097
|
@ -47,7 +47,8 @@ export const helpCommand = <CreateSlashApplicationCommand>{
|
|||
};
|
||||
|
||||
export async function help(bot: Bot, interaction: Interaction) {
|
||||
if (!interaction.guildId) return;
|
||||
if(!interaction.guildId) return;
|
||||
if(!interaction.data) return;
|
||||
|
||||
if(interaction.data.options) {
|
||||
switch(interaction.data.options[0].value) {
|
||||
|
|
|
@ -10,7 +10,7 @@ import {
|
|||
import { ensureVoiceConnection, formatCallbackData, waitingForResponse } from "../utils.ts";
|
||||
|
||||
function addedToQueueResponse(interaction: Interaction, title: string) {
|
||||
return formatCallbackData(`${interaction.user.username} added [**${title}**](${interaction.data.options[0].value}) to the queue.`, "Added to queue");
|
||||
return formatCallbackData(`${interaction.user.username} added [**${title}**](${interaction!.data!.options![0].value}) to the queue.`, "Added to queue");
|
||||
}
|
||||
|
||||
function alreadyPlayingResponse(bot: Bot, interaction: Interaction) {
|
||||
|
@ -43,22 +43,27 @@ export const playCommand = <CreateSlashApplicationCommand>{
|
|||
]
|
||||
};
|
||||
|
||||
|
||||
// todo it crashes when a timestamp is offered
|
||||
export async function play(bot: Bot, interaction: Interaction, _args?) {
|
||||
export async function play(bot: Bot, interaction: Interaction) {
|
||||
if (!interaction.guildId) return;
|
||||
await ensureVoiceConnection(bot, interaction.guildId);
|
||||
const player = bot.helpers.getPlayer(interaction.guildId);
|
||||
await sendInteractionResponse(bot, interaction.id, interaction.token, waitingForResponse);
|
||||
|
||||
let parsed_url;
|
||||
if(interaction!.data!.options) {
|
||||
if(!interaction) return;
|
||||
if(!interaction.data) return;
|
||||
|
||||
if(interaction.data.options) {
|
||||
if(!interaction.data.options[0].value) return;
|
||||
|
||||
try {
|
||||
parsed_url = new URL(interaction!.data!.options[0].value);
|
||||
parsed_url = new URL(interaction.data.options[0].value.toString());
|
||||
} catch {
|
||||
await editOriginalInteractionResponse(bot, interaction.token, badUrlResponse);
|
||||
}
|
||||
|
||||
if(!parsed_url) return;
|
||||
|
||||
let href;
|
||||
// remove the timestamp from the query
|
||||
if(parsed_url.href.indexOf("?t=") !== -1) {
|
||||
|
|
|
@ -2,5 +2,5 @@ export * from "https://deno.land/x/discordeno@18.0.1/mod.ts";
|
|||
export * from "https://deno.land/x/discordeno@18.0.1/plugins/cache/mod.ts";
|
||||
export * as opus from "https://unpkg.com/@evan/wasm@0.0.95/target/opus/deno.js";
|
||||
export * from "https://unpkg.com/@evan/wasm@0.0.95/target/nacl/deno.js";
|
||||
export { ytDownload } from "https://deno.land/x/yt_download@1.3/mod.ts";
|
||||
export { ytDownload } from "https://deno.land/x/yt_download@1.7/mod.ts";
|
||||
export { default as YouTube } from "https://deno.land/x/youtube_sr@v4.1.17/mod.ts";
|
|
@ -1,5 +1,5 @@
|
|||
import { YouTube, ytDownload } from "../../deps.ts";
|
||||
import { bufferIter } from "../../utils/mod.ts";
|
||||
import { bufferIter, retry } from "../../utils/mod.ts";
|
||||
import { demux } from "../demux/mod.ts";
|
||||
import { createAudioSource, empty } from "./audio-source.ts";
|
||||
|
||||
|
@ -20,17 +20,19 @@ export async function getYoutubeSource(query: string, guildId: bigint, added_by?
|
|||
if (results.length > 0) {
|
||||
const { id, title } = results[0];
|
||||
return createAudioSource(title!, async () => {
|
||||
try {
|
||||
const stream = await ytDownload(query, {
|
||||
mimeType: `audio/webm; codecs="opus"`,
|
||||
});
|
||||
return bufferIter(demux(stream));
|
||||
} catch {
|
||||
const stream = await retry(
|
||||
async () =>
|
||||
await ytDownload(id!, {
|
||||
mimeType: `audio/webm; codecs="opus"`,
|
||||
})
|
||||
);
|
||||
if (stream === undefined) {
|
||||
errorMessageCallback(guildId, `There was an error trying to play **${title}**:\n
|
||||
something broke in getYoutubeSource`);
|
||||
console.log(`Failed to play ${title}\n Returning empty stream`);
|
||||
return empty();
|
||||
}
|
||||
return bufferIter(demux(stream));
|
||||
}, guildId, added_by);
|
||||
}
|
||||
} catch(err) {
|
||||
|
|
|
@ -6,4 +6,5 @@ export * from "./number.ts";
|
|||
export * from "./wait.ts";
|
||||
export * from "./file.ts";
|
||||
export * from "./buffer.ts";
|
||||
export * from "./retry.ts";
|
||||
// export * from "./buffered-iterator.ts";
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
export async function retry<T>(
|
||||
func: () => Promise<T>,
|
||||
max_tries = 3
|
||||
): Promise<T | undefined> {
|
||||
let tries = 0;
|
||||
while (tries < max_tries) {
|
||||
try {
|
||||
return await func();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
tries++;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
15
utils.ts
15
utils.ts
|
@ -13,8 +13,6 @@ import {
|
|||
} from "./deps.ts";
|
||||
import { bot } from "./main.ts";
|
||||
|
||||
import { ConnectionData } from "./discordeno-audio-plugin/mod.ts";
|
||||
|
||||
export function channelIsAllowed(guild: string, channel: string) {
|
||||
if(`${guild}:${channel}` in configs.allowed_text_channels) {
|
||||
return true;
|
||||
|
@ -27,7 +25,9 @@ export async function ensureVoiceConnection(bot: Bot, guildId: BigString) {
|
|||
const channels = await getChannels(bot, guildId);
|
||||
const guild = await getGuild(bot, <BigString>guildId);
|
||||
let channelId = <BigString>"";
|
||||
for(let [id, channel] of channels) {
|
||||
for(const [id, channel] of channels) {
|
||||
if(!channel.name) continue;
|
||||
|
||||
if(channel.type == 2 && configs.allowed_voice_channels.includes(`${guild.name.toLowerCase()}:${channel.name.toLowerCase()}`)) {// voice channel
|
||||
channelId = id;
|
||||
}
|
||||
|
@ -65,8 +65,8 @@ export async function getAllowedTextChannel(bot: Bot, guildId: bigint) {
|
|||
const channels = await getChannels(bot, guildId);
|
||||
const guild = await getGuild(bot, <BigString>guildId);
|
||||
let channelId = BigInt(0);
|
||||
for(let [id, channel] of channels) {
|
||||
if(channel.type == 0 && configs.allowed_text_channels.includes(`${guild.name.toLowerCase()}:${channel.name.toLowerCase()}`)) {// text channel
|
||||
for(const [id, channel] of channels) {
|
||||
if(channel.type == 0 && configs.allowed_text_channels.includes(`${guild.name.toLowerCase()}:${channel!.name!.toLowerCase()}`)) {// text channel
|
||||
channelId = id;
|
||||
}
|
||||
}
|
||||
|
@ -81,8 +81,7 @@ export const waitingForResponse = <InteractionResponse>{
|
|||
}
|
||||
};
|
||||
|
||||
function errorMessage(bot: Bot, guildId: bigint, message: string) {
|
||||
const player = bot.helpers.getPlayer(guildId);
|
||||
function errorMessage(message: string) {
|
||||
return <CreateMessage>{
|
||||
embeds: [<Embed>{
|
||||
color: configs.embed_color,
|
||||
|
@ -93,7 +92,7 @@ function errorMessage(bot: Bot, guildId: bigint, message: string) {
|
|||
|
||||
export async function errorMessageCallback(guildId: bigint, message: string) {
|
||||
const channel = await getAllowedTextChannel(bot, guildId);
|
||||
await sendMessage(bot, channel.id, errorMessage(bot, guildId, message));
|
||||
await sendMessage(bot, channel.id, errorMessage(message));
|
||||
}
|
||||
|
||||
export function parseYoutubeId(url: string) {
|
||||
|
|
Loading…
Reference in New Issue