{"size": 2930, "ext": "js", "max_stars_repo_path": "src/util/defaults/collector/MessageCollector.js", "max_stars_repo_name": "Forbidden-Duck/eris.js-commando", "max_stars_repo_stars_event_min_datetime": "2020-10-26T02:26:26.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-03T07:11:28.000Z", "max_issues_repo_path": "src/util/defaults/collector/MessageCollector.js", "max_issues_repo_name": "Forbidden-Duck/eris.js-commando", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/util/defaults/collector/MessageCollector.js", "max_forks_repo_name": "Forbidden-Duck/eris.js-commando", "max_forks_repo_forks_event_min_datetime": "2021-05-07T15:09:35.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-01T12:32:33.000Z"}
{"max_stars_count": 5, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 36.1728395062, "max_line_length": 92, "alphanum_fraction": 0.5938566553}
const { EventEmitter } = require("events");

module.exports = class MessageCollector extends EventEmitter {
    constructor(client, channel, filter, timeout = 60000) {
        super();
        this.filter = filter;
        this.client = client;
        this.channel = channel;
        this.message = null;
        this.end = false;
        this._handleCollect = this._handleCollect.bind(this);
        this._handleChannelDeletion = this._handleChannelDeletion.bind(this);
        this._handleGuildDeletion = this._handleChannelDeletion.bind(this);
        this._idleTimeout = setTimeout(() => this._stop("idle"), timeout);
        this.client.awaitMessage = true;
        this.client.on("messageCreate", this._handleCollect);
        this.client.on("channelDelete", this._handleChannelDeletion);
        this.client.on("guildDelete", this._handleGuildDeletion);
        this.once("end", () => {
            this.client.removeListener("messageCreate", this._handleCollect);
            this.client.removeListener("channelDelete", this._handleChannelDeletion);
            this.client.removeListener("guildDelete", this._handleGuildDeletion);
        });

        if (this.client.awaitMessages == undefined) {
            this.client.awaitMessages = {};
        }
        if (this.client.awaitMessages[channel.id] == undefined) {
            this.client.awaitMessages[channel.id] = [];
        }
        this.client.awaitMessages[channel.id].push(filter);
    }

    _handleCollect(message) {
        const prefix = this.client.util.getPrefix(this.channel.guild);
        if (message.content.toLowerCase().startsWith(prefix.toLowerCase())) {
            message.content = message.content.substring(prefix.length);
        }
        if (message && this.filter(message)) {
            this.message = message;
            this._stop();
        }
    }

    _stop(reason = "user") {
        if (this.end) {
            return;
        }
        if (this._idleTimeout) {
            clearTimeout(this._idleTimeout);
            this._idleTimeout = null;
        }
        this.end = true;

        const filterIndex = this.client.awaitMessages[this.channel.id].indexOf(this.filter);
        if (filterIndex > -1) {
            /*
            Set at a 3 second delay to allow for external use.
            This can be used to prevent commands
            and the command processor will receive it before it's removed
            */
            setTimeout(() => {
                this.client.awaitMessages[this.channel.id].splice(filterIndex, 1);
            }, 1000);
        }

        this.emit("end", this.message, reason);
    }

    _handleChannelDeletion(channel) {
        if (channel.id === this.channel.id) {
            this._stop("channelDelete");
        }
    }

    _handleGuildDeletion(guild) {
        if (this.channel.guild && guild.id === this.channel.guild.id) {
            this._stop("guildDelete");
        }
    }
}