Friday, September 20, 2024 1:24:38 AM
> settings

Customize


Authenticate

> territories.js
const ESMCommand = require("../esm_command");
const Fs = require("fs");
module.exports = class ESMCommand_Territories extends ESMCommand {
    constructor(bot) {
        super(bot);
        this.permissions = {
            requires_registration: true,
            requires_server: true
        };

        this.commandParams = {
            serverID: {
                regex: this.util.regex.serverID.base
            }
        };

        this.information = {
            category: "server",
            params: "<server_id>",
            help: "Displays all territories and information about them associated with the player"
        };

        this.configuration = {
            allowedInTextChannels: {
                modifiable: true,
                default: false
            },
            cooldown: {
                modifiable: true,
                default: [2, "seconds"]
            },
            enabled: {
                modifiable: true,
                default: true
            }
        };
    }

    async fromDiscord() {
        this.send({
            serverID: this.params.serverID,
            command: "territories",
            parameters: {
                query: "list_territories",
                uid: this.params.steamUID
            }
        });
    }

    async fromServer() {
        if (this.util.isEmpty(this.params)) {
            return this.ESMBot.send(this.info.channel, `${this.info.author.tag}, you do not have any territories on this server`);
        }

        // Quick fix for only one territory
        if (!_.isArray(this.params)) {
            this.params = [this.params];
        }

        for (let territory of this.params) {
            let embed = new this.ESMBot.discord.MessageEmbed()
                .setTitle(`Territory "${territory.territory_name}" [id: ${territory.id}]`)
                .setColor(this.ESMBot.colors.BLUE);

            let message = {
                files: [],
                embed: embed
            };

            let flagPath = this.util.getFlagPath(territory.flag_texture);
            if (Fs.existsSync(flagPath)) {
                message.files = [{
                    attachment: flagPath,
                    name: "territory_flag.png"
                }];

                embed.setThumbnail("attachment://territory_flag.png");
            }

            let build_rights = [];
            let moderators = [];
            let memberString = "";

            for (let rights of territory.build_rights) {
                let newString = `${rights[0]} (${rights[1]})\n`;

                if (memberString.length + newString.length < 1024) {
                    memberString += newString;
                } else {
                    build_rights.push(memberString);
                    memberString = "";
                    memberString += newString;
                }
            }

            build_rights.push(memberString);

            memberString = "";
            for (let moderator of territory.moderators) {
                let newString = `${moderator[0]} (${moderator[1]})\n`;

                if (memberString.length + newString.length < 1024) {
                    memberString += newString;
                } else {
                    moderators.push(memberString);
                    memberString = "";
                    memberString += newString;
                }
            }

            moderators.push(memberString);

            let territoryInfo = await this.db.getTerritoryInfo(this.serverID, territory.level);

            if (territoryInfo == null) {
                return this.ESMBot.send(this.info.channel, this.ESMBot.errors(this.info.author.tag, "TERRITORY_INFO_NOT_FOUND"));
            }

            let price = territory.level * territory.object_count * territoryInfo.price_per_object;

            embed.addField("\u200B", "__Territory Stats__")
                .addField(`Flag Status`, `${territory.flag_stolen ? "Stolen!" : "Secure"}`, true)
                .addField(`Level`, `${territory.level}`, true)
                .addField(`Radius`, `${territory.radius}m`, true)
                .addField(`Object Count`, `${territory.object_count}`, true)
                .addField("\u200B", "__Payment Information__")
                .addField(`Last Paid`, `${moment.utc(territory.last_paid_at).format("dddd, MMMM Do YYYY, h:mm:ss a Z")}`)
                .addField("Next due date", `${moment.utc(territory.last_paid_at).add(territoryInfo.territory_lifetime, "days").format("dddd, MMMM Do YYYY, h:mm:ss a Z")}`, true)
                .addField("Price to renew protection", `${price + Math.round(price * (territoryInfo.pay_tax / 100))} poptabs ${territoryInfo.pay_tax !== 0 ? `(${territoryInfo.pay_tax}% tax added)` : ""}`);

            if (territoryInfo.upgrade_price != null) {
                price = territoryInfo.upgrade_price + Math.round(territoryInfo.upgrade_price * (territoryInfo.upgrade_tax / 100));

                embed.addField("\u200B", "__Upgrade Information__")
                    .addField("Price", `${price} poptabs ${territoryInfo.upgrade_tax !== 0 ? `(${territoryInfo.upgrade_tax}% tax added)` : ""}`, true)
                    .addField("Radius", `${territoryInfo.upgrade_radius}`, true)
                    .addField("Object Count", `${territoryInfo.upgrade_object_count}`, true);
            }


            embed.addField("\u200B", "__Territory Members__")
                .addField(`Owner`, `${territory.owner_name} (${territory.owner_uid})`);

            for (let moderator of moderators) {
                embed.addField("Moderators", moderator);
            }

            for (let builder of build_rights) {
                embed.addField("Build Rights", builder);
            }

            this.ESMBot.send(this.info.channel, message);
        }
    }
}
All opinions represented herein are my own
- © 2024 itsthedevman
- build 340fbb8