Friday, September 20, 2024 1:42:18 AM
> settings

Customize


Authenticate

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

        this.commandParams = {
            recipient: {
                regex: this.util.regex.broadcast.base
            },
            message: {
                regex: /(.|[\r\n])+/igm,
                preserve_case: true
            }
        };

        this.information = {
            category: "server",
            params: "<server_id or all> <message>",
            help: "Broadcasts a message via an XM8 notification to all players who allow custom XM8 message with that server\nIf **all** is used in place of the server ID, the message will go out to all servers under this community\nIf **test** is used in place of the server ID, ESMBot will send you a preview of the message without broadcasting it to users."
        };

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

    async fromDiscord() {
        if (this.params.message.length > 1750) {
            return this.ESMBot.send(this.message.channel, this.ESMBot.errors.format(this.message.author, "MESSAGE_TOO_LONG"));
        }

        let serverIDs = [],
            communityID = "",
            userIDs = [];

        if (!["all", "test"].includes(this.params.recipient)) {
            if (!await this.db.isValidServer(this.params.recipient)) {
                return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, ${this.params.recipient} is not a valid server ID`);
            }

            let community = await this.db.getCommunity(this.params.recipient);

            if (community.guild_id !== this.message.guild.id) {
                return this.ESMBot.send(this.message.channel, this.errors.format(this.message.author, "UHH_UHH_UHH"));
            }

            serverIDs.push(this.params.recipient);
            communityID = this.util.getCommunityID(this.params.recipient);
        } else {
            if (!this.util.isGuildAvailable(this.message.guild)) {
                return this.ESMBot.send(this.message.channel, this.ESMBot.errors.format(this.message.author, "GUILD_UNAVAILABLE"));
            }

            communityID = await this.db.getCommunityID(this.message.guild.id);

            if (this.util.isNull(communityID)) {
                return this.ESMBot.send(this.message.channel, this.ESMBot.errors.format(this.message.author, "FAILED_TO_FIND_COMMUNITY"));
            }

            if (this.params.recipient === "test") {
                return this.ESMBot.send(this.message.channel, {
                    title: `Broadcast message from ${communityID}`,
                    description: this.params.message,
                    color: this.ESMBot.colors.ORANGE
                });
            }

            let allServers = await this.db.getServerIDs(communityID);

            if (this.util.isEmpty(allServers)) {
                return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, you don't have any servers registered with me.`);
            }

            serverIDs = allServers;
        }

        for (let serverID of serverIDs) {
            (async () => {
                let users = await this.db.getBroadcastUsers(serverID);
                for (let user of users) {
                    if (userIDs.includes(user.id)) continue;
                    this.ESMBot.send(user.id, {
                        title: `Broadcast message from ${communityID}`,
                        description: this.params.message,
                        color: this.ESMBot.colors.ORANGE
                    });
                    userIDs.push(user.id);
                }
            })();
        }

        this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, they've got mail. 📫`);
    }
}
All opinions represented herein are my own
- © 2024 itsthedevman
- build 340fbb8