Friday, September 20, 2024 1:43:13 AM
> settings

Customize


Authenticate

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

        this.commandParams = {
            serverID: {
                regex: this.util.regex.serverID.base
            },
            poptabAmount: {
                regex: /-?\d+|half|all|info/
            }
        };

        this.information = {
            category: "server",
            params: "<server_id> <poptab_amount or half or all or info>",
            help: "Gambles either a specified amount, half, or all of the players locker on that server. If the poptab amount is **info**, the bot will return win/loss info of the player for that server"
        };

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

        this.values = {};
    }

    async fromDiscord() {
        if (this.params.poptabAmount === "0") {
            return this.ESMBot.send(this.message.channel, `Nice try, ${this.message.author.toString()}. You cannot gamble nothing 😜`);
        }

        if (this.params.poptabAmount === "info") {
            // Reset the cooldown, no need for it.
            this.db.resetCooldown(this.util.getCommunityID(this.params.serverID), "gamble", this.message.author.id);

            // Get the gambling info
            let info = await this.db.getGambleInfo(this.message.author.id, this.params.serverID);

            return this.ESMBot.send(this.message.channel, {
                title: `Gambling information for ${this.params.serverID}`,
                fields: [{
                        name: "Total Wins",
                        value: info.wins,
                        inline: true
                    },
                    {
                        name: "Total Losses",
                        value: info.loss,
                        inline: true
                    },
                    {
                        name: "Total Poptabs Won",
                        value: info.won_poptabs,
                        inline: true
                    },
                    {
                        name: "Total Poptabs Lost",
                        value: info.loss_poptabs,
                        inline: true
                    },
                    {
                        name: "Current Streak",
                        value: info.current_streak,
                        inline: true
                    },
                    {
                        name: "Longest win streak",
                        value: info.win_streak,
                        inline: true
                    },
                    {
                        name: "Longest losing streak",
                        value: info.loss_streak,
                        inline: true
                    }
                ]
            });
        }

        this.send({
            command: "gamble",
            parameters: {
                function_name: "gamble",
                uid: this.params.steamUID,
                amount: this.params.poptabAmount,
                name: this.message.author.username,
                id: this.message.author.id
            }
        });
    }

    async fromServer() {
        if (this.util.isEmpty(this.params)) return;

        let user = await this.ESMBot.getUser(this.info.author.id);

        if (this.util.isEmpty(user)) {
            this.ESMBot.send(this.info.channel, "I was unable to find the specified user.");
            return this.ESMBot.send(
                this.info.channel,
                `Anywho, someone just ${this.params.type} **${this.params.amount}** poptabs. They now have **${this.params.locker_after}** poptabs.\nUnfortunately, this won't count towards this person statistics.\nSomeone should tell someone about this so it can maybe be fixed... Maybe`
            );
        }

        this.values = {
            communityID: this.util.getCommunityID(this.serverID),
            serverID: this.serverID,
            serverName: (await this.db.getServer(this.serverID)).name,
            userName: user.username,
            userTag: user.toString(),
            amountChanged: this.params.amount,
            amountGambled: this.info.parameters.amount,
            lockerBefore: this.params.locker_before,
            lockerAfter: this.params.locker_after
        };

        let gambleStats = {};
        switch (this.params.type) {
            case "won":
                gambleStats = await this.db.addGambleWin(user.id, this.serverID, parseInt(this.params.amount));
                break;
            case "loss":
                gambleStats = await this.db.addGambleLoss(user.id, this.serverID, parseInt(this.params.amount));
                break;
            default:
                return this.ESMBot.send(this.info.channel, "It appears someone has attempted to break the code on the server and sent an invalid type. Please yell at the server owner to double check their code and tell them to stop messing with shit");
        }

        if (this.util.isEmpty(gambleStats)) {
            return this.ESMBot.send(this.info.channel, this.ESMBot.errors.format(user, "GENERIC_ERROR"));
        }

        this.ESMBot.send(this.info.channel, await this.buildEmbed(gambleStats));
    }

    async buildEmbed(gambleStats) {
        let embed = new this.ESMBot.discord.MessageEmbed()
            .setAuthor(_.truncate(`[${this.values.serverID}] ${this.values.serverName}`, {
                length: this.util.embed.TITLE_LENGTH_MAX
            }), this.client.user.avatarURL())
            .setTimestamp(this.ESMBot.moment.utc())
            .setFooter(`Current streak: ${gambleStats.current_streak}`);

        // last_action will either be a "win" or "loss"
        let notifications = await this.db.getNotifications(this.values.communityID, gambleStats.last_action);
        if (_.isEmpty(notifications)) {
            notifications = this.getBackupNotification(gambleStats.last_action);
        }

        let notification = this.util.selectRandom(notifications);
        this.formatNotification(notification, embed);

        return embed;
    }

    formatNotification(notification, embed) {
        if (!_.isEmpty(notification.title)) {
            embed.setTitle(
                _.truncate(
                    this.replaceTemplates(notification.title), {
                        length: this.util.embed.TITLE_LENGTH_MAX
                    }
                )
            );
        }

        if (!_.isEmpty(notification.message)) {
            embed.setDescription(
                _.truncate(
                    this.replaceTemplates(notification.message), {
                        length: this.util.embed.DESCRIPTION_LENGTH_MAX
                    }
                )
            );
        }

        if (notification.color === "random") {
            embed.setColor(this.util.selectRandom(_.values(this.ESMBot.colors)));
        } else {
            embed.setColor(notification.color);
        }
    }

    replaceTemplates(template) {
        return template
            .replace(/{{\s*serverid\s*}}/gi, this.values.serverID)
            .replace(/{{\s*servername\s*}}/gi, this.values.serverName)
            .replace(/{{\s*communityid\s*}}/gi, this.values.communityID)
            .replace(/{{\s*username\s*}}/gi, this.values.userName)
            .replace(/{{\s*usertag\s*}}/gi, this.values.userTag)
            .replace(/{{\s*amountchanged\s*}}/gi, this.values.amountChanged)
            .replace(/{{\s*amountgambled\s*}}/gi, this.values.amountGambled)
            .replace(/{{\s*lockerbefore\s*}}/gi, this.values.lockerBefore)
            .replace(/{{\s*lockerafter\s*}}/gi, this.values.lockerAfter);
    }

    getBackupNotification(type) {
        if (type === "win") {
            return [{
                category: "gambling",
                color: "random",
                message: "You won **{{ amountChanged }}** poptabs. You now have **{{ lockerAfter }}** poptabs",
                title: "Damn, you won **{{ userName }}**",
                type: "win",
            }, {
                category: "gambling",
                color: "random",
                message: "You're one step closer to a new attack helicopter! You won **{{ amountChanged }}** poptabs, and have a total of **{{ lockerAfter }}** poptabs in your locker",
                title: "Guess what **{{ userName }}**?",
                type: "win",
            }, {
                category: "gambling",
                color: "random",
                message: "Congratulations, you won **{{ amountChanged }}** poptabs, this is just enough to afford one month of FarmersOnly.com, **PREMIUM**",
                title: "**{{ userName }}**",
                type: "win",
            }, {
                category: "gambling",
                color: "random",
                message: "You just won **{{ amountChanged }}** poptabs. You have **{{ lockerAfter }}** poptabs in your locker",
                title: "Slow down **{{ userName }}**",
                type: "win",
            }, {
                category: "gambling",
                color: "random",
                message: "Yes, you just won **{{ amountChanged }}** poptabs. Yes, your locker balance is now **{{ lockerAfter }}** poptabs. But did you know gambling is a serious addiction? We are concerned about you, so we found [this](http://www.gamblersanonymous.org/ga/) for you.",
                title: "Hey **{{ userName }}**",
                type: "win",
            }, {
                category: "gambling",
                color: "random",
                message: "They just won **{{ amountChanged }}** poptabs. **{{ userTag }}**, you have **{{ lockerAfter }}** poptabs now.",
                title: "Looks like drinks are on **{{ userName }}**",
                type: "win",
            }];
        } else {
            return [{
                category: "gambling",
                color: "random",
                message: "You lost **{{ amountChanged }}** poptabs. You have **{{ lockerAfter }}** poptabs remaining.",
                title: "Maybe next time **{{ userName }}**!",
                type: "loss",
            }, {
                category: "gambling",
                color: "random",
                message: "You just lost **{{ amountChanged }}** poptabs, but that doesn't have to happen anymore. This [website](http://www.gamblersanonymous.org/ga/) has some awesome resources to help you get over your addiction. If you won't do it for yourself, please, do it for your community. You have **{{ lockerAfter }}** poptabs left.",
                title: "Look **{{ userName }}**, there is still hope.",
                type: "loss",
            }, {
                category: "gambling",
                color: "random",
                message: "I'm serious, don't. You can't afford to. You lost **{{ amountChanged }}** poptabs and you have **{{ lockerAfter }}** poptabs left.",
                title: "Don't quit your day job **{{ userName }}**",
                type: "loss",
            }, {
                category: "gambling",
                color: "random",
                message: "You lost **{{ amountChanged }}** poptabs. You have **{{ lockerAfter }}** poptabs remaining.",
                title: "RIPs, you are not a winner **{{ userName }}**",
                type: "loss",
            }, {
                category: "gambling",
                color: "random",
                message: "You just lost **{{ amountChanged }}** poptabs, but don't worry! Try gambling again and maybe you'll win it back. :wink:\nYou have **{{ lockerAfter }}** poptabs left.",
                title: "I'm sorry **{{ userName }}**",
                type: "loss",
            }];
        }
    }
}
All opinions represented herein are my own
- © 2024 itsthedevman
- build 340fbb8