Friday, September 20, 2024 2:26:59 AM
> settings

Customize


Authenticate

> discord_message_channel.js
const ESMCommand = require("../esm_command");
module.exports = class ESMCommand_DiscordMessageChannel extends ESMCommand {
    constructor(bot) {
        super(bot);
        this.permissions = {
            restricted: true
        };
    }

    async fromServer() {
        let maybeJSON = this.util.tryParseJSON(this.params.message);

        // This is a message
        if (_.isNull(maybeJSON)) {
            return this.ESMBot.send(this.params.channelID, this.params.message);
        }

        // this is a JSON array
        /*
            Expectation:
                0: Title
                1: Description
                2: Fields
                    0: Name
                    1: Value
                    2: Inline? <optional, defaults to false>
                3: Color <optional, default to blue>
        */

        try {
            let embed = new this.ESMBot.discord.MessageEmbed()
                .setTimestamp(moment.utc())
                .setColor(this.ESMBot.colors.BLUE)
                .setAuthor(this.ESMBot.client.user.username, this.client.user.avatarURL());

            // Title
            if (!_.isEmpty(maybeJSON[0])) {
                embed.setTitle(_.truncate(`${maybeJSON[0]}`, {
                    length: this.util.embed.TITLE_LENGTH_MAX
                }));
            }

            // Description
            if (!_.isEmpty(maybeJSON[1])) {
                embed.setDescription(_.truncate(`${maybeJSON[1]}`, {
                    length: this.util.embed.DESCRIPTION_LENGTH_MAX
                }));
            }

            // Fields
            if (!_.isEmpty(maybeJSON[2])) {
                for (let field of maybeJSON[2]) {
                    if (_.isArray(field[1]) || _.isPlainObject(field[1])) {
                        field[1] = _.truncate(`${this.ESMBot.inspect(field[1])}`, {
                            length: this.util.embed.FIELD_VALUE_LENGTH_MAX
                        })
                    } else {
                        field[1] = _.truncate(`${field[1]}`, {
                            length: this.util.embed.FIELD_VALUE_LENGTH_MAX
                        })
                    }

                    embed.addField(
                        // Name
                        _.truncate(`${field[0]}`, {
                            length: this.util.embed.FIELD_NAME_LENGTH_MAX
                        }),

                        // Value
                        field[1],


                        // Inline
                        field[2] ? field[2] : false
                    );
                }
            }

            // Color
            if (!_.isEmpty(maybeJSON[3])) {
                let color = _.toUpper(maybeJSON[3]);
                // Pick a color
                if (["RED", "BLUE", "GREEN", "PURPLE", "PINK", "ORANGE", "WHITE"].includes(color)) {
                    embed.setColor(this.ESMBot.colors[_.toUpper(color)])
                } else if (/^\#[A-F0-9]{6}$/i.test(color)) {
                    embed.setColor(color);
                }
            }

            this.ESMBot.send(this.params.channelID, embed);
        } catch (err) {
            return this.ESMBot.send(this.params.channelID, `I was sent the following message\n\`\`\`${maybeJSON}\`\`\`\nI hit the following error: \`${err}\``);
        }
    }
}
All opinions represented herein are my own
- © 2024 itsthedevman
- build 340fbb8