Error with discord bot

discord
#21

I have changed the version of discord.js in your package.json to “github:discordjs/discord.js”.

You have a new error again, would you know how to solve this one?

1 Like

#22

Well, the code that is causing the error is this:

const { LAVALINK_NODES, LAVALINK } = require("../config");
const { PlayerManager, Player } = require("discord.js-lavalink");
const { shuffleArray, pullProps } = require("./util");
const Paginator = require("./Paginator");
const snekfetch = require("snekfetch");
const Duration = require("duration-js");

class MusicPlayer {
    

    async get(channel) {
        const { guild, id } = channel;
        let player = this.guilds[guild.id];
        if (player) return player;

        player = new GuildPlayer(await this.createPlayer(channel), channel)
        return this.guilds[guild.id] = player;
    }

    check(channel) {
        let player = this.guilds[channel.guild.id];
        if (player) return player;
        else return false;
    }

    kill(guild) {
        const player = this.guilds[guild.id];
        if (!player) return;
        this.manager.leave(guild.id);
        this.guilds[guild.id] = undefined;
    }

    async createPlayer(channel) {
        return this.manager.join({
            channel: channel.id,
            guild: channel.guild.id,
            host: "localhost"
        }, { selfmute: true });
    }

    constructor(bot) {
        this.bot = bot;
        this.manager = new PlayerManager(bot, LAVALINK_NODES, {
            user: bot.user.id,
            shards: 1
        })
    }
}

class GuildPlayer {
    player;
    guild; // The guild this player is attached to.
    channel;
    client;
    _broadcastChannel;

    playlist = [];

    isPlaying = false;
    currentTrack;
    loopPlaylist = false;

    dj;
    // Commands

    async playLink(query) {
        let tracks = await loadTracks(query);
        for (let i = 0; i < tracks.length; i++) {
            tracks[i] = this.formatTrack(tracks[i]);
            this.queueTrack(tracks[i]);
        }
        return tracks;
    }

    async playSearch(query) {
        let track = await loadTracks(query, true);
        track = this.formatTrack(track);
        this.queueTrack(track);
        return track;
    }

    async search(query) {
        const tracks = await loadTracks(query);
        for (let i = 0; i < tracks.length; i++) {
            tracks[i] = this.formatTrack(tracks[i]);
        }
        return tracks;
    }

    skip() {
        if (!this.isPlaying) return;
        this.playNext();
    }

    skipto(to) {
        if (!this.isPlaying) return;
        for (let i = 0; i < to - 1; i++) {
            let sk = this.playlist.shift();
            if (!sk) return this.kill();
            if (this.loopPlaylist) this.playlist.push(sk);
        }
        this.playNext();
    }

    pause() {
        if (!this.isPlaying) return;
        this.player.pause();
    }

    unpause() {
        if (!this.isPlaying) return;
        this.player.pause(false);
    }

    seek(to) {
        if (!this.isPlaying) return;
        if (this.playlist[0].seekable) return;
        this.player.seek(to);
    }

    volume(vol) {
        if (!this.isPlaying) return;
        this.player.volume(vol);
    }

    loop() {
        this.loopPlaylist = !this.loadPlaylist;
    }

    // Track playing

    playNext() {
        const next = this.playlist.shift();
        if (!next) return this.kill();

        if (this.loopPlaylist) this.playlist.push(next);

        this.isPlaying = true;
        this.currentTrack = next;
        this.player.play(next.code);

        const { author, title } = next;
        this.broadcast(`Now playing ${title} uploaded by ${author}`);
    }

    queueTrack(track) {
        if (this.playlist.length < 600)
            this.playlist.push(track);

        if (!this.isPlaying) this.playNext();
    }

    formatTrack(track) {
        const { info } = track;
        return {
            code: track.track,
            title: info.title,
            author: info.author,
            length: info.length,
            seekable: info.isSeekable,
            url: info.uri
        }
    }

    onPlayerEnd(data) {
        if (data.reason === "REPLACED") return;
        if (this.playlist.length < 1) {
            this.broadcast("You've run out of tunes, queue up some more!");
            this.kill();
        }
        this.playNext();
    }

    onPlayerError(error) {
        this.kill();
        console.log(error);
    }

    setBroadcast(channel) {
        this._broadcastChannel = channel;
    }

    broadcast(msg) {
        if (!this._broadcastChannel) return;
        this._broadcastChannel.send(`**:musical_note: | ${msg}**`);
    }

    setDJ(id) {
        this.dj = id;
    }

    isDJ(id, channel) {
        if (id === this.dj) return true;
        const perms = channel.permissionsFor(id);
        if (perms.has("MANAGE_MESSAGES") || perms.has("MANAGE_GUILD")) return true;
        return false;
    }

    kill() {
        this.isPlaying = false;
        this.playlist = [];

        this.player.stop();

        this.client.player.kill(this.guild);
    }

    // Playlist handling

    playlistInfo(page = 1) {
        if (!this.isPlaying) return;
        if (!this.currentTrack) return;

        let tracks = [];
        const pages = new Paginator(this.playlist, 20);
        pages.loopPage(page, (item, i) => {
            tracks.push({
                title: item.title,
                author: item.author,
                url: item.url,
                length: new Duration(item.length),
                index: i
            });
        })
        const pageCount = pages.pageCount;
        let totalLength = this.playlist.reduce((a, b) => ({ length: a.length + b.length })).length;
        totalLength = new Duration(totalLength);
        const nowPlaying = pullProps(this.currentTrack, ["title", "author", "length", "url"]);
        nowPlaying.length = new Duration(nowPlaying.length);

        return { tracks, totalLength, nowPlaying, pageCount };
    }

    shufflePlaylist() {
        if (!this.isPlaying) return;
        this.playlist = shuffleArray(this.playlist);
    }

    async loadPlaylist(urls) {
        let playlist = [];
        for (let i = 0; i < urls.length; i++) {
            let track = await loadTracks(urls[i]);
            track = this.formatTrack(track);
            playlist.push(track);
        }
        this.playlist = playlist;
        this.playNext();
    }

    savePlaylist() {
        let saved = [];
        for (let i = 0; i < this.playlist.length; i++) {
            saved.push(this.playlist[i].url);
        }
        return saved;
    }

    constructor(player, channel, _broadcastChannel) {
        this.guild = channel.guild;
        this.channel = channel;
        this.client = channel.client;
        this.player = player;
        this._broadcastChannel = _broadcastChannel;

        player.on("error", this.onPlayerError.bind(this));
        player.on("end", this.onPlayerEnd.bind(this));
    }
}

async function loadTracks(query, firstResultOnly = false) {
    const res = await snekfetch.get(`http://localhost:2334/loadtracks?identifier=${query}`)
        .set("Authorization", LAVALINK);
    if (!res || !res.body || !res.body.length) throw "No tracks found."
    return firstResultOnly ? res.body[0] : res.body;
}

module.exports = MusicPlayer;

And the piece of program that is causing the error is this:


I tried to replace the “;”, but it doesn’t work. I think it’s a node version error, but I don’t know how to solve it.

1 Like

#23

Has this worked previously?

0 Likes

#24

Yes.
I think. cori tryed to solve a problem with this file in this topic.

1 Like

#25

I have made sure all of cori’s steps were done correctly, but this does not seem to have fixed it, @cori can try taking a look aswell! (warlock-gaming-bot)

0 Likes

#26

I think what’s happening here is that discord.js’s ShardingManager doesn’t respect the --harmony flag when it spawns shards - it starts them up without the flag, so when they try to load your GuildPlayer they still don’t recognize the new “ESNEXT” features like class fields. If you skip the ShardingManager and instead start bot.js directly using the --harmony flag you’ve added I think you’ll avoid the “Unexpected token” error, but my testing in a Remix shows that that leads to another set of errors perhaps related to the LavaLink player configuration, although I can’t be certain.

If you need to use the ShardingManager then you’ll have to rewrite your GuildPlayer code not to use unreleased JavaScript features.

1 Like

#27

OK. And can you please help me doing it? Because I think I don’t know how to do it.

EDIT: The lavalink error is solved by turning on lavalink. But now I’m getting this error:

**EDIT 2:**And by the way, there is a way to get more RAM? Because for this bot I need more RAM.
@cori, can you please help me?

0 Likes

#28

@Callum-OKane can you please take a look?

0 Likes

#29

Your logs are being totally spammed with this:

Fail to create a new connection for the connection pool. Error:{"msg":"Failed to connect to localhost:28015\nFull error:\n{\"errno\":\"ECONNREFUSED\",\"code\":\"ECONNREFUSED\",\"syscall\":\"connect\",\"address\":\"127.0.0.1\",\"port\":28015}","message":"Failed to connect to localhost:28015\nFull error:\n{\"errno\":\"ECONNREFUSED\",\"code\":\"ECONNREFUSED\",\"syscall\":\"connect\",\"address\":\"127.0.0.1\",\"port\":28015}.","isOperational":true}

Fail to create a new connection for the connection pool. Error:{"msg":"Failed to connect to localhost:28015\nFull error:\n{\"errno\":\"ECONNREFUSED\",\"code\":\"ECONNREFUSED\",\"syscall\":\"connect\",\"address\":\"127.0.0.1\",\"port\":28015}","message":"Failed to connect to localhost:28015\nFull error:\n{\"errno\":\"ECONNREFUSED\",\"code\":\"ECONNREFUSED\",\"syscall\":\"connect\",\"address\":\"127.0.0.1\",\"port\":28015}.","isOperational":true}

(node:1840) UnhandledPromiseRejectionWarning: ReqlDriverError: None of the pools have an opened connection and failed to open a new one.
0 Likes

#30

That is solved by turning on Lavalink.

0 Likes

#31

Ok, is everything else working fine now?

edit: I looked at your project and the other error still seems to be there that you said you fixed?

0 Likes

#32

No. I’m still having this error:error

0 Likes

#33

No, your not, thats gone.

Are you looking at the same code?

0 Likes

#34

I turned on lavalink. But my bot don’t turn on because I don’t have RAM.

0 Likes

#35

But your’re right. Now I have this error:

0 Likes

#36

Hey @TheBigerGamer from those errors I see a couple of things. It seems like you’re trying to connect to Trello but are missing an API key for that (or have the wrong one), Then you’ve also got something trying to connect to port 2333, which isn’t a port that Glitch uses. It looks a little like your LavaLink service and connection attempts to port 28015. Do you have a different service that you’re expecting to be running? Where is that port 2333 coming from in your code?

You’d asked previously about additional memory, and while we plan to offer paid options that might include extra memory, we don’t have a timeline for those yet, and until then there’s no option available to you for increasing your project’s memory. If that’s going to be problematic for you long term (and that’s likely, I think, since as I understand things LavaLink is pretty memory-intensive) then you might consider paring down your project to better stay within the memory limitations.

2 Likes

#37

OK. Thakns, but do you know any alternative to lavalink?

0 Likes