2020-07-29 02:21:22 +02:00
|
|
|
import { Message, GuildTextableChannel, EmbedOptions } from "eris";
|
|
|
|
import { PluginData } from "knub";
|
2020-08-05 17:38:47 +03:00
|
|
|
import { UtilityPluginType } from "../types";
|
2020-08-10 02:25:19 +03:00
|
|
|
import { UnknownUser, trimLines, embedPadding, resolveMember, resolveUser, preEmbedPadding, sorter } from "src/utils";
|
2020-07-29 02:21:22 +02:00
|
|
|
import moment from "moment-timezone";
|
|
|
|
import { CaseTypes } from "src/data/CaseTypes";
|
|
|
|
import humanizeDuration from "humanize-duration";
|
2020-08-10 00:24:06 +03:00
|
|
|
import { inGuildTz } from "../../../utils/timezones";
|
|
|
|
import { getDateFormat } from "../../../utils/dateFormats";
|
2020-07-29 02:21:22 +02:00
|
|
|
|
2020-08-05 17:38:47 +03:00
|
|
|
export async function getUserInfoEmbed(
|
|
|
|
pluginData: PluginData<UtilityPluginType>,
|
|
|
|
userId: string,
|
|
|
|
compact = false,
|
|
|
|
): Promise<EmbedOptions | null> {
|
|
|
|
const user = await resolveUser(pluginData.client, userId);
|
|
|
|
if (!user || user instanceof UnknownUser) {
|
|
|
|
return null;
|
2020-07-29 02:21:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-05 17:38:47 +03:00
|
|
|
const member = await resolveMember(pluginData.client, pluginData.guild, user.id);
|
|
|
|
|
2020-07-29 02:21:22 +02:00
|
|
|
const embed: EmbedOptions = {
|
|
|
|
fields: [],
|
|
|
|
};
|
|
|
|
|
2020-08-05 22:16:53 +03:00
|
|
|
embed.author = {
|
|
|
|
name: `User: ${user.username}#${user.discriminator}`,
|
|
|
|
};
|
|
|
|
|
|
|
|
const avatarURL = user.avatarURL || user.defaultAvatarURL;
|
|
|
|
embed.author.icon_url = avatarURL;
|
|
|
|
|
2020-08-10 00:24:06 +03:00
|
|
|
const createdAt = moment.utc(user.createdAt, "x");
|
|
|
|
const prettyCreatedAt = inGuildTz(pluginData, createdAt).format(getDateFormat(pluginData, "pretty_datetime"));
|
|
|
|
const accountAge = humanizeDuration(moment.utc().valueOf() - user.createdAt, {
|
2020-08-05 17:38:47 +03:00
|
|
|
largest: 2,
|
|
|
|
round: true,
|
|
|
|
});
|
2020-07-29 02:21:22 +02:00
|
|
|
|
2020-08-05 17:38:47 +03:00
|
|
|
if (compact) {
|
|
|
|
embed.fields.push({
|
2020-08-05 22:16:53 +03:00
|
|
|
name: preEmbedPadding + "User information",
|
2020-08-05 17:38:47 +03:00
|
|
|
value: trimLines(`
|
|
|
|
Profile: <@!${user.id}>
|
2020-08-10 00:24:06 +03:00
|
|
|
Created: **${accountAge} ago** (\`${prettyCreatedAt}\`)
|
2020-08-05 17:38:47 +03:00
|
|
|
`),
|
|
|
|
});
|
|
|
|
if (member) {
|
2020-08-10 00:24:06 +03:00
|
|
|
const joinedAt = moment.utc(member.joinedAt, "x");
|
|
|
|
const prettyJoinedAt = inGuildTz(pluginData, joinedAt).format(getDateFormat(pluginData, "pretty_datetime"));
|
|
|
|
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedAt, {
|
2020-08-05 17:38:47 +03:00
|
|
|
largest: 2,
|
|
|
|
round: true,
|
2020-07-29 02:21:22 +02:00
|
|
|
});
|
2020-08-10 00:24:06 +03:00
|
|
|
embed.fields[0].value += `\nJoined: **${joinAge} ago** (\`${prettyJoinedAt}\`)`;
|
2020-07-29 02:21:22 +02:00
|
|
|
} else {
|
|
|
|
embed.fields.push({
|
2020-08-05 22:16:53 +03:00
|
|
|
name: preEmbedPadding + "!! NOTE !!",
|
|
|
|
value: "User is not on the server",
|
2020-07-29 02:21:22 +02:00
|
|
|
});
|
|
|
|
}
|
2020-08-05 17:38:47 +03:00
|
|
|
|
|
|
|
return embed;
|
2020-07-29 02:21:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-05 17:38:47 +03:00
|
|
|
embed.fields.push({
|
2020-08-05 22:16:53 +03:00
|
|
|
name: preEmbedPadding + "User information",
|
|
|
|
value: trimLines(`
|
|
|
|
Name: **${user.username}#${user.discriminator}**
|
|
|
|
ID: \`${user.id}\`
|
2020-08-10 00:24:06 +03:00
|
|
|
Created: **${accountAge} ago** (\`${prettyCreatedAt}\`)
|
2020-08-05 22:16:53 +03:00
|
|
|
Mention: <@!${user.id}>
|
|
|
|
`),
|
2020-08-05 17:38:47 +03:00
|
|
|
});
|
|
|
|
|
2020-07-29 02:21:22 +02:00
|
|
|
if (member) {
|
2020-08-10 00:24:06 +03:00
|
|
|
const joinedAt = moment.utc(member.joinedAt, "x");
|
|
|
|
const prettyJoinedAt = inGuildTz(pluginData, joinedAt).format(getDateFormat(pluginData, "pretty_datetime"));
|
|
|
|
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedAt, {
|
2020-07-29 02:21:22 +02:00
|
|
|
largest: 2,
|
|
|
|
round: true,
|
|
|
|
});
|
|
|
|
const roles = member.roles.map(id => pluginData.guild.roles.get(id)).filter(r => !!r);
|
2020-08-10 02:25:19 +03:00
|
|
|
roles.sort(sorter("position", "DESC"));
|
2020-07-29 02:21:22 +02:00
|
|
|
|
|
|
|
embed.fields.push({
|
2020-08-05 22:16:53 +03:00
|
|
|
name: preEmbedPadding + "Member information",
|
|
|
|
value: trimLines(`
|
2020-08-10 00:24:06 +03:00
|
|
|
Joined: **${joinAge} ago** (\`${prettyJoinedAt}\`)
|
2020-08-10 02:25:19 +03:00
|
|
|
${roles.length > 0 ? "Roles: " + roles.map(r => `<@&${r.id}>`).join(", ") : ""}
|
2020-08-05 22:16:53 +03:00
|
|
|
`),
|
2020-07-29 02:21:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const voiceChannel = member.voiceState.channelID
|
|
|
|
? pluginData.guild.channels.get(member.voiceState.channelID)
|
|
|
|
: null;
|
|
|
|
if (voiceChannel || member.voiceState.mute || member.voiceState.deaf) {
|
|
|
|
embed.fields.push({
|
2020-08-05 22:16:53 +03:00
|
|
|
name: preEmbedPadding + "Voice information",
|
|
|
|
value: trimLines(`
|
2020-07-29 02:21:22 +02:00
|
|
|
${voiceChannel ? `Current voice channel: **${voiceChannel ? voiceChannel.name : "None"}**` : ""}
|
|
|
|
${member.voiceState.mute ? "Server voice muted: **Yes**" : ""}
|
|
|
|
${member.voiceState.deaf ? "Server voice deafened: **Yes**" : ""}
|
2020-08-05 22:16:53 +03:00
|
|
|
`),
|
2020-07-29 02:21:22 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
embed.fields.push({
|
2020-08-05 22:18:38 +03:00
|
|
|
name: preEmbedPadding + "Member information",
|
|
|
|
value: "⚠ User is not on the server",
|
2020-07-29 02:21:22 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
const cases = (await pluginData.state.cases.getByUserId(user.id)).filter(c => !c.is_hidden);
|
|
|
|
|
|
|
|
if (cases.length > 0) {
|
|
|
|
cases.sort((a, b) => {
|
|
|
|
return a.created_at < b.created_at ? 1 : -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
const caseSummary = cases.slice(0, 3).map(c => {
|
2020-08-06 01:56:51 +03:00
|
|
|
return `${CaseTypes[c.type]} (#${c.case_number})`;
|
2020-07-29 02:21:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const summaryText = cases.length > 3 ? "Last 3 cases" : "Summary";
|
|
|
|
|
|
|
|
embed.fields.push({
|
2020-08-05 22:16:53 +03:00
|
|
|
name: preEmbedPadding + "Cases",
|
2020-07-29 02:21:22 +02:00
|
|
|
value: trimLines(`
|
|
|
|
Total cases: **${cases.length}**
|
|
|
|
${summaryText}: ${caseSummary.join(", ")}
|
|
|
|
`),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-05 17:38:47 +03:00
|
|
|
return embed;
|
2020-07-29 02:21:22 +02:00
|
|
|
}
|