Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Fix music bot example #237

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/music-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"discord-api-types": "^0.19.0",
"discord.js": "^13.0.0-dev.328501b.1626912223",
"libsodium-wrappers": "^0.7.9",
"youtube-dl-exec": "^1.2.4",
"youtube-dl-exec": "^2.0.0",
"ytdl-core": "^4.8.3"
},
"devDependencies": {
"tsconfig-paths": "^3.9.0",
"typescript": "~4.2.2"
"typescript": "^4.5.2"
}
}
2 changes: 1 addition & 1 deletion examples/music-bot/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ client.on('interactionCreate', async (interaction: Interaction) => {
let subscription = subscriptions.get(interaction.guildId);

if (interaction.commandName === 'play') {
await interaction.defer();
await interaction.deferReply();
// Extract the video URL from the command
const url = interaction.options.get('song')!.value! as string;

Expand Down
8 changes: 5 additions & 3 deletions examples/music-bot/src/music/subscription.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
AudioPlayer,
AudioPlayerState,
AudioPlayerStatus,
AudioResource,
createAudioPlayer,
entersState,
VoiceConnection,
VoiceConnectionDisconnectReason,
VoiceConnectionState,
VoiceConnectionStatus,
} from '@discordjs/voice';
import type { Track } from './track';
Expand All @@ -29,7 +31,7 @@ export class MusicSubscription {
this.audioPlayer = createAudioPlayer();
this.queue = [];

this.voiceConnection.on('stateChange', async (_: any, newState: { status: any; reason: any; closeCode: number; }) => {
this.voiceConnection.on('stateChange', async (_: any, newState: VoiceConnectionState) => {
if (newState.status === VoiceConnectionStatus.Disconnected) {
if (newState.reason === VoiceConnectionDisconnectReason.WebSocketClose && newState.closeCode === 4014) {
/**
Expand Down Expand Up @@ -84,7 +86,7 @@ export class MusicSubscription {
});

// Configure audio player
this.audioPlayer.on('stateChange', (oldState: { status: any; resource: any; }, newState: { status: any; resource: any; }) => {
this.audioPlayer.on('stateChange', (oldState: AudioPlayerState, newState: AudioPlayerState) => {
if (newState.status === AudioPlayerStatus.Idle && oldState.status !== AudioPlayerStatus.Idle) {
// If the Idle state is entered from a non-Idle state, it means that an audio resource has finished playing.
// The queue is then processed to start playing the next track, if one is available.
Expand All @@ -96,7 +98,7 @@ export class MusicSubscription {
}
});

this.audioPlayer.on('error', (error: { resource: any; }) => (error.resource as AudioResource<Track>).metadata.onError(error));
this.audioPlayer.on('error', (error: { message: string; name: string; resource: any; }) => (error.resource as AudioResource<Track>).metadata.onError(error));

voiceConnection.subscribe(this.audioPlayer);
}
Expand Down
10 changes: 5 additions & 5 deletions examples/music-bot/src/music/track.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getInfo } from 'ytdl-core';
import { AudioResource, createAudioResource, demuxProbe } from '@discordjs/voice';
import { raw as ytdl } from 'youtube-dl-exec';
import { exec as ytdl } from 'youtube-dl-exec';

/**
* This is the data required to create a Track object.
Expand Down Expand Up @@ -48,10 +48,10 @@ export class Track implements TrackData {
const process = ytdl(
this.url,
{
o: '-',
q: '',
f: 'bestaudio[ext=webm+acodec=opus+asr=48000]/bestaudio',
r: '100K',
output: '-',
quiet: true,
format: 'bestaudio[ext=webm+acodec=opus+asr=48000]/bestaudio',
limitRate: '100K',
Comment on lines +51 to +54
Copy link
Author

@conr conr Nov 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Types for the flags were added to youtube-dl-exec in this commit.

},
{ stdio: ['ignore', 'pipe', 'ignore'] },
);
Expand Down
1 change: 0 additions & 1 deletion examples/music-bot/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"rootDir": "src",
"paths": {
"@discordjs/voice": ["../../"],
Expand Down