more error handling

This commit is contained in:
Jill 2021-11-07 15:30:06 +03:00
parent e4bdf94c5c
commit 7daab42d5f
1 changed files with 29 additions and 22 deletions

View File

@ -113,35 +113,42 @@ async function play(song, id, connection, channel) {
url = out.stdout;
}
const stream = got(url, {isStream: true});
const resource = Voice.createAudioResource(stream, { inputType: Voice.StreamType.Arbitrary });
try {
const stream = got(url, {isStream: true});
const resource = Voice.createAudioResource(stream, { inputType: Voice.StreamType.Arbitrary });
stream.on('error', err => {
stream.on('error', err => {
console.log(err);
channel.send(`failed to play track: \`\`\`${err}\`\`\``);
advanceQueue(id, channel, subscription, connection);
return;
});
if (players[id]) {
console.log('reusing player');
player = players[id];
} else {
console.log('creating new player');
player = Voice.createAudioPlayer();
players[id] = player;
subscription = connection.subscribe(player);
player.on(Voice.AudioPlayerStatus.Idle, () => {
advanceQueue(id, channel, subscription, connection);
});
player.on(Voice.AudioPlayerStatus.Playing, () => {
playerStartTime[id] = Date.now();
})
}
player.play(resource);
} catch(err) {
console.log(err);
channel.send(`failed to play track: \`\`\`${err}\`\`\``);
advanceQueue(id, channel, subscription, connection);
return;
});
if (players[id]) {
console.log('reusing player');
player = players[id];
} else {
console.log('creating new player');
player = Voice.createAudioPlayer();
players[id] = player;
subscription = connection.subscribe(player);
player.on(Voice.AudioPlayerStatus.Idle, () => {
advanceQueue(id, channel, subscription, connection);
});
player.on(Voice.AudioPlayerStatus.Playing, () => {
playerStartTime[id] = Date.now();
})
}
player.play(resource);
return player;
}