Friday, September 20, 2024 2:35:32 AM
> settings

Customize


Authenticate

> playermode.js
const ESMCommand = require("../esm_command");
module.exports = class ESMCommand_PlayerMode extends ESMCommand {
    constructor(bot) {
        super(bot);
        this.permissions = {
            dm_only: true
        };

        this.commandParams = {
            communityID: {
                regex: this.util.regex.communityID.base
            },
            state: {
                regex: /enable|disable|accept|decline/
            }
        };

        this.information = {
            category: "system",
            params: "<community_id> <enable/disable>",
            help: "Enables or Disables Player Mode for a Community. For more information about Player Mode, please read this [article](https://www.esmbot.com/wiki/player_mode)"
        };
    }

    async fromDiscord() {
        this.community = await this.db.getCommunity(this.params.communityID);

        if (this.util.isEmpty(this.community)) {
            return this.ESMBot.send(this.message.channel, this.ESMBot.errors.format(this.message.author, "INVALID_COMMUNITY"));
        }

        let guild = await this.ESMBot.getGuild(this.community.guild_id);

        if (!guild.available) {
            return this.ESMBot.send(this.message.channel, this.ESMBot.errors.format(this.message.author, "GUILD_UNAVAILABLE"));
        }

        // Admin check
        let members = await guild.members.fetch();
        let guildMember = members.get(this.message.author.id);
        if (this.util.isEmpty(guildMember) || !guildMember.permissions.has("ADMINISTRATOR")) {
            return this.ESMBot.send(this.message.channel, this.ESMBot.errors.format(this.message.author, "UHH_UHH_UHH"));
        }

        switch (this.params.state) {
            case "enable":
                if (this.community.player_mode_enabled) {
                    return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, Player Mode is already enabled for this Community`);
                }
                this.addRequestForEnable();
                break;
            case "disable":
                if (!this.community.player_mode_enabled) {
                    return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, Player Mode is already disabled for this Community`);
                }
                this.addRequestForDisable();
                break;
            case "accept":
                this.acceptRequest();
                break;
            case "decline":
                this.declineRequest();
                break;
        }
    }

    async addRequestForEnable() {
        let servers = await this.db.getServers(this.community.id);
        if (!_.isEmpty(servers)) {
            return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, there are servers registered with this community. Please log into our web portal (https://www.esmbot.com/portal), remove all registered servers and then run this command again`);
        }

        let requests = await this.db.getRequests(this.message.author.id, {
            type: "playerMode",
            communityID: this.community.id
        });

        if (!this.util.isEmpty(requests)) {
            return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, you have a pending request to enable player mode.\nTo accept: \`!playermode ${this.community.id} accept\`\nTo decline: \`!playermode ${this.community.id} decline\``);
        }

        this.db.addRequest(this.message.author.id, {
            type: "playerMode",
            communityID: this.community.id
        });

        let embed = new this.ESMBot.discord.MessageEmbed()
            .setColor(this.ESMBot.colors.BLUE)
            .setAuthor(this.client.user.username, this.client.user.avatarURL())
            .setDescription(`${this.message.author.toString()}, you have requested to enable Player Mode for this community: \`${this.community.id}\`\nFor more information about Player Mode, please read this [article](https://www.esmbot.com/wiki/player_mode).`)
            .addField("To Accept", `Reply with \`!playermode ${this.community.id} accept\``)
            .addField("To Decline", `Reply with \`!playermode ${this.community.id} decline\``)
            .setFooter("This request will decline after 1 day");

        this.ESMBot.send(this.message.channel, embed);
    }

    async addRequestForDisable() {
        let requests = await this.db.getRequests(this.message.author.id, {
            type: "playerMode",
            communityID: this.community.id
        });

        if (!this.util.isEmpty(requests)) {
            return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, you have a pending request to disable player mode.\nTo accept: \`!playermode ${this.community.id} accept\`\nTo decline: \`!playermode ${this.community.id} decline\``);
        }

        this.db.addRequest(this.message.author.id, {
            type: "playerMode",
            communityID: this.community.id
        });

        let embed = new this.ESMBot.discord.MessageEmbed()
            .setColor(this.ESMBot.colors.BLUE)
            .setAuthor(this.client.user.username, this.client.user.avatarURL())
            .setDescription(`${this.message.author.toString()}, you have requested to disable Player Mode for this community: \`${this.community.id}\`\nFor more information about Player Mode, please read this [article](https://www.esmbot.com/wiki/player_mode).`)
            .addField("To Accept", `Reply with \`!playermode ${this.community.id} accept\``)
            .addField("To Decline", `Reply with \`!playermode ${this.community.id} decline\``)
            .setFooter("This request will decline after 1 day");

        this.ESMBot.send(this.message.channel, embed);
    }

    async acceptRequest() {
        let request = await this.db.getRequests(this.message.author.id, {
            type: "playerMode"
        });

        if (this.util.isEmpty(request)) {
            return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, you do not have any pending requests for Player Mode`);
        }

        this.db.setPlayerMode(request.communityID, !this.community.player_mode_enabled);

        this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, request accepted. Player Mode has been ${!this.community.player_mode_enabled ? "enabled" : "disabled" } for \`${request.communityID}\``);

        this.db.removeRequest(request.id);
    }

    async declineRequest() {
        let request = await this.db.getRequests(this.message.author.id, {
            type: "playerMode"
        });

        if (this.util.isEmpty(request)) {
            return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, you do not have any pending requests for Player Mode`);
        }

        this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, request declined. No changes have been made`);
        this.db.removeRequest(request.id);
    }
}
All opinions represented herein are my own
- © 2024 itsthedevman
- build 340fbb8