diff --git a/src/index.js b/src/index.js index bbf6d86..76d1be1 100644 --- a/src/index.js +++ b/src/index.js @@ -9,6 +9,7 @@ const apikeys = require("../apikeys.json"); const Command = require("./lib/command.js"); const events = require("./lib/events.js"); const timer = require("./lib/timer.js"); +const {pomello} = require("./lib/utils.js"); const bot = new Dysnomia.Client(config.token, { defaultImageFormat: "png", @@ -82,14 +83,7 @@ bot.on("messageUpdate", (msg, oldMsg) => { bot.once("ready", async () => { logger.info("hf:main", "Connected to Discord."); - logger.info( - "hf:main", - `Logged in as: ${ - bot.user.discriminator != "0" - ? `${bot.user.username}#${bot.user.discriminator}` - : bot.user.username - } (${bot.user.id})` - ); + logger.info("hf:main", `Logged in as: ${pomello(bot.user)} (${bot.user.id})`); const channel = await bot.getDMChannel(config.owner_id); if (channel) { diff --git a/src/lib/utils.js b/src/lib/utils.js index a910d5b..5183799 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -10,6 +10,12 @@ function pastelize(id) { return parseInt(hex, 16); } +function pomello(user) { + return user.discriminator && user.discriminator != "0" + ? `${user.username}#${user.discriminator}` + : `@${user.username}`; +} + function getTopColor(msg, id, fallback = 0x7289da) { if (!msg.guildID) return fallback; const guild = msg.channel.guild || hf.bot.guilds.get(msg.guildID); @@ -322,11 +328,7 @@ async function lookupUser(msg, str, filter) { selection.push({ value: user, key: user.id, - display: `${ - user.discriminator != "0" - ? `${user.username}#${user.discriminator}` - : user.username - }${user.nick ? ` (${user.nick})` : ""}`, + display: `${pomello(user)}${user.nick ? ` (${user.nick})` : ""}`, }); } } @@ -401,6 +403,7 @@ function getUploadLimit(guild) { module.exports = { pastelize, + pomello, getTopColor, safeString, formatTime, diff --git a/src/modules/misc.js b/src/modules/misc.js index 436c9c2..bd7a78b 100644 --- a/src/modules/misc.js +++ b/src/modules/misc.js @@ -4,10 +4,11 @@ const CATEGORY = "misc"; const {librex} = require("../../config.json"); const { - hastebin, - safeString, - parseHtmlEntities, formatTime, + hastebin, + parseHtmlEntities, + pomello, + safeString, } = require("../lib/utils.js"); const GoogleImages = require("google-images"); const {tinycolor, random: randomColor} = require("@ctrl/tinycolor"); @@ -233,11 +234,9 @@ poll.callback = async function (msg, line, [topic, ...options]) { if (arrOptions.length < 2) return "A minimum of two options are required."; const reactions = []; - let displayString = `**${ - msg.author.discriminator != "0" - ? `${msg.author.username}#${msg.author.discriminator}` - : msg.author.username - }** has started a poll:\n## __${topic}__\n`; + let displayString = `**${pomello( + msg.author + )}** has started a poll:\n## __${topic}__\n`; for (let i = 0; i < arrOptions.length; i++) { displayString += (i === 9 ? "\ud83d\udd1f" : `${i + 1}\u20e3`) + @@ -263,11 +262,9 @@ vote.callback = async function (msg, line, topic, {maybe}) { topic = topic.join(" "); return { - content: `**${ - msg.author.discriminator != "0" - ? `${msg.author.username}#${msg.author.discriminator}` - : msg.author.username - }** has started a vote:\n## __${topic}__\n<:ms_tick:503341995348066313>: Yes\n<:ms_cross:503341994974773250>: No${ + content: `**${pomello( + 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" : "" }`, addReactions: [ diff --git a/src/modules/moderation.js b/src/modules/moderation.js index e0c622e..c836d45 100644 --- a/src/modules/moderation.js +++ b/src/modules/moderation.js @@ -1,7 +1,7 @@ const Command = require("../lib/command.js"); const CATEGORY = "moderation"; -const {lookupUser} = require("../lib/utils.js"); +const {lookupUser, pomello} = require("../lib/utils.js"); const tidy = new Command("tidy"); tidy.addAlias("prune"); @@ -25,11 +25,7 @@ tidy.callback = async function (msg, line, [subcommand, count, extra]) { }); await msg.channel.deleteMessages( messages.map((m) => m.id), - `Message purge by ${ - msg.author.discriminator != "0" - ? `${msg.author.username}#${msg.author.discriminator}` - : msg.author.username - }` + `Message purge by ${pomello(msg.author)}` ); return `Deleted ${messages.length} message(s).`; @@ -45,15 +41,7 @@ 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 ${ - msg.author.discriminator != "0" - ? `${msg.author.username}#${msg.author.discriminator}` - : msg.author.username - } targeting ${ - user.discriminator != "0" - ? `${user.username}#${user.discriminator}` - : user.username - }` + `Message purge by ${pomello(msg.author)} targeting ${pomello(user)}` ); return `Deleted ${messages.length} message(s).`; @@ -66,11 +54,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 ${ - msg.author.discriminator != "0" - ? `${msg.author.username}#${msg.author.discriminator}` - : msg.author.username - } targeting bots` + `Message purge by ${pomello(msg.author)} targeting bots` ); return `Deleted ${messages.length} message(s).`; @@ -85,11 +69,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 ${ - msg.author.discriminator != "0" - ? `${msg.author.username}#${msg.author.discriminator}` - : msg.author.username - } targeting "${count}"` + `Message purge by ${pomello(msg.author)} targeting "${count}"` ); return `Deleted ${messages.length} message(s).`; diff --git a/src/modules/music.js b/src/modules/music.js index 5bcea6d..393c185 100644 --- a/src/modules/music.js +++ b/src/modules/music.js @@ -6,8 +6,9 @@ const ffprobe = require("node-ffprobe"); const Command = require("../lib/command.js"); const { formatTime, - selectionMessage, parseHtmlEntities, + pomello, + selectionMessage, } = require("../lib/utils.js"); const REGEX_YOUTUBE = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/; @@ -753,11 +754,7 @@ command.callback = async function ( key: item.id, display: (item.title ?? item.url).substr(0, 100), description: hasManageMessages - ? `Added by: ${ - user.discriminator != "0" - ? `${user.username}#${user.discriminator}` - : user.username - }` + ? `Added by: ${pomello(user)}` : "", }; }), diff --git a/src/modules/utility.js b/src/modules/utility.js index 6c211a1..c3fe7f5 100644 --- a/src/modules/utility.js +++ b/src/modules/utility.js @@ -6,9 +6,10 @@ const CATEGORY = "utility"; const sharp = require("sharp"); const { + formatTime, hastebin, lookupUser, - formatTime, + pomello, safeString, } = require("../lib/utils.js"); const {getNamesFromString} = require("../lib/unicode.js"); @@ -235,11 +236,7 @@ avatar.callback = async function (msg, line, [user], {server, guild}) { } const baseEmbed = { - title: `Avatar for \`${ - member.discriminator != "0" - ? `${member.username}#${member.discriminator}` - : member.username - }\``, + title: `Avatar for \`${pomello(member)}\``, }; const normalAvatar = member.user.avatar; @@ -284,11 +281,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 \`${ - msg.author.discriminator != "0" - ? `${msg.author.username}#${msg.author.discriminator}` - : msg.author.username - }\``, + title: `Avatar for \`${pomello(msg.author)}\``, }; const normalAvatar = msg.author.avatar; @@ -389,7 +382,7 @@ banner.callback = async function (msg, line, [user], {server, guild}) { return { embeds: [ { - title: `Banner for \`${userObj.username}#${userObj.discriminator}\``, + title: `Banner for \`${pomello(userObj)}\``, url, image: { url, @@ -457,11 +450,7 @@ lookupinvite.callback = async function (msg, line) { const inviter = invite.inviter ? { name: "Inviter", - value: `**${ - invite.inviter.discriminator && invite.inviter.discriminator != "0" - ? `${invite.inviter.username}#${invite.inviter.discriminator}` - : `@${invite.inviter.username}` - }** (${invite.inviter.id})`, + value: `**${pomello(invite.inviter)}** (${invite.inviter.id})`, inline: false, } : null; @@ -733,20 +722,16 @@ flagdump.callback = async function (msg, line, [numOrMention], {id, list}) { if (!user) { return "User not cached."; } else { - return `\`${ - user.discriminator != "0" - ? `${user.username}#${user.discriminator}` - : user.username - }\`'s public flags:\n\`\`\`${flagFromInt(user.publicFlags)}\`\`\``; + return `\`${pomello(user)}\`'s public flags:\n\`\`\`${flagFromInt( + user.publicFlags + )}\`\`\``; } } else if (!isNaN(num)) { return `\`\`\`\n${flagFromInt(num)}\`\`\``; } else { - return `\`${ - msg.author.discriminator != "0" - ? `${msg.author.username}#${msg.author.discriminator}` - : msg.author.username - }\`'s public flags:\n\`\`\`${flagFromInt(msg.author.publicFlags)}\`\`\``; + return `\`${pomello(msg.author)}\`'s public flags:\n\`\`\`${flagFromInt( + msg.author.publicFlags + )}\`\`\``; } }; hf.registerCommand(flagdump); @@ -902,12 +887,7 @@ presence.callback = async function (msg, line) { } if (target) { - if (!target.clientStatus) - return `**${ - target.discriminator != "0" - ? `${target.username}#${target.discriminator}` - : target.username - }** is offline.`; + if (!target.clientStatus) return `**${pomello(target)}** is offline.`; let icons = ""; for (const platform of Object.keys(target.clientStatus)) { @@ -1121,11 +1101,7 @@ presence.callback = async function (msg, line) { } return { - content: `Presence for **${ - target.discriminator != "0" - ? `${target.username}#${target.discriminator}` - : target.username - }**: ${icons.trim()}`, + content: `Presence for **${pomello(target)}**: ${icons.trim()}`, embeds, files, };