Friday, September 20, 2024 1:23:11 AM
> settings

Customize


Authenticate

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

        this.commandParams = {
            serverID: {
                regex: this.util.regex.serverID.base
            },
            reply: {
                regex: /accept|decline/i,
                nullable: true
            }
        };

        this.information = {
            category: "server",
            params: "<server_id>",
            help: "Receive rewards on the specified server. These rewards can be changed on a per server basis"
        };

        this.configuration = {
            allowedInTextChannels: {
                modifiable: true,
                default: true
            },
            cooldown: {
                modifiable: true,
                default: [1, "times"]
            },
            enabled: {
                modifiable: true,
                default: true
            }
        }
    }

    async fromDiscord() {
        if (this.params.reply === "accept") {
            this.acceptRequest();
        } else if (this.params.reply === "decline") {
            this.declineRequest();
            this.resetCooldown();
        } else {
            this.addRequest();
            this.resetCooldown();
        }
    }

    async fromServer() {
        let receipt = JSON.parse(this.params.receipt);
        let message = `${this.info.author.tag}, you got some goodies! The following items were given to you:\n`;
        for (let item of receipt) {
            message += `  - ${item[1]}x ${item[0]}\n`;
        }

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

    async addRequest() {
        let requests = await this.db.getRequests(this.message.author.id, {
            type: "reward",
            serverID: this.params.serverID
        });

        if (!this.util.isEmpty(requests)) {
            return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, you already have pending request.\nYou can accept (\`!reward ${this.params.serverID} accept\`) or decline (\`!reward ${this.params.serverID} decline\`) by replying`);
        }

        let rewards = await this.db.getServerRewards(this.params.serverID);

        if (_.isEmpty(rewards) || (_.isEmpty(rewards.items) && rewards.locker_poptabs === 0 && rewards.player_poptabs === 0 && rewards.respect === 0)) {
            return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, it looks like this server doesn't have any rewards for you to redeem`);
        }

        this.db.addRequest(this.message.author.id, {
            type: "reward",
            serverID: this.params.serverID
        });

        this.ESMBot.send(this.message.channel, {
            author: {
                name: this.client.user.username,
                icon_url: this.client.user.avatarURL()
            },
            description: `**${this.message.author.toString()}, you have requested a reward on \`${this.params.serverID}\`**\nYou are required to be spawned in game and alive to receive awards. ${!_.isEmpty(rewards.items) ? "\n**NOTE:** This reward contains items. We will attempt to add the items to your player, however, if you do not have enough space, **the items will be dropped at your feet.**" : ""}`,
            fields: [{
                    name: "To Accept",
                    value: `Reply with \`!reward ${this.params.serverID} accept\``
                },
                {
                    name: "To Decline",
                    value: `Reply with \`!reward ${this.params.serverID} decline\``
                }
            ],
            footer: {
                text: "This request will decline after 1 day"
            }
        });
    }

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

        if (this.util.isEmpty(request)) {
            this.resetCooldown();
            return this.ESMBot.send(this.message.channel, `${this.info.author.tag}, you do not have any pending requests for rewards on this server`);
        }

        this.ESMBot.send(this.message.channel, "Processing reward request. Please wait...");

        this.db.removeRequest(request.id);

        return this.send({
            server_id: this.params.serverID,
            command: "reward",
            parameters: {
                function_name: "rewardPlayer",
                target_uid: this.params.steamUID
            }
        });
    }

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

        if (this.util.isEmpty(request)) {
            return this.ESMBot.send(this.message.channel, `${this.info.author.tag}, you do not have any pending requests for rewards on this server`);
        }

        this.db.removeRequest(request.id);

        return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, your request for rewards on ${this.params.serverID} has been declined`);
    }
}
All opinions represented herein are my own
- © 2024 itsthedevman
- build 340fbb8