const { Command } = require('../../../../interfaces'); const similarity = require('similarity'); const { Util } = require('../../../../../util'); class UserCommand extends Command { constructor(client) { super(client, { name: 'user', module: 'information', description: 'Display information about user.', guildOnly: true, arguments: [ { name: 'search', type: 'STRING', types: ['FLAG', 'VERBAL'], required: true }, { name: 'discriminator', aliases: [ 'discrim', 'disc' ], type: 'BOOLEAN', types: ['FLAG'] } ] }); this.client = client; } async execute(message, { params, args }) { let response = ''; if(args.search && args.search.value.length > 1) { const key = args.search.value; let count = 0; const keyFilterMember = (m) => m.nickname && (m.nickname.toLowerCase().includes(key) || similarity(m.nickname.toLowerCase(), key) > 0.75 && Math.abs(m.nickname.length - key.length) < 3) || m.user.username.toLowerCase().includes(key) || similarity(m.user.username.toLowerCase(), key) > 0.75 && Math.abs(m.user.username.length - key.length) < 3; const discrimFilterMember = (m) => m.user.discriminator === key; const keyFilterUser = (u) => u.username.toLowerCase().includes(key) || similarity(u.username, key) > 0.75 && Math.abs(u.username.length - key.length) < 3; const discrimFilterUser = (u) => u.discriminator === key; const members = message.guild.members.cache.filter(args.discriminator ? discrimFilterMember : keyFilterMember); const users = this.client.users.cache.filter(args.discriminator ? discrimFilterUser : keyFilterUser).filter((u) => !members.has(u.id)); if (!users.size && !members.size) return message.formattedRespond('C_USER_SEARCH_404'); for(const [ id, member ] of members) { response += `${Util.escapeMarkdown(member.user.tag)} ${member.nickname ? `- ${Util.escapeMarkdown(member.nickname)}` : ''} [${id}] <:members:741721081261588589>\n`; count++; if(response.length > 1900) break; } if (response.length < 1900) for (const [id, user] of users) { response += `${Util.escapeMarkdown(user.tag)} [${id}]\n`; count++; if (response.length > 1900) break; } response = { description: response, title: message.format('C_USER_SEARCH_TITLE', { key }), color: 0x0088cc, footer: { text: message.format('C_USER_SEARCH_FOOTER', { matches: members.size + users.size, count }) } }; } else { let user = null; if (params.length > 0) { user = await this.client.resolver.resolveUser(params.join(' ')); if (!user) return message.formattedRespond('C_USER_404'); } else user = message.author; const member = await message.guild.members.fetch(user.id).catch(); const { activities } = user.presence; response = { title: `**${user.tag}**`, description: response, thumbnail: { url: user.avatarURL() || user.defaultAvatarURL }, fields: [ { name: message.format('C_USER_DATA_NAME'), value: message.format('C_USER_DATA', { id: user.id, created: user.createdAt.toDateString(), status: user.presence.status, // eslint-disable-next-line no-nested-ternary activity: activities.length > 0 ? activities[0].type === 'CUSTOM_STATUS' ? `${activities[0].name}: ${activities[0].state || 'emoji'}` : activities[0].name : 'Nothing', globalActivity: user.lastMessage ? user.lastMessage.createdAt.toDateString() : 'N/A' }), inline: true } ], footer: { text: `ID: ${user.id}` } }; if (member) { response.fields.push({ name: message.format('C_USER_MEMBER_NAME'), value: message.format('C_USER_MEMBER', { nickname: member.nickname ? member.nickname : 'N/A', joined: member.joinedAt ? member.joinedAt.toDateString() : 'N/A', serverActivity: member.lastMessage ? member.lastMessage.createdAt.toDateString() : 'N/A' }), inline: true }); const roles = member.roles.cache.filter((r) => r.name !== '@everyone').sort((a, b) => b.rawPosition - a.rawPosition); let counter = 0; if (roles.size) response.fields.push({ name: message.format('C_USER_ROLES_TITLE'), value: roles.map((r) => { const str = `<@&${r.id}>`; counter += str.length; return counter <= 950 ? str : ''; }).join(' ') }); const highestColouredRole = member.roles.cache.filter((role) => role.color !== 0).sort((a, b) => b.rawPosition - a.rawPosition).first(); if (highestColouredRole) response.color = highestColouredRole.color; } const flags = user.flags || await user.fetchFlags(); if (flags.bitfield) response.fields.push({ name: message.format('C_USER_FLAGS'), value: flags.toArray().join(', ') }); } return message.embed(response); } } module.exports = UserCommand;