Friday, September 20, 2024 1:27:33 AM
> settings

Customize


Authenticate

> info.js
const ESMCommand = require("../esm_command");
const Fs = require("fs");
const Path = require("path");

module.exports = class ESMCommand_Info extends ESMCommand {
    constructor(bot) {
        super(bot);
        this.permissions = {
            disabled_in_player_mode: true,
            requires_registration: true,
            requires_server: true,
            text_only: true
        };

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

            id: {
                regex: this.util.regex.targetOrTerritory.base
            }
        };

        this.information = {
            category: "server",
            params: "<server_id> <target_uid | @user | territory_id>",
            help: "Returns information about a player or a territory. This command is only available to certain roles and can only be used in a text channel"
        };

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

    async fromDiscord() {
        if (this.util.regex.target.only.test(this.params.id)) {
            let info = await this.db.getSteamUIDFromTag(this.params.id);
            if (!info.uid) {
                return this.ESMBot.send(this.message.channel, info.message);
            }

            this.send({
                serverID: this.params.serverID,
                command: "info",
                parameters: {
                    query: "player_info",
                    uid: info.uid
                }
            });
        } else if (this.util.regex.territoryID.only.test(this.params.id)) {
            this.send({
                serverID: this.params.serverID,
                command: "info",
                parameters: {
                    query: "territory_info",
                    territory_id: this.params.id
                }
            });
        } else {
            this.ESMBot.send(this.message.channel, `${this.message.author}, you provided an invalid target. Target can only be a discord tag, a steam UID, or a territory ID`);
        }
    }

    async fromServer() {
        if (this.util.isEmpty(this.params)) {
            return this.ESMBot.send(this.info.channel, `${this.info.author.tag}, I didn't find any information for your query`);
        }

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

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

            let flagPath = this.util.getFlagPath(this.params.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 this.params.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 this.params.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, this.params.level);

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

            let price = this.params.level * this.params.object_count * territoryInfo.price_per_object;

            embed.addField("\u200B", "__Territory Stats__")
                .addField(`Flag Status`, `${this.params.flag_stolen ? "Stolen!" : "Secure"}`, true)
                .addField(`Level`, `${this.params.level}`, true)
                .addField(`Radius`, `${this.params.radius}m`, true)
                .addField(`Object Count`, `${this.params.object_count}`, true)
                .addField("\u200B", "__Payment Information__")
                .addField(`Last Paid`, `${moment.utc(this.params.last_paid_at).format("dddd, MMMM Do YYYY, h:mm:ss a Z")}`)
                .addField("Next due date", `${moment.utc(this.params.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`, `${this.params.owner_name} (${this.params.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);
        } else {
            let embed = {
                title: `Player Information on ${this.serverID}`,
                color: this.ESMBot.colors.BLUE,
                fields: [{
                        name: "Player Name",
                        value: this.params.name
                    },
                    {
                        name: "Money",
                        value: `${this.params.money} poptabs`,
                        inline: true
                    },
                    {
                        name: "Locker",
                        value: `${this.params.locker} poptabs`,
                        inline: true
                    },
                    {
                        name: "Respect",
                        value: `${this.params.score}`,
                        inline: true
                    },
                    {
                        name: "Health",
                        value: `${Math.round(100 - (parseFloat(this.params.damage) * 100))}%`,
                        inline: true
                    },
                    {
                        name: "Hunger",
                        value: `${Math.round(parseFloat(this.params.hunger))}%`,
                        inline: true
                    },
                    {
                        name: "Thirst",
                        value: `${Math.round(parseFloat(this.params.thirst))}%`,
                        inline: true
                    },
                    {
                        name: "Kills/Deaths",
                        value: `${this.params.kills} / ${this.params.deaths}`
                    },
                    {
                        name: "KD Ratio",
                        value: this.params.deaths === 0 ? "0" : `${parseInt(this.params.kills) / parseInt(this.params.deaths)}`
                    }
                ]
            };

            if (!this.util.isEmpty(this.params.territories) && this.params.territories !== "{}") {
                embed.fields.push({
                    name: "Territories",
                    value: this.util.objectToStringFormatted(JSON.parse(this.params.territories))
                });
            }

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