all repos — simple-discord-music-bot @ 3b18ef76a09748a67db32742c1911d937086dba4

A Discord bot making use of discord.js and play-yt.

functions/myqueue.js (view raw)

  1const {
  2    createAudioResource,
  3    createAudioPlayer,
  4    AudioPlayerStatus
  5} = require('@discordjs/voice');
  6const play = require('play-dl');
  7
  8async function resourceFromUrl(url) {
  9    const stream = await play.stream(url);
 10    return createAudioResource(stream.stream, { inputType: stream.type })
 11}
 12
 13class MyQueue {
 14    #nowPlaying = null;
 15    #queue = Array();
 16    get queue() {
 17        if (this.#nowPlaying)
 18            return [this.#nowPlaying].concat(this.#queue);
 19        return null
 20    }
 21
 22    constructor() {
 23        if (MyQueue._instance) {
 24            return MyQueue._instance
 25        }
 26        MyQueue._instance = this;
 27
 28        this.#nowPlaying = "";
 29        this.connection = null;
 30        this.#queue = Array();
 31        this.player = createAudioPlayer();
 32        this.player.on(AudioPlayerStatus.Idle, () => {
 33            if (this.#queue.length > 0) 
 34                return this.next();
 35            this.player.subscribers.forEach((e) => e.connection.disconnect());
 36        });
 37    }
 38
 39    clear() {
 40        this.#queue = Array();
 41    }
 42
 43    stop() {
 44        this.clear();
 45        this.#nowPlaying = null;
 46        if (this.player) return this.player.stop();
 47        return false;
 48    }
 49
 50    async next() {
 51        if (this.#queue.length == 0)
 52            return this.stop();
 53        this.#nowPlaying = this.#queue.shift();
 54        const resource = await resourceFromUrl(this.#nowPlaying);
 55        this.player.play(resource);
 56        this.connection.subscribe(this.player);
 57    }
 58
 59    add(url, position=this.#queue.length) {
 60        const l = this.#queue.length;
 61        const normalizedPosition = position % (l + 1);
 62        this.#queue.splice(normalizedPosition, 0, url);
 63        if (l == 0) this.next();
 64    }
 65
 66    async addArray(urls) {
 67        const l = this.#queue.length;
 68        this.#queue.push(...urls);
 69        if (l == 0 && this.player.state.status == AudioPlayerStatus.Idle) this.next();
 70        return l != this.#queue.length;
 71    }
 72
 73    pause() {
 74        this.player.pause();
 75    }
 76
 77    resume() {
 78        this.player.unpause();
 79        this.connection.subscribe(this.player);
 80    }
 81
 82    async outro(url) {
 83        this.player.pause();
 84        const resource = await resourceFromUrl(url);
 85
 86        const p = createAudioPlayer();
 87        p.on(AudioPlayerStatus.Idle, () => {
 88            if (this.player.state.status == AudioPlayerStatus.Paused) {
 89                this.resume();
 90                return
 91            }
 92            p.subscribers.forEach((e) => e.connection.disconnect());
 93        });
 94
 95        p.play(resource);
 96        this.connection.subscribe(p);
 97    }
 98}
 99
100module.exports = { MyQueue }