all repos — simple-discord-music-bot @ f1b6c2135d3740fb92fc2a8f095e159d93480a4a

A Discord bot making use of discord.js and play-yt.

minor bug fixes
Bi-Rabittoh andronacomarco@gmail.com
Wed, 10 Jan 2024 00:33:34 +0100
commit

f1b6c2135d3740fb92fc2a8f095e159d93480a4a

parent

11618de1e7166cedd87a510b2bab1d52ff1c5744

4 files changed, 37 insertions(+), 25 deletions(-)

jump to
M src/commands/play.tssrc/commands/play.ts

@@ -3,23 +3,23 @@ import play, { YouTubeVideo } from 'play-dl';

import { playUrls, getChannel, formatTitle } from '../functions/music'; async function handleUserInput(input: string): Promise<YouTubeVideo[]> { - switch (play.yt_validate(input)) { - case 'video': - const info = await play.video_basic_info(input); - return [info.video_details]; - case 'search': - const results = await play.search(input, { source: { youtube: 'video' }, limit: 1 }); - - if (results.length == 0) + try { + switch (play.yt_validate(input)) { + case 'video': + const info = await play.video_basic_info(input); + return [info.video_details]; + case 'playlist': + const playlist = await play.playlist_info(input, { incomplete: true }); + return await playlist.all_videos(); + case 'search': + const results = await play.search(input, { source: { youtube: 'video' }, limit: 1 }); + if (results.length > 0) return [results[0]]; + default: return []; - - const firstResult = results[0]; - return [firstResult]; - case 'playlist': - const playlist = await play.playlist_info(input, { incomplete: true }); - return await playlist.all_videos(); - default: - return []; + } + } catch (error) { + console.error(error); + return []; } }

@@ -41,7 +41,6 @@

await interaction.deferReply(); const opt = interaction.options; const input = opt.getString('query'); - const yt_videos = await handleUserInput(input); const added = await playUrls(yt_videos, channel);
M src/commands/stop.tssrc/commands/stop.ts

@@ -4,7 +4,7 @@

module.exports = { data: new SlashCommandBuilder() .setName('stop') - .setDescription('Stop the music.'), + .setDescription('Stop the music and leaves the voice channel.'), async execute(interaction: ChatInputCommandInteraction) { const channel = await getChannel(interaction);
M src/functions/music.tssrc/functions/music.ts

@@ -23,10 +23,11 @@ if (!channel) {

console.log('Channel error:', channel); return; } - + if (videos.length == 0) return []; q.connection = getChannelConnection(channel); return await q.addArray(videos); } + /* Only useful for radio export async function playStream(url: string, channel: VoiceBasedChannel) { if (!channel) {
M src/functions/myqueue.tssrc/functions/myqueue.ts

@@ -1,9 +1,14 @@

-import { createAudioResource, createAudioPlayer, NoSubscriberBehavior, AudioPlayerStatus, VoiceConnection, AudioPlayer } from '@discordjs/voice'; +import { createAudioResource, createAudioPlayer, NoSubscriberBehavior, AudioPlayerStatus, VoiceConnection, AudioPlayer, AudioResource } from '@discordjs/voice'; import play, { YouTubeVideo } from 'play-dl'; -async function resourceFromYTUrl(url: string) { - const stream = await play.stream(url); - return createAudioResource(stream.stream, { inputType: stream.type }) +async function resourceFromYTUrl(url: string): Promise<AudioResource<null>> { + try { + const stream = await play.stream(url); + return createAudioResource(stream.stream, { inputType: stream.type }) + } catch (error) { + return null; + } + } export default class MyQueue {

@@ -39,10 +44,14 @@ clear() {

this.#queue = Array<YouTubeVideo>(); } - stop() { + stop(): boolean { this.clear(); this.#nowPlaying = null; - if (this.player) return this.player.stop(); + if (this.player) { + const p = this.player.stop(); + const c = this.connection.disconnect(); + return p && c; + } return false; }

@@ -51,6 +60,9 @@ if (this.#queue.length == 0)

return this.stop(); this.#nowPlaying = this.#queue.shift(); const resource = await resourceFromYTUrl(this.#nowPlaying.url); + if (!resource) { + return await this.next(); + } this.player.play(resource); this.connection.subscribe(this.player); }