utility: add pomelo command, also rename username helper to formatUsername
This commit is contained in:
		
							parent
							
								
									6c35ce2ae8
								
							
						
					
					
						commit
						cabe10c230
					
				
					 6 changed files with 80 additions and 26 deletions
				
			
		| 
						 | 
				
			
			@ -48,7 +48,7 @@ global.hf = {
 | 
			
		|||
};
 | 
			
		||||
 | 
			
		||||
const CommandDispatcher = require("./lib/commandDispatcher.js");
 | 
			
		||||
const {pomello} = require("./lib/utils.js");
 | 
			
		||||
const {formatUsername} = require("./lib/utils.js");
 | 
			
		||||
 | 
			
		||||
for (const file of fs.readdirSync(resolve(__dirname, "modules"))) {
 | 
			
		||||
  require(resolve(__dirname, "modules", file));
 | 
			
		||||
| 
						 | 
				
			
			@ -84,7 +84,10 @@ bot.on("messageUpdate", (msg, oldMsg) => {
 | 
			
		|||
 | 
			
		||||
bot.once("ready", async () => {
 | 
			
		||||
  logger.info("hf:main", "Connected to Discord.");
 | 
			
		||||
  logger.info("hf:main", `Logged in as: ${pomello(bot.user)} (${bot.user.id})`);
 | 
			
		||||
  logger.info(
 | 
			
		||||
    "hf:main",
 | 
			
		||||
    `Logged in as: ${formatUsername(bot.user)} (${bot.user.id})`
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  const channel = await bot.getDMChannel(config.owner_id);
 | 
			
		||||
  if (channel) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,7 +10,7 @@ function pastelize(id) {
 | 
			
		|||
  return parseInt(hex, 16);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function pomello(user) {
 | 
			
		||||
function formatUsername(user) {
 | 
			
		||||
  return user.discriminator && user.discriminator != "0"
 | 
			
		||||
    ? `${user.username}#${user.discriminator}`
 | 
			
		||||
    : `@${user.username}`;
 | 
			
		||||
| 
						 | 
				
			
			@ -328,7 +328,7 @@ async function lookupUser(msg, str, filter) {
 | 
			
		|||
      selection.push({
 | 
			
		||||
        value: user,
 | 
			
		||||
        key: user.id,
 | 
			
		||||
        display: `${pomello(user)}${user.nick ? ` (${user.nick})` : ""}`,
 | 
			
		||||
        display: `${formatUsername(user)}${user.nick ? ` (${user.nick})` : ""}`,
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			@ -403,7 +403,7 @@ function getUploadLimit(guild) {
 | 
			
		|||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  pastelize,
 | 
			
		||||
  pomello,
 | 
			
		||||
  formatUsername,
 | 
			
		||||
  getTopColor,
 | 
			
		||||
  safeString,
 | 
			
		||||
  formatTime,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,7 @@ const {
 | 
			
		|||
  formatTime,
 | 
			
		||||
  hastebin,
 | 
			
		||||
  parseHtmlEntities,
 | 
			
		||||
  pomello,
 | 
			
		||||
  formatUsername,
 | 
			
		||||
  safeString,
 | 
			
		||||
} = require("../lib/utils.js");
 | 
			
		||||
const GoogleImages = require("google-images");
 | 
			
		||||
| 
						 | 
				
			
			@ -234,7 +234,7 @@ poll.callback = async function (msg, line, [topic, ...options]) {
 | 
			
		|||
  if (arrOptions.length < 2) return "A minimum of two options are required.";
 | 
			
		||||
 | 
			
		||||
  const reactions = [];
 | 
			
		||||
  let displayString = `**${pomello(
 | 
			
		||||
  let displayString = `**${formatUsername(
 | 
			
		||||
    msg.author
 | 
			
		||||
  )}** has started a poll:\n## __${topic}__\n`;
 | 
			
		||||
  for (let i = 0; i < arrOptions.length; i++) {
 | 
			
		||||
| 
						 | 
				
			
			@ -262,7 +262,7 @@ vote.callback = async function (msg, line, topic, {maybe}) {
 | 
			
		|||
  topic = topic.join(" ");
 | 
			
		||||
 | 
			
		||||
  return {
 | 
			
		||||
    content: `**${pomello(
 | 
			
		||||
    content: `**${formatUsername(
 | 
			
		||||
      msg.author
 | 
			
		||||
    )}** has started a vote:\n## __${topic}__\n<:ms_tick:503341995348066313>: Yes\n<:ms_cross:503341994974773250>: No${
 | 
			
		||||
      maybe ? "\n<:ms_tilda:581268710925271095>: Maybe/Uncertain" : ""
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
const Command = require("../lib/command.js");
 | 
			
		||||
const CATEGORY = "moderation";
 | 
			
		||||
 | 
			
		||||
const {lookupUser, pomello} = require("../lib/utils.js");
 | 
			
		||||
const {lookupUser, formatUsername} = require("../lib/utils.js");
 | 
			
		||||
 | 
			
		||||
const tidy = new Command("tidy");
 | 
			
		||||
tidy.addAlias("prune");
 | 
			
		||||
| 
						 | 
				
			
			@ -25,7 +25,7 @@ tidy.callback = async function (msg, line, [subcommand, count, extra]) {
 | 
			
		|||
      });
 | 
			
		||||
      await msg.channel.deleteMessages(
 | 
			
		||||
        messages.map((m) => m.id),
 | 
			
		||||
        `Message purge by ${pomello(msg.author)}`
 | 
			
		||||
        `Message purge by ${formatUsername(msg.author)}`
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      return `Deleted ${messages.length} message(s).`;
 | 
			
		||||
| 
						 | 
				
			
			@ -41,7 +41,9 @@ tidy.callback = async function (msg, line, [subcommand, count, extra]) {
 | 
			
		|||
        });
 | 
			
		||||
        await msg.channel.deleteMessages(
 | 
			
		||||
          messages.filter((m) => m.author.id == user.id).map((m) => m.id),
 | 
			
		||||
          `Message purge by ${pomello(msg.author)} targeting ${pomello(user)}`
 | 
			
		||||
          `Message purge by ${formatUsername(
 | 
			
		||||
            msg.author
 | 
			
		||||
          )} targeting ${formatUsername(user)}`
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        return `Deleted ${messages.length} message(s).`;
 | 
			
		||||
| 
						 | 
				
			
			@ -54,7 +56,7 @@ tidy.callback = async function (msg, line, [subcommand, count, extra]) {
 | 
			
		|||
      });
 | 
			
		||||
      await msg.channel.deleteMessages(
 | 
			
		||||
        messages.filter((m) => msg.author.bot).map((m) => m.id),
 | 
			
		||||
        `Message purge by ${pomello(msg.author)} targeting bots`
 | 
			
		||||
        `Message purge by ${formatUsername(msg.author)} targeting bots`
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      return `Deleted ${messages.length} message(s).`;
 | 
			
		||||
| 
						 | 
				
			
			@ -69,7 +71,7 @@ tidy.callback = async function (msg, line, [subcommand, count, extra]) {
 | 
			
		|||
      });
 | 
			
		||||
      await msg.channel.deleteMessages(
 | 
			
		||||
        messages.filter((m) => m.content.indexOf(count) > -1).map((m) => m.id),
 | 
			
		||||
        `Message purge by ${pomello(msg.author)} targeting "${count}"`
 | 
			
		||||
        `Message purge by ${formatUsername(msg.author)} targeting "${count}"`
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      return `Deleted ${messages.length} message(s).`;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,7 @@ const Command = require("../lib/command.js");
 | 
			
		|||
const {
 | 
			
		||||
  formatTime,
 | 
			
		||||
  parseHtmlEntities,
 | 
			
		||||
  pomello,
 | 
			
		||||
  formatUsername,
 | 
			
		||||
  selectionMessage,
 | 
			
		||||
} = require("../lib/utils.js");
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -774,7 +774,7 @@ command.callback = async function (
 | 
			
		|||
              key: item.id,
 | 
			
		||||
              display: (item.title ?? item.url).substr(0, 100),
 | 
			
		||||
              description: hasManageMessages
 | 
			
		||||
                ? `Added by: ${pomello(user)}`
 | 
			
		||||
                ? `Added by: ${formatUsername(user)}`
 | 
			
		||||
                : "",
 | 
			
		||||
            };
 | 
			
		||||
          }),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,7 +9,7 @@ const {
 | 
			
		|||
  formatTime,
 | 
			
		||||
  hastebin,
 | 
			
		||||
  lookupUser,
 | 
			
		||||
  pomello,
 | 
			
		||||
  formatUsername,
 | 
			
		||||
  safeString,
 | 
			
		||||
} = require("../lib/utils.js");
 | 
			
		||||
const {getNamesFromString} = require("../lib/unicode.js");
 | 
			
		||||
| 
						 | 
				
			
			@ -366,7 +366,7 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
 | 
			
		|||
      }
 | 
			
		||||
 | 
			
		||||
      const baseEmbed = {
 | 
			
		||||
        title: `Avatar for \`${pomello(member)}\``,
 | 
			
		||||
        title: `Avatar for \`${formatUsername(member)}\``,
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      const normalAvatar = member.user.avatar;
 | 
			
		||||
| 
						 | 
				
			
			@ -411,7 +411,7 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
 | 
			
		|||
    const guild = msg.channel.guild || hf.bot.guilds.get(msg.guildID);
 | 
			
		||||
 | 
			
		||||
    const baseEmbed = {
 | 
			
		||||
      title: `Avatar for \`${pomello(msg.author)}\``,
 | 
			
		||||
      title: `Avatar for \`${formatUsername(msg.author)}\``,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    const normalAvatar = msg.author.avatar;
 | 
			
		||||
| 
						 | 
				
			
			@ -512,7 +512,7 @@ banner.callback = async function (msg, line, [user], {server, guild}) {
 | 
			
		|||
  return {
 | 
			
		||||
    embeds: [
 | 
			
		||||
      {
 | 
			
		||||
        title: `Banner for \`${pomello(userObj)}\``,
 | 
			
		||||
        title: `Banner for \`${formatUsername(userObj)}\``,
 | 
			
		||||
        url,
 | 
			
		||||
        image: {
 | 
			
		||||
          url,
 | 
			
		||||
| 
						 | 
				
			
			@ -580,7 +580,7 @@ lookupinvite.callback = async function (msg, line) {
 | 
			
		|||
    const inviter = invite.inviter
 | 
			
		||||
      ? {
 | 
			
		||||
          name: "Inviter",
 | 
			
		||||
          value: `**${pomello(invite.inviter)}** (${invite.inviter.id})`,
 | 
			
		||||
          value: `**${formatUsername(invite.inviter)}** (${invite.inviter.id})`,
 | 
			
		||||
          inline: false,
 | 
			
		||||
        }
 | 
			
		||||
      : null;
 | 
			
		||||
| 
						 | 
				
			
			@ -865,16 +865,16 @@ flagdump.callback = async function (msg, line, [numOrMention], {id, list}) {
 | 
			
		|||
    if (!user) {
 | 
			
		||||
      return "User not cached.";
 | 
			
		||||
    } else {
 | 
			
		||||
      return `\`${pomello(user)}\`'s public flags:\n\`\`\`${flagFromInt(
 | 
			
		||||
      return `\`${formatUsername(user)}\`'s public flags:\n\`\`\`${flagFromInt(
 | 
			
		||||
        user.publicFlags
 | 
			
		||||
      )}\`\`\``;
 | 
			
		||||
    }
 | 
			
		||||
  } else if (!isNaN(num)) {
 | 
			
		||||
    return `\`\`\`\n${flagFromInt(num)}\`\`\``;
 | 
			
		||||
  } else {
 | 
			
		||||
    return `\`${pomello(msg.author)}\`'s public flags:\n\`\`\`${flagFromInt(
 | 
			
		||||
      msg.author.publicFlags
 | 
			
		||||
    )}\`\`\``;
 | 
			
		||||
    return `\`${formatUsername(
 | 
			
		||||
      msg.author
 | 
			
		||||
    )}\`'s public flags:\n\`\`\`${flagFromInt(msg.author.publicFlags)}\`\`\``;
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
hf.registerCommand(flagdump);
 | 
			
		||||
| 
						 | 
				
			
			@ -1030,7 +1030,8 @@ presence.callback = async function (msg, line) {
 | 
			
		|||
  }
 | 
			
		||||
 | 
			
		||||
  if (target) {
 | 
			
		||||
    if (!target.clientStatus) return `**${pomello(target)}** is offline.`;
 | 
			
		||||
    if (!target.clientStatus)
 | 
			
		||||
      return `**${formatUsername(target)}** is offline.`;
 | 
			
		||||
 | 
			
		||||
    const icons = [];
 | 
			
		||||
    for (const platform of Object.keys(target.clientStatus)) {
 | 
			
		||||
| 
						 | 
				
			
			@ -1251,7 +1252,7 @@ presence.callback = async function (msg, line) {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    return {
 | 
			
		||||
      content: `Presence for **${pomello(target)}**: ${icons.join(" ")}`,
 | 
			
		||||
      content: `Presence for **${formatUsername(target)}**: ${icons.join(" ")}`,
 | 
			
		||||
      embeds,
 | 
			
		||||
      files,
 | 
			
		||||
    };
 | 
			
		||||
| 
						 | 
				
			
			@ -1261,4 +1262,52 @@ presence.callback = async function (msg, line) {
 | 
			
		|||
};
 | 
			
		||||
hf.registerCommand(presence);
 | 
			
		||||
 | 
			
		||||
const POMELO_REGEX = /^[a-z0-9._]{1,32}$/;
 | 
			
		||||
const pomelo = new Command("pomelo");
 | 
			
		||||
pomelo.category = CATEGORY;
 | 
			
		||||
pomelo.helpText = "Check to see if a username is taken or not";
 | 
			
		||||
pomelo.usage = "[username] <...username>";
 | 
			
		||||
pomelo.callback = async function (msg, line) {
 | 
			
		||||
  if (!line || line === "") return "Arguments required.";
 | 
			
		||||
 | 
			
		||||
  const usernames = line.toLowerCase().split(" ");
 | 
			
		||||
 | 
			
		||||
  if (usernames.length == 1) {
 | 
			
		||||
    const name = usernames[0];
 | 
			
		||||
    if (name.length > 32 || !POMELO_REGEX.test(name))
 | 
			
		||||
      return {reaction: "\u{1f6ab}"};
 | 
			
		||||
 | 
			
		||||
    const res = await hf.bot.requestHandler.request(
 | 
			
		||||
      "POST",
 | 
			
		||||
      "/unique-username/username-attempt-unauthed",
 | 
			
		||||
      false,
 | 
			
		||||
      {username: name}
 | 
			
		||||
    );
 | 
			
		||||
    return {reaction: res.taken ? "\u274c" : "\u2705"};
 | 
			
		||||
  } else {
 | 
			
		||||
    const lines = [];
 | 
			
		||||
 | 
			
		||||
    for (const name of usernames) {
 | 
			
		||||
      if (name.length > 32 || !POMELO_REGEX.test(name)) {
 | 
			
		||||
        lines.push(`\u{1f6ab} \`${name}\``);
 | 
			
		||||
      } else {
 | 
			
		||||
        try {
 | 
			
		||||
          const res = await hf.bot.requestHandler.request(
 | 
			
		||||
            "POST",
 | 
			
		||||
            "/unique-username/username-attempt-unauthed",
 | 
			
		||||
            false,
 | 
			
		||||
            {username: name}
 | 
			
		||||
          );
 | 
			
		||||
          lines.push(`${res.taken ? "\u274c" : "\u2705"} \`${name}\``);
 | 
			
		||||
        } catch {
 | 
			
		||||
          lines.push(`\u26a0\ufe0f \`${name}\``);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return lines.join("\n");
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
hf.registerCommand(pomelo);
 | 
			
		||||
 | 
			
		||||
// }}}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue