galactic-bot/structure/client/components/commands/information/User.js

214 lines
8.5 KiB
JavaScript
Raw Normal View History

2020-06-19 23:02:16 +02:00
const { Command } = require('../../../../interfaces');
const similarity = require('similarity');
2020-08-14 10:35:41 +02:00
const { Util, Emojis } = require('../../../../../util');
const Constants = {
Badges: {
DISCORD_EMPLOYEE: Emojis['discord-staff'],
DISCORD_PARTNER: Emojis['discord-partner'],
HYPESQUAD_EVENTS: Emojis['hypesquad-events'],
BUGHUNTER_LEVEL_1: Emojis['bughunter'], //eslint-disable-line dot-notation
BUGHUNTER_LEVEL_2: Emojis['bughunter-gold'],
HOUSE_BRAVERY: Emojis['hypesquad-bravery'],
HOUSE_BRILLIANCE: Emojis['hypesquad-brilliance'],
HOUSE_BALANCE: Emojis['hypesquad-balance'],
EARLY_SUPPORTER: Emojis['early-supporter'],
VERIFIED_DEVELOPER: Emojis['bot-developer']
}
};
class UserCommand extends Command {
constructor(client) {
super(client, {
name: 'user',
2020-06-19 23:02:16 +02:00
module: 'information',
2020-05-21 10:51:44 +02:00
description: 'Display information about user.',
2020-04-21 19:56:31 +02:00
guildOnly: true,
2020-09-05 22:57:10 +02:00
aliases: [
'userinfo',
'whois'
],
2020-04-21 19:56:31 +02:00
arguments: [
{
2020-04-21 19:56:31 +02:00
name: 'search',
2020-05-01 16:13:40 +02:00
type: 'STRING',
2020-04-21 19:56:31 +02:00
types: ['FLAG', 'VERBAL'],
2020-08-13 22:46:01 +02:00
required: true
},
{
name: 'discriminator',
aliases: [ 'discrim', 'disc' ],
type: 'BOOLEAN',
types: ['FLAG']
}
],
clientPermissions: ['SEND_MESSAGES', 'EMBED_LINKS']
});
this.client = client;
}
2020-04-21 19:56:31 +02:00
async execute(message, { params, args }) {
2020-08-10 02:00:39 +02:00
let response = '';
2020-04-21 19:56:31 +02:00
if(args.search && args.search.value.length > 1) {
const key = args.search.value;
let count = 0;
2020-08-13 22:46:01 +02:00
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) ||
2020-08-14 00:12:48 +02:00
m.user.username.toLowerCase().includes(key) || similarity(m.user.username.toLowerCase(), key) > 0.75 && Math.abs(m.user.username.length - key.length) < 3;
2020-08-13 22:46:01 +02:00
const discrimFilterMember = (m) => m.user.discriminator === key;
2020-08-14 00:12:48 +02:00
const keyFilterUser = (u) => u.username.toLowerCase().includes(key) || similarity(u.username, key) > 0.75 && Math.abs(u.username.length - key.length) < 3;
2020-08-13 22:46:01 +02:00
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) {
2020-08-14 00:12:48 +02:00
response += `${Util.escapeMarkdown(member.user.tag)} ${member.nickname ? `- ${Util.escapeMarkdown(member.nickname)}` : ''} [${id}] <:members:741721081261588589>\n`;
count++;
if(response.length > 1900) break;
}
2020-08-13 22:46:01 +02:00
if (response.length < 1900) for (const [id, user] of users) {
2020-08-14 00:12:48 +02:00
response += `${Util.escapeMarkdown(user.tag)} [${id}]\n`;
2020-08-13 22:46:01 +02:00
count++;
if (response.length > 1900) break;
}
response = {
description: response,
2020-05-01 16:13:40 +02:00
title: message.format('C_USER_SEARCH_TITLE', {
key
2020-04-21 19:56:31 +02:00
}),
color: 0x0088cc,
footer: {
2020-05-01 16:13:40 +02:00
text: message.format('C_USER_SEARCH_FOOTER', {
2020-08-14 00:12:48 +02:00
matches: members.size + users.size,
2020-04-21 19:56:31 +02:00
count
})
}
};
} else {
let user = null;
2020-04-21 19:56:31 +02:00
if (params.length > 0) {
user = await this.client.resolver.resolveUser(params.join(' '));
2020-08-10 02:00:39 +02:00
if (!user) return message.formattedRespond('C_USER_404');
2020-04-21 19:56:31 +02:00
} else user = message.author;
2020-08-14 10:35:41 +02:00
const member = await message.guild.members.fetch(user.id).catch((error) => {}); //eslint-disable-line
2020-08-06 00:13:09 +02:00
const { activities } = user.presence;
2020-08-14 10:35:41 +02:00
const flags = user.flags || await user.fetchFlags();
const badges = flags.toArray().filter((f) => Constants.Badges[f])
.map((f) => Constants.Badges[f]);
response = {
2020-08-14 10:35:41 +02:00
author: {
2020-08-15 23:23:37 +02:00
name: Util.escapeMarkdown(user.tag)
// icon_url: user.displayAvatarURL() //eslint-disable-line camelcase
2020-08-14 10:35:41 +02:00
},
description: response,
thumbnail: {
2020-08-17 22:49:25 +02:00
url: user.displayAvatarURL({ dynamic: true }) || user.defaultAvatarURL
2020-08-10 02:00:39 +02:00
},
2020-08-14 10:35:41 +02:00
fields: [],
2020-08-10 02:00:39 +02:00
footer: {
2020-08-14 10:35:41 +02:00
text: `• User ID: ${user.id}`
}
};
2020-08-10 02:00:39 +02:00
const activity = activities.reduce((acc, curr) => {
if (acc.length) acc += `\n`;
if (curr.emoji) acc += `<emoji:${curr.emoji.name}> `;
2021-05-06 19:21:39 +02:00
if (curr.type !== 'CUSTOM_STATUS') acc += `__${curr.name}__\n`;
if (curr.state) acc += curr.state;
2021-05-06 19:21:39 +02:00
if (curr.type === 'LISTENING') acc += ': ' + curr.details;
return acc;
}, '');
2020-08-14 10:35:41 +02:00
const userField = {
name: message.format('C_USER_DATA_NAME'),
value: message.format('C_USER_DATA', {
id: user.id,
bot: user.bot ? ` ${Emojis.bot}` : '',
created: user.createdAt.toDateString(),
status: user.presence.status,
// eslint-disable-next-line no-nested-ternary
2021-05-06 19:21:39 +02:00
//activity, //activity: activities.length > 0 ? activities[0].type === 'CUSTOM_STATUS' ? `${activities[0].name}: ${activities[0].state || 'emoji'}` : activities[0].name : 'Nothing',
2020-08-14 10:35:41 +02:00
globalActivity: user.lastMessage ? user.lastMessage.createdAt.toDateString() : 'N/A'
2021-05-06 19:21:39 +02:00
}),
inline: true
2020-08-14 10:35:41 +02:00
};
if(badges.length > 0) {
userField.value += `\n${message.format('C_USER_BADGES', {
badges: badges.join(' ')
})}`;
}
response.fields.push(userField);
2021-05-06 19:21:39 +02:00
if (activities.length) response.fields.push({
name: message.format('C_USER_ACTIVITIES'),
value: activity,
inline: true
});
2020-08-10 02:00:39 +02:00
if (member) {
2020-08-14 10:35:41 +02:00
const memberField = {
2020-08-10 02:00:39 +02:00
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'
2020-08-14 10:35:41 +02:00
})
};
2020-08-10 02:00:39 +02:00
const roles = member.roles.cache.filter((r) => r.name !== '@everyone').sort((a, b) => b.rawPosition - a.rawPosition);
2020-08-14 10:35:41 +02:00
const maxRoles = 30;
if(roles.size > 0) {
memberField.value += `\n${message.format('C_USER_MEMBER_ROLES', {
roles: roles.size > maxRoles ? `${roles.slice(0, maxRoles).map((r) => `<@&${r.id}>`).join(' ')} \`...${maxRoles-roles.size} more roles\`` : roles.map((r) => `<@&${r.id}>`).join(' ')
})}`;
}
response.fields.push(memberField);
// 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(' ')
// });
2020-08-10 02:00:39 +02:00
const highestColouredRole = member.roles.cache.filter((role) => role.color !== 0).sort((a, b) => b.rawPosition - a.rawPosition).first();
if (highestColouredRole) response.color = highestColouredRole.color;
}
}
return message.embed(response);
}
}
module.exports = UserCommand;