Revert from ytdownload to ytdl_core, prevent bot from reconnecting if it's already connected (#2)

* update discordeno-audio-plugin

* rename discordeno-audio-plugin-main folder to discordeno-audio-plugin

* switch back to ytdl instead of ytDownload, remove unused combine.ts file, make empty() async, don't reconnect to the socket if the bot's already playing

Co-authored-by: Lexie Love <lexie@unabridgedsoftware.com>
This commit is contained in:
alexandra-love 2023-01-17 16:09:21 -06:00 committed by GitHub
parent a348da6b74
commit 320d6d1782
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 27 additions and 17 deletions

View File

@ -14,7 +14,7 @@ import { configs } from "../configs.ts"
import { bot } from "../main.ts";
import { getAllowedTextChannel } from "../utils.ts";
import { ConnectionData } from "../discordeno-audio-plugin-main/mod.ts";
import { ConnectionData } from "../discordeno-audio-plugin/mod.ts";
export async function np(bot: Bot, interaction: Interaction) {
await sendInteractionResponse(bot, interaction.id, interaction.token, nowPlayingResponse(bot, interaction));

View File

@ -18,6 +18,9 @@ function alreadyPlayingResponse(bot: Bot, interaction: Interaction) {
return formatCallbackData(`The bot is already playing.
Currently playing: **${player.nowPlaying.title}**, added by ${player.nowPlaying.added_by}`);
}
const badUrlResponse = formatCallbackData(`Bad URL, please enter a URL that starts with https://youtube.com or https://youtu.be.`);
const emptyQueueResponse = formatCallbackData(`There's nothing in the queue to play right now.`);
function nowPlayingResponse(bot: Bot, interaction: Interaction) {
@ -48,17 +51,23 @@ export async function play(bot: Bot, interaction: Interaction, _args?) {
const player = bot.helpers.getPlayer(interaction.guildId);
await sendInteractionResponse(bot, interaction.id, interaction.token, waitingForResponse);
if(interaction.data.options) {
const parsed_url = new URL(interaction.data.options[0].value);
let parsed_url;
if(interaction!.data!.options) {
try {
parsed_url = new URL(interaction!.data!.options[0].value);
} catch {
await editOriginalInteractionResponse(bot, interaction.token, badUrlResponse);
}
let href;
if(parsed_url.href.indexOf("?t=") !== -1) {
href = parsed_url.href.substring(0, parsed_url.href.indexOf("?"))
} else {
href = parsed_url.href;
}
// TODO: maybe switch to ytdl and not have to use the deno youtube library?
const result = await player.pushQuery(interaction.user.username, href);
if(result && parsed_url.href.indexOf("youtube.com") !== -1 || parsed_url.href.indexOf("youtu.be") !== -1 && result[0].title) {
if(result && result[0] && parsed_url.href.indexOf("youtube.com") !== -1 || parsed_url.href.indexOf("youtu.be") !== -1 && result[0].title) {
await editOriginalInteractionResponse(bot, interaction.token, addedToQueueResponse(interaction, result[0].title));
}
} else {

View File

@ -20,4 +20,4 @@ export { type Bot } from "https://deno.land/x/discordeno@17.0.1/bot.ts";
export { type Interaction } from "https://deno.land/x/discordeno@17.0.1/transformers/interaction.ts";
export { type ApplicationCommandOption, type ApplicationCommandOptionChoice, type Embed } from "https://deno.land/x/discordeno@17.0.1/transformers/mod.ts";
export { leaveVoiceChannel } from "https://deno.land/x/discordeno@17.0.1/helpers/guilds/mod.ts";
export { getConnectionData } from "./discordeno-audio-plugin-main/src/connection-data.ts";
export { getConnectionData } from "./discordeno-audio-plugin/src/connection-data.ts";

View File

@ -3,5 +3,6 @@ export * from "https://deno.land/x/discordeno@17.1.0/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.2/mod.ts";
export type { VideoFormat } from "https://deno.land/x/ytdl_core@v0.1.0/src/types.ts";
export type { VideoFormat } from "https://deno.land/x/ytdl_core@v0.1.2/src/types.ts";
export { downloadFromInfo, getInfo, ytdl } from "https://deno.land/x/ytdl_core@v0.1.2/mod.ts";
export { default as YouTube } from "https://deno.land/x/youtube_sr@v4.1.17/mod.ts";

View File

@ -9,7 +9,7 @@ export type AudioSource = {
added_by?: string;
};
async function* empty() {}
export async function* empty() {}
export function createAudioSource(
title: string,

View File

@ -1,4 +1,4 @@
import { YouTube, ytDownload } from "../../deps.ts";
import { YouTube, ytdl } from "../../deps.ts";
import { bufferIter } from "../../utils/mod.ts";
import { demux } from "../demux/mod.ts";
import { createAudioSource, empty } from "./audio-source.ts";
@ -28,14 +28,13 @@ export async function getYoutubeSource(query: string, added_by?: string) {
const results = await YouTube.search(query, { limit: 1, type: "video" });
if (results.length > 0) {
const { id, title } = results[0];
return createAudioSource(title!, async () => {
return createAudioSource(title!, async () => {
try {
const stream = await ytDownload(id!, {
mimeType: `audio/webm; codecs="opus"`,
const stream = await ytdl(id!, {
filter: supportedFormatFilter
});
return bufferIter(demux(stream));
} catch {
console.error("error");
console.log(`Failed to play ${title}\n Returning empty stream`);
return empty();
}

View File

@ -52,8 +52,10 @@ function createAudioHelpers(bot: Bot, loadSource: LoadSource) {
const { guildId, userId, sessionId } = event;
if (bot.id === userId && guildId) {
const conn = getConnectionData(bot.id, guildId);
conn.connectInfo.sessionId = sessionId;
connectWebSocket(conn, bot.id, guildId);
if(!conn.player.nowPlaying) {
conn.connectInfo.sessionId = sessionId;
connectWebSocket(conn, bot.id, guildId);
}
}
oldStateListener(bot, event);
};

View File

@ -7,7 +7,6 @@ export function connectWebSocket(
userId: bigint,
guildId: bigint
) {
console.log("trying to connect the web socket");
conn.context.ready = false;
const { token, sessionId, endpoint } = conn.connectInfo;
if (

View File

@ -17,7 +17,7 @@ import { playCommand } from "./commands/play.ts";
import { skipCommand } from "./commands/skip.ts";
import { unloopCommand } from "./commands/unloop.ts";
import { enableAudioPlugin } from "./discordeno-audio-plugin-main/mod.ts";
import { enableAudioPlugin } from "./discordeno-audio-plugin/mod.ts";
let sessionId = "";