get the basic bot working

This commit is contained in:
Snowflake107 2021-06-12 01:03:53 +05:45
parent a823c6f173
commit 61c30986ea
4 changed files with 63 additions and 20 deletions

View file

@ -7,7 +7,7 @@ const client = new Client({
}); });
const player = new Player(client); const player = new Player(client);
player.on("trackStart", (queue, track) => console.log(`Now playing: ${track.title} in ${queue.guild.name}!`)); // player.on("trackStart", (queue, track) => console.log(`Now playing: ${track.title} in ${queue.guild.name}!`));
client.on("ready", () => console.log("Bot is online!")); client.on("ready", () => console.log("Bot is online!"));
@ -20,15 +20,48 @@ client.on("message", async message => {
if (!conn) return; if (!conn) return;
return void message.channel.send(`Now Playing: **${conn.current.title}** (Played **${Math.floor(conn.connection.streamTime / 1000)} seconds**)`); return void message.channel.send(`Now Playing: **${conn.current.title}** (Played **${Math.floor(conn.connection.streamTime / 1000)} seconds**)`);
} }
if (message.content.startsWith("!pause") && message.guild.me.voice.channelID) {
const conn = player.getQueue(message.guild.id);
if (!conn) return;
conn.setPaused(true);
return void message.channel.send("Paused!");
}
if (message.content.startsWith("!resume") && message.guild.me.voice.channelID) {
const conn = player.getQueue(message.guild.id);
if (!conn) return;
conn.setPaused(false);
return void message.channel.send("Resumed!");
}
if (message.content.startsWith("!skip") && message.guild.me.voice.channelID) {
const conn = player.getQueue(message.guild.id);
if (!conn) return;
conn.skip();
return void message.channel.send("Done!");
}
if (message.content.startsWith("!queue") && message.guild.me.voice.channelID) {
const conn = player.getQueue(message.guild.id);
if (!conn) return;
return void message.channel.send({ content: conn.toString(), split: true });
}
if (message.content.startsWith("!vol") && message.guild.me.voice.channelID) {
const conn = player.getQueue(message.guild.id);
if (!conn) return;
conn.connection.setVolume(parseInt(message.content.slice(4).trim()));
return void message.channel.send("Volume changed!");
}
if (message.content.startsWith("!p") && message.member.voice.channelID) { if (message.content.startsWith("!p") && message.member.voice.channelID) {
const queue = player.createQueue(message.guild); const queue = player.createQueue(message.guild);
const song = await player.search(message.content.slice(2).trim(), message.author).then(x => x[0]); const song = await player.search(message.content.slice(2).trim(), message.author).then(x => x[0]);
queue.addTrack(song);
if (!queue.connection) { if (!queue.connection) {
queue.connect(message.member.voice.channel) queue.connect(message.member.voice.channel)
.then(async q => { .then(async q => {
await q.play(song); await q.play();
message.channel.send(`🎶 | Playing: **${song.title}**!`);
}); });
} else {
message.channel.send(`🎶 | Queued: **${song.title}**!`);
} }
} }
}); });

View file

@ -47,20 +47,20 @@ class DiscordPlayer extends EventEmitter<PlayerEvents> {
}); });
return videos.map((m) => { return videos.map((m) => {
(m as any).source = "youtube"; (m as any).source = "youtube";
return new Track(this, { return new Track(this, {
title: m.title, title: m.title,
description: m.description, description: m.description,
author: m.channel?.name, author: m.channel?.name,
url: m.url, url: m.url,
requestedBy: requestedBy, requestedBy: requestedBy,
thumbnail: m.thumbnail?.displayThumbnailURL("maxresdefault"), thumbnail: m.thumbnail?.displayThumbnailURL("maxresdefault"),
views: m.views, views: m.views,
fromPlaylist: false, fromPlaylist: false,
duration: m.durationFormatted, duration: m.durationFormatted,
raw: m raw: m
})
}); });
});
} }
default: default:
return []; return [];

View file

@ -60,7 +60,8 @@ class Queue {
skip() { skip() {
if (!this.connection) return false; if (!this.connection) return false;
return this.connection.end(); this.connection.end();
return true;
} }
addTrack(track: Track) { addTrack(track: Track) {
@ -71,6 +72,11 @@ class Queue {
this.tracks.push(...tracks); this.tracks.push(...tracks);
} }
setPaused(paused?: boolean) {
if (!this.connection) return false;
return paused ? this.connection.pause() : this.connection.resume();
}
async play(src?: Track) { async play(src?: Track) {
if (!this.connection || !this.connection.voiceConnection) if (!this.connection || !this.connection.voiceConnection)
throw new Error("Voice connection is not available, use <Queue>.connect()!"); throw new Error("Voice connection is not available, use <Queue>.connect()!");
@ -108,7 +114,7 @@ class Queue {
}); });
} }
const dispatcher = this.connection.playStream(resource); const dispatcher = await this.connection.playStream(resource);
dispatcher.setVolume(this.options.initialVolume); dispatcher.setVolume(this.options.initialVolume);
dispatcher.on("start", () => { dispatcher.on("start", () => {

View file

@ -103,7 +103,9 @@ class BasicStreamDispatcher extends EventEmitter<VoiceEvents> {
* Disconnects from voice * Disconnects from voice
*/ */
disconnect() { disconnect() {
this.voiceConnection.destroy(); try {
this.voiceConnection.destroy();
} catch {}
} }
/** /**
@ -125,19 +127,21 @@ class BasicStreamDispatcher extends EventEmitter<VoiceEvents> {
* Play stream * Play stream
* @param {AudioResource} resource The audio resource to play * @param {AudioResource} resource The audio resource to play
*/ */
playStream(resource: AudioResource<Track> = this.audioResource) { async playStream(resource: AudioResource<Track> = this.audioResource) {
if (!resource) throw new PlayerError("Audio resource is not available!"); if (!resource) throw new PlayerError("Audio resource is not available!");
if (!this.audioResource) this.audioResource = resource; if (!this.audioResource) this.audioResource = resource;
if (this.voiceConnection.state.status !== VoiceConnectionStatus.Ready) await entersState(this.voiceConnection, VoiceConnectionStatus.Ready, 20000);
this.audioPlayer.play(resource); this.audioPlayer.play(resource);
return this; return this;
} }
setVolume(value: number) { setVolume(value: number) {
if (!this.audioResource) return; if (!this.audioResource || isNaN(value) || value < 0 || value > Infinity) return false;
// ye boi logarithmic ✌ // ye boi logarithmic ✌
this.audioResource.volume.setVolumeLogarithmic(value / 200); this.audioResource.volume.setVolumeLogarithmic(value / 200);
return true;
} }
get streamTime() { get streamTime() {