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,24 +1,27 @@
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,
@ -28,7 +31,7 @@ export async function actualInfoCmd(msg: Message, args: any, pluginData: PluginD
embed.title = `${user.username}#${user.discriminator}`; embed.title = `${user.username}#${user.discriminator}`;
embed.thumbnail = { url: user.avatarURL }; embed.thumbnail = { url: user.avatarURL };
if (args.compact) { if (compact) {
embed.fields.push({ embed.fields.push({
name: "User information", name: "User information",
value: trimLines(` value: trimLines(`
@ -49,9 +52,10 @@ export async function actualInfoCmd(msg: Message, args: any, pluginData: PluginD
value: embedPadding, value: embedPadding,
}); });
} }
msg.channel.createMessage({ embed });
return; return embed;
} else { }
embed.fields.push({ embed.fields.push({
name: "User information", name: "User information",
value: value:
@ -61,10 +65,6 @@ export async function actualInfoCmd(msg: Message, args: any, pluginData: PluginD
Created: **${accountAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})** Created: **${accountAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})**
`) + embedPadding, `) + embedPadding,
}); });
}
} else {
embed.title = `Unknown user`;
}
if (member) { if (member) {
const joinedAt = moment(member.joinedAt); const joinedAt = moment(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,10 +113,13 @@ 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) {
const embed = await getUserInfoEmbed(pluginData, searchResult.results[0].id, false);
if (embed) {
searchMsg.edit("Only one result:"); searchMsg.edit("Only one result:");
actualInfoCmd(msg, { user: searchResult.results[0], compact: false }, pluginData); msg.channel.createMessage({ embed });
return; return;
} }
}
searchMsg.edit(result); searchMsg.edit(result);