Reorganize !info abstraction

Consistent with the abstraction for !server/!serverinfo now
This commit is contained in:
Dragory 2020-08-05 17:38:47 +03:00
parent a234e18ec3
commit 1df00d8548
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
3 changed files with 66 additions and 56 deletions

View file

@ -1,6 +1,7 @@
import { utilityCmd } from "../types"; import { utilityCmd } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes"; import { commandTypeHelpers as ct } from "../../../commandTypes";
import { actualInfoCmd } from "../actualInfoCmd"; import { getUserInfoEmbed } from "../functions/getUserInfoEmbed";
import { sendErrorMessage } from "../../../pluginUtils";
export const InfoCmd = utilityCmd({ export const InfoCmd = utilityCmd({
trigger: "info", trigger: "info",
@ -14,7 +15,13 @@ export const InfoCmd = utilityCmd({
compact: ct.switchOption({ shortcut: "c" }), compact: ct.switchOption({ shortcut: "c" }),
}, },
async run({ message: msg, args, pluginData }) { async run({ message, args, pluginData }) {
actualInfoCmd(msg, args, pluginData); const embed = await getUserInfoEmbed(pluginData, args.user.id, args.compact);
if (!embed) {
sendErrorMessage(pluginData, message.channel, "User not found");
return;
}
message.channel.createMessage({ embed });
}, },
}); });

View file

@ -1,71 +1,71 @@
import { Message, GuildTextableChannel, EmbedOptions } from "eris"; import { Message, GuildTextableChannel, EmbedOptions } from "eris";
import { PluginData } from "knub"; import { PluginData } from "knub";
import { UtilityPluginType } from "./types"; import { UtilityPluginType } from "../types";
import { UnknownUser, trimLines, embedPadding, resolveMember } from "src/utils"; import { UnknownUser, trimLines, embedPadding, resolveMember, resolveUser } from "src/utils";
import moment from "moment-timezone"; import moment from "moment-timezone";
import { CaseTypes } from "src/data/CaseTypes"; import { CaseTypes } from "src/data/CaseTypes";
import humanizeDuration from "humanize-duration"; import humanizeDuration from "humanize-duration";
export async function actualInfoCmd(msg: Message, args: any, pluginData: PluginData<UtilityPluginType>) { export async function getUserInfoEmbed(
const user = args.user || msg.author; pluginData: PluginData<UtilityPluginType>,
userId: string,
let member; compact = false,
if (!(user instanceof UnknownUser)) { ): Promise<EmbedOptions | null> {
member = await resolveMember(pluginData.client, (msg.channel as GuildTextableChannel).guild, user.id); const user = await resolveUser(pluginData.client, userId);
if (!user || user instanceof UnknownUser) {
return null;
} }
const member = await resolveMember(pluginData.client, pluginData.guild, user.id);
const embed: EmbedOptions = { const embed: EmbedOptions = {
fields: [], fields: [],
}; };
if (user && !(user instanceof UnknownUser)) { const createdAt = moment(user.createdAt);
const createdAt = moment(user.createdAt); const accountAge = humanizeDuration(moment().valueOf() - user.createdAt, {
const accountAge = humanizeDuration(moment().valueOf() - user.createdAt, { largest: 2,
largest: 2, round: true,
round: true, });
embed.title = `${user.username}#${user.discriminator}`;
embed.thumbnail = { url: user.avatarURL };
if (compact) {
embed.fields.push({
name: "User information",
value: trimLines(`
Profile: <@!${user.id}>
Created: **${accountAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})**
`),
}); });
if (member) {
embed.title = `${user.username}#${user.discriminator}`; const joinedAt = moment(member.joinedAt);
embed.thumbnail = { url: user.avatarURL }; const joinAge = humanizeDuration(moment().valueOf() - member.joinedAt, {
largest: 2,
if (args.compact) { round: true,
embed.fields.push({
name: "User information",
value: trimLines(`
Profile: <@!${user.id}>
Created: **${accountAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})**
`),
}); });
if (member) { embed.fields[0].value += `\nJoined: **${joinAge} ago (${joinedAt.format("YYYY-MM-DD[T]HH:mm:ss")})**`;
const joinedAt = moment(member.joinedAt);
const joinAge = humanizeDuration(moment().valueOf() - member.joinedAt, {
largest: 2,
round: true,
});
embed.fields[0].value += `\nJoined: **${joinAge} ago (${joinedAt.format("YYYY-MM-DD[T]HH:mm:ss")})**`;
} else {
embed.fields.push({
name: "!! USER IS NOT ON THE SERVER !!",
value: embedPadding,
});
}
msg.channel.createMessage({ embed });
return;
} else { } else {
embed.fields.push({ embed.fields.push({
name: "User information", name: "!! USER IS NOT ON THE SERVER !!",
value: value: embedPadding,
trimLines(`
ID: **${user.id}**
Profile: <@!${user.id}>
Created: **${accountAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})**
`) + embedPadding,
}); });
} }
} else {
embed.title = `Unknown user`; return embed;
} }
embed.fields.push({
name: "User information",
value:
trimLines(`
ID: **${user.id}**
Profile: <@!${user.id}>
Created: **${accountAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})**
`) + embedPadding,
});
if (member) { if (member) {
const joinedAt = moment(member.joinedAt); const joinedAt = moment(member.joinedAt);
const joinAge = humanizeDuration(moment().valueOf() - member.joinedAt, { const joinAge = humanizeDuration(moment().valueOf() - member.joinedAt, {
@ -125,5 +125,5 @@ export async function actualInfoCmd(msg: Message, args: any, pluginData: PluginD
}); });
} }
msg.channel.createMessage({ embed }); return embed;
} }

View file

@ -10,7 +10,7 @@ import { searchCmdSignature } from "./commands/SearchCmd";
import { banSearchSignature } from "./commands/BanSearchCmd"; import { banSearchSignature } from "./commands/BanSearchCmd";
import { UtilityPluginType } from "./types"; import { UtilityPluginType } from "./types";
import { refreshMembersIfNeeded } from "./refreshMembers"; import { refreshMembersIfNeeded } from "./refreshMembers";
import { actualInfoCmd } from "./actualInfoCmd"; import { getUserInfoEmbed } from "./functions/getUserInfoEmbed";
const SEARCH_RESULTS_PER_PAGE = 15; const SEARCH_RESULTS_PER_PAGE = 15;
const SEARCH_ID_RESULTS_PER_PAGE = 50; const SEARCH_ID_RESULTS_PER_PAGE = 50;
@ -113,9 +113,12 @@ export async function displaySearch(
const cfg = pluginData.config.getForUser(msg.author); const cfg = pluginData.config.getForUser(msg.author);
if (cfg.info_on_single_result && searchResult.totalResults === 1) { if (cfg.info_on_single_result && searchResult.totalResults === 1) {
searchMsg.edit("Only one result:"); const embed = await getUserInfoEmbed(pluginData, searchResult.results[0].id, false);
actualInfoCmd(msg, { user: searchResult.results[0], compact: false }, pluginData); if (embed) {
return; searchMsg.edit("Only one result:");
msg.channel.createMessage({ embed });
return;
}
} }
searchMsg.edit(result); searchMsg.edit(result);