utility: add pomelo command, also rename username helper to formatUsername

This commit is contained in:
Cynthia Foxwell 2023-10-05 13:41:47 -06:00
parent 6c35ce2ae8
commit cabe10c230
6 changed files with 80 additions and 26 deletions

View file

@ -48,7 +48,7 @@ global.hf = {
}; };
const CommandDispatcher = require("./lib/commandDispatcher.js"); 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"))) { for (const file of fs.readdirSync(resolve(__dirname, "modules"))) {
require(resolve(__dirname, "modules", file)); require(resolve(__dirname, "modules", file));
@ -84,7 +84,10 @@ bot.on("messageUpdate", (msg, oldMsg) => {
bot.once("ready", async () => { bot.once("ready", async () => {
logger.info("hf:main", "Connected to Discord."); 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); const channel = await bot.getDMChannel(config.owner_id);
if (channel) { if (channel) {

View file

@ -10,7 +10,7 @@ function pastelize(id) {
return parseInt(hex, 16); return parseInt(hex, 16);
} }
function pomello(user) { function formatUsername(user) {
return user.discriminator && user.discriminator != "0" return user.discriminator && user.discriminator != "0"
? `${user.username}#${user.discriminator}` ? `${user.username}#${user.discriminator}`
: `@${user.username}`; : `@${user.username}`;
@ -328,7 +328,7 @@ async function lookupUser(msg, str, filter) {
selection.push({ selection.push({
value: user, value: user,
key: user.id, 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 = { module.exports = {
pastelize, pastelize,
pomello, formatUsername,
getTopColor, getTopColor,
safeString, safeString,
formatTime, formatTime,

View file

@ -7,7 +7,7 @@ const {
formatTime, formatTime,
hastebin, hastebin,
parseHtmlEntities, parseHtmlEntities,
pomello, formatUsername,
safeString, safeString,
} = require("../lib/utils.js"); } = require("../lib/utils.js");
const GoogleImages = require("google-images"); 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."; if (arrOptions.length < 2) return "A minimum of two options are required.";
const reactions = []; const reactions = [];
let displayString = `**${pomello( let displayString = `**${formatUsername(
msg.author msg.author
)}** has started a poll:\n## __${topic}__\n`; )}** has started a poll:\n## __${topic}__\n`;
for (let i = 0; i < arrOptions.length; i++) { for (let i = 0; i < arrOptions.length; i++) {
@ -262,7 +262,7 @@ vote.callback = async function (msg, line, topic, {maybe}) {
topic = topic.join(" "); topic = topic.join(" ");
return { return {
content: `**${pomello( content: `**${formatUsername(
msg.author msg.author
)}** has started a vote:\n## __${topic}__\n<:ms_tick:503341995348066313>: Yes\n<:ms_cross:503341994974773250>: No${ )}** has started a vote:\n## __${topic}__\n<:ms_tick:503341995348066313>: Yes\n<:ms_cross:503341994974773250>: No${
maybe ? "\n<:ms_tilda:581268710925271095>: Maybe/Uncertain" : "" maybe ? "\n<:ms_tilda:581268710925271095>: Maybe/Uncertain" : ""

View file

@ -1,7 +1,7 @@
const Command = require("../lib/command.js"); const Command = require("../lib/command.js");
const CATEGORY = "moderation"; const CATEGORY = "moderation";
const {lookupUser, pomello} = require("../lib/utils.js"); const {lookupUser, formatUsername} = require("../lib/utils.js");
const tidy = new Command("tidy"); const tidy = new Command("tidy");
tidy.addAlias("prune"); tidy.addAlias("prune");
@ -25,7 +25,7 @@ tidy.callback = async function (msg, line, [subcommand, count, extra]) {
}); });
await msg.channel.deleteMessages( await msg.channel.deleteMessages(
messages.map((m) => m.id), messages.map((m) => m.id),
`Message purge by ${pomello(msg.author)}` `Message purge by ${formatUsername(msg.author)}`
); );
return `Deleted ${messages.length} message(s).`; return `Deleted ${messages.length} message(s).`;
@ -41,7 +41,9 @@ tidy.callback = async function (msg, line, [subcommand, count, extra]) {
}); });
await msg.channel.deleteMessages( await msg.channel.deleteMessages(
messages.filter((m) => m.author.id == user.id).map((m) => m.id), 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).`; return `Deleted ${messages.length} message(s).`;
@ -54,7 +56,7 @@ tidy.callback = async function (msg, line, [subcommand, count, extra]) {
}); });
await msg.channel.deleteMessages( await msg.channel.deleteMessages(
messages.filter((m) => msg.author.bot).map((m) => m.id), 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).`; return `Deleted ${messages.length} message(s).`;
@ -69,7 +71,7 @@ tidy.callback = async function (msg, line, [subcommand, count, extra]) {
}); });
await msg.channel.deleteMessages( await msg.channel.deleteMessages(
messages.filter((m) => m.content.indexOf(count) > -1).map((m) => m.id), 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).`; return `Deleted ${messages.length} message(s).`;

View file

@ -7,7 +7,7 @@ const Command = require("../lib/command.js");
const { const {
formatTime, formatTime,
parseHtmlEntities, parseHtmlEntities,
pomello, formatUsername,
selectionMessage, selectionMessage,
} = require("../lib/utils.js"); } = require("../lib/utils.js");
@ -774,7 +774,7 @@ command.callback = async function (
key: item.id, key: item.id,
display: (item.title ?? item.url).substr(0, 100), display: (item.title ?? item.url).substr(0, 100),
description: hasManageMessages description: hasManageMessages
? `Added by: ${pomello(user)}` ? `Added by: ${formatUsername(user)}`
: "", : "",
}; };
}), }),

View file

@ -9,7 +9,7 @@ const {
formatTime, formatTime,
hastebin, hastebin,
lookupUser, lookupUser,
pomello, formatUsername,
safeString, safeString,
} = require("../lib/utils.js"); } = require("../lib/utils.js");
const {getNamesFromString} = require("../lib/unicode.js"); const {getNamesFromString} = require("../lib/unicode.js");
@ -366,7 +366,7 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
} }
const baseEmbed = { const baseEmbed = {
title: `Avatar for \`${pomello(member)}\``, title: `Avatar for \`${formatUsername(member)}\``,
}; };
const normalAvatar = member.user.avatar; 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 guild = msg.channel.guild || hf.bot.guilds.get(msg.guildID);
const baseEmbed = { const baseEmbed = {
title: `Avatar for \`${pomello(msg.author)}\``, title: `Avatar for \`${formatUsername(msg.author)}\``,
}; };
const normalAvatar = msg.author.avatar; const normalAvatar = msg.author.avatar;
@ -512,7 +512,7 @@ banner.callback = async function (msg, line, [user], {server, guild}) {
return { return {
embeds: [ embeds: [
{ {
title: `Banner for \`${pomello(userObj)}\``, title: `Banner for \`${formatUsername(userObj)}\``,
url, url,
image: { image: {
url, url,
@ -580,7 +580,7 @@ lookupinvite.callback = async function (msg, line) {
const inviter = invite.inviter const inviter = invite.inviter
? { ? {
name: "Inviter", name: "Inviter",
value: `**${pomello(invite.inviter)}** (${invite.inviter.id})`, value: `**${formatUsername(invite.inviter)}** (${invite.inviter.id})`,
inline: false, inline: false,
} }
: null; : null;
@ -865,16 +865,16 @@ flagdump.callback = async function (msg, line, [numOrMention], {id, list}) {
if (!user) { if (!user) {
return "User not cached."; return "User not cached.";
} else { } else {
return `\`${pomello(user)}\`'s public flags:\n\`\`\`${flagFromInt( return `\`${formatUsername(user)}\`'s public flags:\n\`\`\`${flagFromInt(
user.publicFlags user.publicFlags
)}\`\`\``; )}\`\`\``;
} }
} else if (!isNaN(num)) { } else if (!isNaN(num)) {
return `\`\`\`\n${flagFromInt(num)}\`\`\``; return `\`\`\`\n${flagFromInt(num)}\`\`\``;
} else { } else {
return `\`${pomello(msg.author)}\`'s public flags:\n\`\`\`${flagFromInt( return `\`${formatUsername(
msg.author.publicFlags msg.author
)}\`\`\``; )}\`'s public flags:\n\`\`\`${flagFromInt(msg.author.publicFlags)}\`\`\``;
} }
}; };
hf.registerCommand(flagdump); hf.registerCommand(flagdump);
@ -1030,7 +1030,8 @@ presence.callback = async function (msg, line) {
} }
if (target) { if (target) {
if (!target.clientStatus) return `**${pomello(target)}** is offline.`; if (!target.clientStatus)
return `**${formatUsername(target)}** is offline.`;
const icons = []; const icons = [];
for (const platform of Object.keys(target.clientStatus)) { for (const platform of Object.keys(target.clientStatus)) {
@ -1251,7 +1252,7 @@ presence.callback = async function (msg, line) {
} }
return { return {
content: `Presence for **${pomello(target)}**: ${icons.join(" ")}`, content: `Presence for **${formatUsername(target)}**: ${icons.join(" ")}`,
embeds, embeds,
files, files,
}; };
@ -1261,4 +1262,52 @@ presence.callback = async function (msg, line) {
}; };
hf.registerCommand(presence); 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);
// }}} // }}}