This commit is contained in:
Snowflake107 2021-06-18 00:54:02 +05:45
parent 315d94acfe
commit bfd88e8d42
3 changed files with 39 additions and 20 deletions

View file

@ -46,13 +46,13 @@ Here is the code you will need to get started with discord-player. Then, you wil
```js ```js
const Discord = require("discord.js"), const Discord = require("discord.js"),
client = new Discord.Client, client = new Discord.Client({ intents: ["GUILD_VOICE_STATES", "GUILD_MESSAGES", "GUILDS"] }),
settings = { settings = {
prefix: "!", prefix: "!",
token: "Your Discord Token" token: "Your Discord Token"
}; };
const { Player } = require("discord-player"); const { Player, QueryType } = require("discord-player");
// Create a new Player (you don't need any API Key) // Create a new Player (you don't need any API Key)
const player = new Player(client); const player = new Player(client);
@ -61,7 +61,7 @@ const player = new Player(client);
client.player = player; client.player = player;
// add the trackStart event so when a song will be played this message will be sent // add the trackStart event so when a song will be played this message will be sent
client.player.on("trackStart", (message, track) => message.channel.send(`Now playing ${track.title}...`)) client.player.on("trackStart", (queue, track) => queue.metadata.channel.send(`Now playing ${track.title}...`))
client.once("ready", () => { client.once("ready", () => {
console.log("I'm ready !"); console.log("I'm ready !");
@ -75,8 +75,27 @@ client.on("message", async (message) => {
// !play Despacito // !play Despacito
// will play "Despacito" in the voice channel // will play "Despacito" in the voice channel
if (command === "play") { if (command === "play") {
client.player.play(message, args[0]); if (!message.member.voice.channel) return void message.reply("You are not in a voice channel!");
// as we registered the event above, no need to send a success message here if (message.guild.me.voice.channel && message.member.voice.channelID !== message.guild.me.voice.channelID) return void message.reply("You are not in my voice channel!");
const queue = client.player.createQueue(message.guild, {
metadata: message
});
// verify vc connection
try {
if (!queue.connection) await queue.connect(message.member.voice.channel);
} catch {
queue.destroy();
return void message.reply("Could not join your voice channel!");
}
const track = await client.player.search(args[0], {
searchEngine: QueryType.YOUTUBE_SEARCH
}).then(x => x.tracks[1]);
if (!track) return void message.reply("Track not found!");
queue.play(track);
} }
}); });

View file

@ -49,7 +49,6 @@ class Queue<T = unknown> {
this.options, this.options,
{ {
leaveOnEnd: true, leaveOnEnd: true,
leaveOnEndCooldown: 1000,
leaveOnStop: true, leaveOnStop: true,
leaveOnEmpty: true, leaveOnEmpty: true,
leaveOnEmptyCooldown: 1000, leaveOnEmptyCooldown: 1000,
@ -109,9 +108,9 @@ class Queue<T = unknown> {
/** /**
* Destroys this queue * Destroys this queue
*/ */
destroy() { destroy(disconnect = this.options.leaveOnStop) {
this.connection.end(); this.connection.end();
this.connection.disconnect(); if (disconnect) this.connection.disconnect();
this.player.queues.delete(this.guild.id); this.player.queues.delete(this.guild.id);
} }
@ -245,7 +244,8 @@ class Queue<T = unknown> {
seek: options.seek seek: options.seek
}).on("error", (err) => this.player.emit("error", this, err)); }).on("error", (err) => this.player.emit("error", this, err));
} else { } else {
stream = ytdl.arbitraryStream( stream = ytdl
.arbitraryStream(
track.raw.source === "soundcloud" ? await track.raw.engine.downloadProgressive() : typeof track.raw.engine === "function" ? await track.raw.engine() : track.raw.engine, track.raw.source === "soundcloud" ? await track.raw.engine.downloadProgressive() : typeof track.raw.engine === "function" ? await track.raw.engine() : track.raw.engine,
{ {
opusEncoded: false, opusEncoded: false,
@ -253,7 +253,8 @@ class Queue<T = unknown> {
encoderArgs: options.encoderArgs ?? [], encoderArgs: options.encoderArgs ?? [],
seek: options.seek seek: options.seek
} }
).on("error", (err) => this.player.emit("error", this, err)); )
.on("error", (err) => this.player.emit("error", this, err));
} }
const resource: AudioResource<Track> = this.connection.createStream(stream, { const resource: AudioResource<Track> = this.connection.createStream(stream, {
@ -275,7 +276,7 @@ class Queue<T = unknown> {
if (options.filtersUpdate) return; if (options.filtersUpdate) return;
if (!this.tracks.length && this.repeatMode === QueueRepeatMode.OFF) { if (!this.tracks.length && this.repeatMode === QueueRepeatMode.OFF) {
this.destroy(); if (this.options.leaveOnEnd) this.destroy();
this.player.emit("queueEnd", this); this.player.emit("queueEnd", this);
} else { } else {
if (this.repeatMode === QueueRepeatMode.TRACK) return void this.play(Util.last(this.previousTracks), { immediate: true }); if (this.repeatMode === QueueRepeatMode.TRACK) return void this.play(Util.last(this.previousTracks), { immediate: true });

View file

@ -74,7 +74,6 @@ export interface PlayerProgressbarOptions {
export interface PlayerOptions { export interface PlayerOptions {
leaveOnEnd?: boolean; leaveOnEnd?: boolean;
leaveOnEndCooldown?: number;
leaveOnStop?: boolean; leaveOnStop?: boolean;
leaveOnEmpty?: boolean; leaveOnEmpty?: boolean;
leaveOnEmptyCooldown?: number; leaveOnEmptyCooldown?: number;