Saamstep / modmail

Support tickets managed in Discord with ease.
https://samstep.net
49 stars 25 forks source link

Fixed `announce.js` command... #42

Closed JAVAB3ANS closed 3 years ago

JAVAB3ANS commented 3 years ago

There was a bit of trouble with Modmail announce.js code so here's my spin on it to help with the issue:

const { Command } = require("discord.js-commando");

module.exports = class announceCommand extends Command {
  constructor(client) {
    super(client, {
      name: "announce",
      group: "admins",
      memberName: "announce", 
      throttling: {
          usages: 2,
          duration: 5,
            },
      description: "Make a formatted announcement using embed data", 
      args: [
        {
          key: "option",
          prompt: "Please choose a valid option \`edit, append, embed\`",
          type: "string",
          oneOf: ["edit", "append", "embed"],
        },
        {
          key: "id",
          prompt: "Please provide a message id to edit or mention a channel to send this message to",
          type: "string",
          validate: (id) => {
            if(id.match(/^[0-9]*$/)) {
              return true;
            } else {
                return "Please enter a proper snowflake!"
            }
          }
        },
        {
          key: "title",
          prompt: "Please provide some title text.",
          type: "string",
          validate: (title) => {
            if (title.length < 256) {
              return true;
            } else {
                return "Please enter embed title under 256 characters!"
            }
          }
        },

        {
          key: "body",
          prompt: "Please provide some body text.",
          type: "string",
          validate: (body) => {
            if (body.length < 2048) {
              return true;
            } else {
                return "Please enter embed description under 2048 characters!";
            }
          }
        },
        {
          key: "color",
          prompt: "Please provide some color.",
          type: "string",
        },
        {
          key: "image",
          prompt: "Please provide an image URL.",
          type: "string",
          validate: (image) => {
            if (image.match(/([a-z\-_0-9\/\:\.]*\.(jpg|jpeg|png|gif))/i)) {
              return true;
            } else {
                return "Please enter a proper image URL with the listed extensions!";
            }
          }
        },
        {
          key: "footer",
          prompt: "Please provide some footer text.",
          type: "string",
          validate: (footer) => {
            if (footer.length < 2048) {
              return true;
            } else {
                return "Please enter embed footer under 2048 characters!";
            }
          }
        },

      ],
    });
  }

  async run(message, { option, id, title, body, color, image, footer }) {
    switch (option) {
      case "edit":
        try {
          message.channel.messages.fetch(id).then((m) => {
            m.edit({
              embed: {
        title: title,
                description: body,
        color: color,
        footer: footer,
        image: image
              },
            });
          });
        } catch (e) {
          return this.client.error(e + "Channel not found, you must run in same channel as message!", message);
        }
        break;
      case "append":
        try {
          message.channel.messages.fetch(id).then((m) => {
            m.edit({
              embed: {
                description: m.embeds[0].description + " " + body,
              },
            });
          });
        } catch (e) {
          return this.client.error("Channel not found, you must run in same channel as message!", message);
        }
        break; 
      case "embed":
        try {
          let announceChannel = this.client.channels.cache.get(`${id.replace(/</g, "").replace(/>/g, "").replace(/#/g, "")}`);
          announceChannel.send({ embed: { title: title, description: body, image: { url: image }, footer: { text: footer }, color: color } });
        } catch (e) {
          return this.client.error(e, message);
        }
        break;    }
    message.delete();
  }
};

Let me know your thoughts boss :)

Saamstep commented 3 years ago

Holup this bot shouldn't really have this command lol. Also what was the issue it was having before?

JAVAB3ANS commented 3 years ago

It had an issue like this with the normal code:

image

Something along the lines of this as well:

image

To be honest, announce.js still pretty good for modmail bot though -- serves its purpose well in a super configurable way 💯

Saamstep commented 3 years ago

Command works as intended when used correctly. While I think I could make it more user friendly, an announce command is not of the highest priority for a Modmail system. I am also not a huge fan of commands with too many arguments (4-5 is quite a lot). Which is why I made this command to intake JSON generated from tools such as an embed visualizer.

Proper usage would be [p]announce embed #channel-to-send

Bot will then say: @User, Please provide some body text (embed format) Respond with cancel to cancel the command. The command will automatically be cancelled in 30 seconds.

This is where I would send my entire JSON body all formatted in one single message instead of multiple arguments.

{
    "title": "title ~~(did you know you can have markdown here too?)~~",
    "description": "this supports [named links](https://discordapp.com) on top of the previously shown subset of markdown. ```\nyes, even code blocks```",
    "url": "https://discordapp.com",
    "color": 14293695,
    "timestamp": "2021-05-11T20:17:33.775Z",
    "footer": {
      "icon_url": "https://cdn.discordapp.com/embed/avatars/0.png",
      "text": "footer text"
    },
    "thumbnail": {
      "url": "https://cdn.discordapp.com/embed/avatars/0.png"
    },
    "image": {
      "url": "https://cdn.discordapp.com/embed/avatars/0.png"
    },
    "author": {
      "name": "author name",
      "url": "https://discordapp.com",
      "icon_url": "https://cdn.discordapp.com/embed/avatars/0.png"
    },
    "fields": [
      {
        "name": "🤔",
        "value": "some of these properties have certain limits..."
      },
      {
        "name": "😱",
        "value": "try exceeding some of them!"
      },
      {
        "name": "🙄",
        "value": "an informative error should show up, and this view will remain as-is until all issues are fixed"
      },
      {
        "name": "<:thonkang:219069250692841473>",
        "value": "these last two",
        "inline": true
      },
      {
        "name": "<:thonkang:219069250692841473>",
        "value": "are inline fields",
        "inline": true
      }
    ]
  }

And that outputs: output

This allows users to create little templates and copy and paste and use as needed giving them access to all the fields they want in an embed and only limited by Discord's 2000 character limit. Like I said, not very user friendly, unless you are familiar with JSON syntax, but if there are more requests to make something more user friendly I will consider it. I will also update the documentation in the help command to make it more clear.

JAVAB3ANS commented 3 years ago

Ye great stuff boss