2021-06-06 23:51:32 +02:00
|
|
|
import { MessageEmbedOptions, Role } from "discord.js";
|
|
|
|
import humanizeDuration from "humanize-duration";
|
2020-10-01 01:43:38 +03:00
|
|
|
import { GuildPluginData } from "knub";
|
2020-07-29 02:21:22 +02:00
|
|
|
import moment from "moment-timezone";
|
2020-10-01 01:43:38 +03:00
|
|
|
import { CaseTypes } from "../../../data/CaseTypes";
|
2021-06-06 23:51:32 +02:00
|
|
|
import {
|
2021-06-08 02:23:30 +02:00
|
|
|
EmbedWith,
|
|
|
|
messageLink,
|
|
|
|
preEmbedPadding,
|
|
|
|
resolveMember,
|
|
|
|
resolveUser,
|
|
|
|
sorter,
|
|
|
|
trimLines,
|
|
|
|
UnknownUser,
|
2021-06-06 23:51:32 +02:00
|
|
|
} from "../../../utils";
|
2020-08-19 00:19:12 +03:00
|
|
|
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { UtilityPluginType } from "../types";
|
2020-07-29 02:21:22 +02:00
|
|
|
|
2020-08-05 17:38:47 +03:00
|
|
|
export async function getUserInfoEmbed(
|
2020-10-01 01:43:38 +03:00
|
|
|
pluginData: GuildPluginData<UtilityPluginType>,
|
2020-08-05 17:38:47 +03:00
|
|
|
userId: string,
|
|
|
|
compact = false,
|
2020-08-19 00:19:12 +03:00
|
|
|
requestMemberId?: string,
|
2021-06-02 04:07:50 +02:00
|
|
|
): Promise<MessageEmbedOptions | null> {
|
2020-08-05 17:38:47 +03:00
|
|
|
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-11-09 20:03:57 +02:00
|
|
|
const embed: EmbedWith<"fields"> = {
|
2020-07-29 02:21:22 +02:00
|
|
|
fields: [],
|
|
|
|
};
|
|
|
|
|
2020-08-19 00:19:12 +03:00
|
|
|
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
|
|
|
|
|
2020-08-05 22:16:53 +03:00
|
|
|
embed.author = {
|
|
|
|
name: `User: ${user.username}#${user.discriminator}`,
|
|
|
|
};
|
|
|
|
|
2021-06-02 04:07:50 +02:00
|
|
|
const avatarURL = user.avatarURL() || user.defaultAvatarURL;
|
2020-08-05 22:16:53 +03:00
|
|
|
embed.author.icon_url = avatarURL;
|
|
|
|
|
2020-08-10 00:24:06 +03:00
|
|
|
const createdAt = moment.utc(user.createdAt, "x");
|
2020-08-19 00:19:12 +03:00
|
|
|
const tzCreatedAt = requestMemberId
|
|
|
|
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
|
|
|
|
: timeAndDate.inGuildTz(createdAt);
|
|
|
|
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
|
2021-06-02 04:07:50 +02:00
|
|
|
const accountAge = humanizeDuration(moment.utc().valueOf() - user.createdTimestamp, {
|
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) {
|
2021-06-02 04:07:50 +02:00
|
|
|
const joinedAt = moment.utc(member.joinedTimestamp!, "x");
|
2020-08-19 00:19:12 +03:00
|
|
|
const tzJoinedAt = requestMemberId
|
|
|
|
? await timeAndDate.inMemberTz(requestMemberId, joinedAt)
|
|
|
|
: timeAndDate.inGuildTz(joinedAt);
|
|
|
|
const prettyJoinedAt = tzJoinedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
|
2021-06-02 04:07:50 +02:00
|
|
|
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedTimestamp!, {
|
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) {
|
2021-06-02 04:07:50 +02:00
|
|
|
const joinedAt = moment.utc(member.joinedTimestamp!, "x");
|
2020-08-19 00:19:12 +03:00
|
|
|
const tzJoinedAt = requestMemberId
|
|
|
|
? await timeAndDate.inMemberTz(requestMemberId, joinedAt)
|
|
|
|
: timeAndDate.inGuildTz(joinedAt);
|
|
|
|
const prettyJoinedAt = tzJoinedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
|
2021-06-02 04:07:50 +02:00
|
|
|
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedTimestamp!, {
|
2020-07-29 02:21:22 +02:00
|
|
|
largest: 2,
|
|
|
|
round: true,
|
|
|
|
});
|
2021-06-02 04:07:50 +02:00
|
|
|
const roles = member.roles.cache
|
|
|
|
.map(role => pluginData.guild.roles.cache.get(role.id))
|
|
|
|
.filter(r => r != null) as Role[];
|
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
|
|
|
});
|
|
|
|
|
2021-07-04 23:14:12 +02:00
|
|
|
const voiceChannel = member.voice.channelId ? pluginData.guild.channels.cache.get(member.voice.channelId) : null;
|
2021-06-02 04:07:50 +02:00
|
|
|
if (voiceChannel || member.voice.mute || member.voice.deaf) {
|
2020-07-29 02:21:22 +02:00
|
|
|
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"}**` : ""}
|
2021-06-02 04:07:50 +02:00
|
|
|
${member.voice.mute ? "Server voice muted: **Yes**" : ""}
|
|
|
|
${member.voice.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-10 02:38:49 +03:00
|
|
|
const summaryText = `${CaseTypes[c.type]} (#${c.case_number})`;
|
|
|
|
|
|
|
|
if (c.log_message_id) {
|
|
|
|
const [channelId, messageId] = c.log_message_id.split("-");
|
2020-08-10 03:26:39 +03:00
|
|
|
return `[${summaryText}](${messageLink(pluginData.guild.id, channelId, messageId)})`;
|
2020-08-10 02:38:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return summaryText;
|
2020-07-29 02:21:22 +02:00
|
|
|
});
|
|
|
|
|
2020-08-10 02:38:49 +03:00
|
|
|
const summaryLabel = cases.length > 3 ? "Last 3 cases" : "Summary";
|
2020-07-29 02:21:22 +02:00
|
|
|
|
|
|
|
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}**
|
2020-08-10 02:38:49 +03:00
|
|
|
${summaryLabel}: ${caseSummary.join(", ")}
|
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
|
|
|
}
|