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

157 lines
6.1 KiB
JavaScript
Raw Normal View History

2020-06-19 23:02:16 +02:00
const { Command } = require('../../../../interfaces');
const similarity = require('similarity');
2020-08-10 02:00:39 +02:00
const { Util } = require('../../../../../util');
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,
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']
}
2020-04-21 19:56:31 +02:00
]
});
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) ||
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) {
2020-08-13 22:46:01 +02:00
response += `${member.user.tag} ${member.nickname ? `- ${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) {
response += `${user.tag} [${id}]\n`;
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-04-21 19:56:31 +02:00
matches: members.size,
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-10 02:00:39 +02:00
const member = await message.guild.members.fetch(user.id).catch(() => { });
2020-08-06 00:13:09 +02:00
const { activities } = user.presence;
response = {
2020-08-10 02:00:39 +02:00
title: `**${user.tag}**`,
description: response,
thumbnail: {
url: user.avatarURL() || user.defaultAvatarURL
2020-08-10 02:00:39 +02:00
},
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,
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}`
}
};
2020-08-10 02:00:39 +02:00
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;