updates
This commit is contained in:
parent
315d94acfe
commit
bfd88e8d42
3 changed files with 39 additions and 20 deletions
31
README.md
31
README.md
|
@ -46,13 +46,13 @@ Here is the code you will need to get started with discord-player. Then, you wil
|
|||
|
||||
```js
|
||||
const Discord = require("discord.js"),
|
||||
client = new Discord.Client,
|
||||
client = new Discord.Client({ intents: ["GUILD_VOICE_STATES", "GUILD_MESSAGES", "GUILDS"] }),
|
||||
settings = {
|
||||
prefix: "!",
|
||||
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)
|
||||
const player = new Player(client);
|
||||
|
@ -61,7 +61,7 @@ const player = new Player(client);
|
|||
client.player = player;
|
||||
|
||||
// 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", () => {
|
||||
console.log("I'm ready !");
|
||||
|
@ -74,9 +74,28 @@ client.on("message", async (message) => {
|
|||
|
||||
// !play Despacito
|
||||
// will play "Despacito" in the voice channel
|
||||
if(command === "play"){
|
||||
client.player.play(message, args[0]);
|
||||
// as we registered the event above, no need to send a success message here
|
||||
if (command === "play") {
|
||||
if (!message.member.voice.channel) return void message.reply("You are not in a voice channel!");
|
||||
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);
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -49,7 +49,6 @@ class Queue<T = unknown> {
|
|||
this.options,
|
||||
{
|
||||
leaveOnEnd: true,
|
||||
leaveOnEndCooldown: 1000,
|
||||
leaveOnStop: true,
|
||||
leaveOnEmpty: true,
|
||||
leaveOnEmptyCooldown: 1000,
|
||||
|
@ -109,9 +108,9 @@ class Queue<T = unknown> {
|
|||
/**
|
||||
* Destroys this queue
|
||||
*/
|
||||
destroy() {
|
||||
destroy(disconnect = this.options.leaveOnStop) {
|
||||
this.connection.end();
|
||||
this.connection.disconnect();
|
||||
if (disconnect) this.connection.disconnect();
|
||||
this.player.queues.delete(this.guild.id);
|
||||
}
|
||||
|
||||
|
@ -245,15 +244,17 @@ class Queue<T = unknown> {
|
|||
seek: options.seek
|
||||
}).on("error", (err) => this.player.emit("error", this, err));
|
||||
} else {
|
||||
stream = ytdl.arbitraryStream(
|
||||
track.raw.source === "soundcloud" ? await track.raw.engine.downloadProgressive() : typeof track.raw.engine === "function" ? await track.raw.engine() : track.raw.engine,
|
||||
{
|
||||
opusEncoded: false,
|
||||
fmt: "s16le",
|
||||
encoderArgs: options.encoderArgs ?? [],
|
||||
seek: options.seek
|
||||
}
|
||||
).on("error", (err) => this.player.emit("error", this, err));
|
||||
stream = ytdl
|
||||
.arbitraryStream(
|
||||
track.raw.source === "soundcloud" ? await track.raw.engine.downloadProgressive() : typeof track.raw.engine === "function" ? await track.raw.engine() : track.raw.engine,
|
||||
{
|
||||
opusEncoded: false,
|
||||
fmt: "s16le",
|
||||
encoderArgs: options.encoderArgs ?? [],
|
||||
seek: options.seek
|
||||
}
|
||||
)
|
||||
.on("error", (err) => this.player.emit("error", this, err));
|
||||
}
|
||||
|
||||
const resource: AudioResource<Track> = this.connection.createStream(stream, {
|
||||
|
@ -275,7 +276,7 @@ class Queue<T = unknown> {
|
|||
if (options.filtersUpdate) return;
|
||||
|
||||
if (!this.tracks.length && this.repeatMode === QueueRepeatMode.OFF) {
|
||||
this.destroy();
|
||||
if (this.options.leaveOnEnd) this.destroy();
|
||||
this.player.emit("queueEnd", this);
|
||||
} else {
|
||||
if (this.repeatMode === QueueRepeatMode.TRACK) return void this.play(Util.last(this.previousTracks), { immediate: true });
|
||||
|
|
|
@ -74,7 +74,6 @@ export interface PlayerProgressbarOptions {
|
|||
|
||||
export interface PlayerOptions {
|
||||
leaveOnEnd?: boolean;
|
||||
leaveOnEndCooldown?: number;
|
||||
leaveOnStop?: boolean;
|
||||
leaveOnEmpty?: boolean;
|
||||
leaveOnEmptyCooldown?: number;
|
||||
|
|
Loading…
Reference in a new issue