More work on porting Utility commands
This commit is contained in:
parent
9bb72e0ffd
commit
017fcd93cc
6 changed files with 187 additions and 45 deletions
|
@ -1,6 +1,6 @@
|
|||
import { utilityCmd } from "../types";
|
||||
import { baseTypeHelpers as t } from "knub";
|
||||
import { archiveSearch, displaySearch, SearchType } from "./search";
|
||||
import { archiveSearch, displaySearch, SearchType } from "../search";
|
||||
|
||||
// Separate from BanSearchCmd to avoid a circular reference from ./search.ts
|
||||
export const banSearchSignature = {
|
||||
|
|
143
backend/src/plugins/Utility/commands/InfoCmd.ts
Normal file
143
backend/src/plugins/Utility/commands/InfoCmd.ts
Normal file
|
@ -0,0 +1,143 @@
|
|||
import { utilityCmd } from "../types";
|
||||
import { baseTypeHelpers as t } from "knub";
|
||||
import { customArgumentHelpers as ct } from "../../../customArgumentTypes";
|
||||
import { embedPadding, resolveMember, trimLines, UnknownUser } from "../../../utils";
|
||||
import { EmbedOptions, GuildTextableChannel } from "eris";
|
||||
import moment from "moment-timezone";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
import { CaseTypes } from "../../../data/CaseTypes";
|
||||
|
||||
export const InfoCmd = utilityCmd({
|
||||
trigger: "info",
|
||||
description: "Show basic information about a user",
|
||||
usage: "!info 106391128718245888",
|
||||
permission: "can_info",
|
||||
|
||||
signature: {
|
||||
user: ct.resolvedUserLoose({ required: false }),
|
||||
|
||||
compact: t.switchOption({ shortcut: "c" }),
|
||||
},
|
||||
|
||||
async run({ message: msg, args, pluginData }) {
|
||||
const user = args.user || msg.author;
|
||||
|
||||
let member;
|
||||
if (!(user instanceof UnknownUser)) {
|
||||
member = await resolveMember(pluginData.client, (msg.channel as GuildTextableChannel).guild, user.id);
|
||||
}
|
||||
|
||||
const embed: EmbedOptions = {
|
||||
fields: [],
|
||||
};
|
||||
|
||||
if (user && !(user instanceof UnknownUser)) {
|
||||
const createdAt = moment(user.createdAt);
|
||||
const accountAge = humanizeDuration(moment().valueOf() - user.createdAt, {
|
||||
largest: 2,
|
||||
round: true,
|
||||
});
|
||||
|
||||
embed.title = `${user.username}#${user.discriminator}`;
|
||||
embed.thumbnail = { url: user.avatarURL };
|
||||
|
||||
if (args.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) {
|
||||
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 {
|
||||
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,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
embed.title = `Unknown user`;
|
||||
}
|
||||
|
||||
if (member) {
|
||||
const joinedAt = moment(member.joinedAt);
|
||||
const joinAge = humanizeDuration(moment().valueOf() - member.joinedAt, {
|
||||
largest: 2,
|
||||
round: true,
|
||||
});
|
||||
const roles = member.roles.map(id => pluginData.guild.roles.get(id)).filter(r => !!r);
|
||||
|
||||
embed.fields.push({
|
||||
name: "Member information",
|
||||
value:
|
||||
trimLines(`
|
||||
Joined: **${joinAge} ago (${joinedAt.format("YYYY-MM-DD[T]HH:mm:ss")})**
|
||||
${roles.length > 0 ? "Roles: " + roles.map(r => r.name).join(", ") : ""}
|
||||
`) + embedPadding,
|
||||
});
|
||||
|
||||
const voiceChannel = member.voiceState.channelID
|
||||
? pluginData.guild.channels.get(member.voiceState.channelID)
|
||||
: null;
|
||||
if (voiceChannel || member.voiceState.mute || member.voiceState.deaf) {
|
||||
embed.fields.push({
|
||||
name: "Voice information",
|
||||
value:
|
||||
trimLines(`
|
||||
${voiceChannel ? `Current voice channel: **${voiceChannel ? voiceChannel.name : "None"}**` : ""}
|
||||
${member.voiceState.mute ? "Server voice muted: **Yes**" : ""}
|
||||
${member.voiceState.deaf ? "Server voice deafened: **Yes**" : ""}
|
||||
`) + embedPadding,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
embed.fields.push({
|
||||
name: "!! USER IS NOT ON THE SERVER !!",
|
||||
value: embedPadding,
|
||||
});
|
||||
}
|
||||
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 => {
|
||||
return `${CaseTypes[c.type]} (#${c.case_number})`;
|
||||
});
|
||||
|
||||
const summaryText = cases.length > 3 ? "Last 3 cases" : "Summary";
|
||||
|
||||
embed.fields.push({
|
||||
name: "Cases",
|
||||
value: trimLines(`
|
||||
Total cases: **${cases.length}**
|
||||
${summaryText}: ${caseSummary.join(", ")}
|
||||
`),
|
||||
});
|
||||
}
|
||||
|
||||
msg.channel.createMessage({ embed });
|
||||
},
|
||||
});
|
|
@ -1,6 +1,6 @@
|
|||
import { utilityCmd } from "../types";
|
||||
import { baseTypeHelpers as t } from "knub";
|
||||
import { archiveSearch, displaySearch, SearchType } from "./search";
|
||||
import { archiveSearch, displaySearch, SearchType } from "../search";
|
||||
|
||||
// Separate from SearchCmd to avoid a circular reference from ./search.ts
|
||||
export const searchCmdSignature = {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { CategoryChannel, EmbedOptions, TextChannel, VoiceChannel } from "eris";
|
||||
import { CategoryChannel, EmbedOptions, RESTChannelInvite, TextChannel, VoiceChannel } from "eris";
|
||||
import moment from "moment-timezone";
|
||||
import { embedPadding, formatNumber, memoize, MINUTES, trimLines } from "../../../utils";
|
||||
import { utilityCmd } from "../types";
|
||||
|
@ -10,56 +10,56 @@ export const ServerCmd = utilityCmd({
|
|||
usage: "!server",
|
||||
permission: "can_server",
|
||||
|
||||
async run({ message }) {
|
||||
async run({ message, pluginData }) {
|
||||
const { guild } = pluginData;
|
||||
|
||||
const embed: EmbedOptions = {
|
||||
fields: [],
|
||||
color: parseInt("6b80cf", 16),
|
||||
};
|
||||
|
||||
embed.thumbnail = { url: this.guild.iconURL };
|
||||
embed.thumbnail = { url: guild.iconURL };
|
||||
|
||||
const createdAt = moment(this.guild.createdAt);
|
||||
const serverAge = humanizeDuration(moment().valueOf() - this.guild.createdAt, {
|
||||
const createdAt = moment(guild.createdAt);
|
||||
const serverAge = humanizeDuration(moment().valueOf() - guild.createdAt, {
|
||||
largest: 2,
|
||||
round: true,
|
||||
});
|
||||
|
||||
const owner = this.bot.users.get(this.guild.ownerID);
|
||||
const owner = pluginData.client.users.get(guild.ownerID);
|
||||
const ownerName = owner ? `${owner.username}#${owner.discriminator}` : "Unknown#0000";
|
||||
|
||||
embed.fields.push({
|
||||
name: `Server information - ${this.guild.name}`,
|
||||
name: `Server information - ${guild.name}`,
|
||||
value:
|
||||
trimLines(`
|
||||
Created: **${serverAge} ago** (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})
|
||||
Owner: **${ownerName}** (${this.guild.ownerID})
|
||||
Voice region: **${this.guild.region}**
|
||||
${this.guild.features.length > 0 ? "Features: " + this.guild.features.join(", ") : ""}
|
||||
Owner: **${ownerName}** (${guild.ownerID})
|
||||
Voice region: **${guild.region}**
|
||||
${guild.features.length > 0 ? "Features: " + guild.features.join(", ") : ""}
|
||||
`) + embedPadding,
|
||||
});
|
||||
|
||||
const restGuild = await memoize(
|
||||
() => this.bot.getRESTGuild(this.guildId),
|
||||
`getRESTGuild_${this.guildId}`,
|
||||
() => pluginData.client.getRESTGuild(guild.id),
|
||||
`getRESTGuild_${guild.id}`,
|
||||
10 * MINUTES,
|
||||
);
|
||||
|
||||
// For servers with a vanity URL, we can use the numbers from the invite for online count
|
||||
// (which is nowadays usually more accurate for large servers)
|
||||
const invite = this.guild.vanityURL
|
||||
? await memoize(
|
||||
() => this.bot.getInvite(this.guild.vanityURL, true),
|
||||
`getInvite_${this.guild.vanityURL}`,
|
||||
10 * MINUTES,
|
||||
)
|
||||
const invite = guild.vanityURL
|
||||
? ((await memoize(
|
||||
() => pluginData.client.getInvite(guild.vanityURL, true),
|
||||
`getInvite_${guild.vanityURL}`,
|
||||
10 * MINUTES,
|
||||
)) as RESTChannelInvite)
|
||||
: null;
|
||||
|
||||
const totalMembers = invite ? invite.memberCount : this.guild.memberCount;
|
||||
const totalMembers = invite?.memberCount || guild.memberCount;
|
||||
|
||||
const onlineMemberCount = invite
|
||||
? invite.presenceCount
|
||||
: this.guild.members.filter(m => m.status !== "offline").length;
|
||||
const offlineMemberCount = this.guild.memberCount - onlineMemberCount;
|
||||
const onlineMemberCount = invite ? invite.presenceCount : guild.members.filter(m => m.status !== "offline").length;
|
||||
const offlineMemberCount = guild.memberCount - onlineMemberCount;
|
||||
|
||||
let memberCountTotalLines = `Total: **${formatNumber(totalMembers)}**`;
|
||||
if (restGuild.maxMembers) {
|
||||
|
@ -81,10 +81,10 @@ export const ServerCmd = utilityCmd({
|
|||
`),
|
||||
});
|
||||
|
||||
const totalChannels = this.guild.channels.size;
|
||||
const categories = this.guild.channels.filter(channel => channel instanceof CategoryChannel);
|
||||
const textChannels = this.guild.channels.filter(channel => channel instanceof TextChannel);
|
||||
const voiceChannels = this.guild.channels.filter(channel => channel instanceof VoiceChannel);
|
||||
const totalChannels = guild.channels.size;
|
||||
const categories = guild.channels.filter(channel => channel instanceof CategoryChannel);
|
||||
const textChannels = guild.channels.filter(channel => channel instanceof TextChannel);
|
||||
const voiceChannels = guild.channels.filter(channel => channel instanceof VoiceChannel);
|
||||
|
||||
embed.fields.push({
|
||||
name: "Channels",
|
||||
|
@ -104,16 +104,16 @@ export const ServerCmd = utilityCmd({
|
|||
1: 100,
|
||||
2: 150,
|
||||
3: 250,
|
||||
}[this.guild.premiumTier] || 50;
|
||||
}[guild.premiumTier] || 50;
|
||||
|
||||
embed.fields.push({
|
||||
name: "Other stats",
|
||||
inline: true,
|
||||
value:
|
||||
trimLines(`
|
||||
Roles: **${this.guild.roles.size}** / 250
|
||||
Emojis: **${this.guild.emojis.length}** / ${maxEmojis}
|
||||
Boosts: **${this.guild.premiumSubscriptionCount ?? 0}** (level ${this.guild.premiumTier})
|
||||
Roles: **${guild.roles.size}** / 250
|
||||
Emojis: **${guild.emojis.length}** / ${maxEmojis}
|
||||
Boosts: **${guild.premiumSubscriptionCount ?? 0}** (level ${guild.premiumTier})
|
||||
`) + embedPadding,
|
||||
});
|
||||
|
||||
|
|
|
@ -1,408 +0,0 @@
|
|||
import { Member, Message, User } from "eris";
|
||||
import moment from "moment-timezone";
|
||||
import escapeStringRegexp from "escape-string-regexp";
|
||||
import safeRegex from "safe-regex";
|
||||
import { isFullMessage, MINUTES, multiSorter, noop, sorter, trimLines } from "../../../utils";
|
||||
import { sendErrorMessage } from "../../../pluginUtils";
|
||||
import { PluginData } from "knub";
|
||||
import { ArgsFromSignatureOrArray } from "knub/dist/commands/commandUtils";
|
||||
import { searchCmdSignature } from "./SearchCmd";
|
||||
import { banSearchSignature } from "./BanSearchCmd";
|
||||
import { UtilityPluginType } from "../types";
|
||||
import { refreshMembersIfNeeded } from "../refreshMembers";
|
||||
|
||||
const SEARCH_RESULTS_PER_PAGE = 15;
|
||||
const SEARCH_ID_RESULTS_PER_PAGE = 50;
|
||||
const SEARCH_EXPORT_LIMIT = 1_000_000;
|
||||
|
||||
export enum SearchType {
|
||||
MemberSearch,
|
||||
BanSearch,
|
||||
}
|
||||
|
||||
class SearchError extends Error {}
|
||||
|
||||
type MemberSearchParams = ArgsFromSignatureOrArray<typeof searchCmdSignature>;
|
||||
type BanSearchParams = ArgsFromSignatureOrArray<typeof banSearchSignature>;
|
||||
|
||||
export async function displaySearch(
|
||||
pluginData: PluginData<UtilityPluginType>,
|
||||
args: MemberSearchParams,
|
||||
searchType: SearchType.MemberSearch,
|
||||
msg: Message,
|
||||
);
|
||||
export async function displaySearch(
|
||||
pluginData: PluginData<UtilityPluginType>,
|
||||
args: BanSearchParams,
|
||||
searchType: SearchType.BanSearch,
|
||||
msg: Message,
|
||||
);
|
||||
export async function displaySearch(
|
||||
pluginData: PluginData<UtilityPluginType>,
|
||||
args: MemberSearchParams | BanSearchParams,
|
||||
searchType: SearchType,
|
||||
msg: Message,
|
||||
) {
|
||||
// If we're not exporting, load 1 page of search results at a time and allow the user to switch pages with reactions
|
||||
let originalSearchMsg: Message = null;
|
||||
let searching = false;
|
||||
let currentPage = args.page || 1;
|
||||
let hasReactions = false;
|
||||
let clearReactionsFn = null;
|
||||
let clearReactionsTimeout = null;
|
||||
|
||||
const perPage = args.ids ? SEARCH_ID_RESULTS_PER_PAGE : SEARCH_RESULTS_PER_PAGE;
|
||||
|
||||
const loadSearchPage = async page => {
|
||||
if (searching) return;
|
||||
searching = true;
|
||||
|
||||
// The initial message is created here, as well as edited to say "Searching..." on subsequent requests
|
||||
// We don't "await" this so we can start loading the search results immediately instead of after the message has been created/edited
|
||||
let searchMsgPromise: Promise<Message>;
|
||||
if (originalSearchMsg) {
|
||||
searchMsgPromise = originalSearchMsg.edit("Searching...");
|
||||
} else {
|
||||
searchMsgPromise = msg.channel.createMessage("Searching...");
|
||||
searchMsgPromise.then(m => (originalSearchMsg = m));
|
||||
}
|
||||
|
||||
let searchResult;
|
||||
try {
|
||||
switch (searchType) {
|
||||
case SearchType.MemberSearch:
|
||||
searchResult = await performMemberSearch(pluginData, args as MemberSearchParams, page, perPage);
|
||||
break;
|
||||
case SearchType.BanSearch:
|
||||
searchResult = await performBanSearch(pluginData, args as BanSearchParams, page, perPage);
|
||||
break;
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof SearchError) {
|
||||
return sendErrorMessage(pluginData, msg.channel, e.message);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
|
||||
if (searchResult.totalResults === 0) {
|
||||
return sendErrorMessage(pluginData, msg.channel, "No results found");
|
||||
}
|
||||
|
||||
const resultWord = searchResult.totalResults === 1 ? "matching member" : "matching members";
|
||||
const headerText =
|
||||
searchResult.totalResults > perPage
|
||||
? trimLines(`
|
||||
**Page ${searchResult.page}** (${searchResult.from}-${searchResult.to}) (total ${searchResult.totalResults})
|
||||
`)
|
||||
: `Found ${searchResult.totalResults} ${resultWord}`;
|
||||
|
||||
const resultList = args.ids
|
||||
? formatSearchResultIdList(searchResult.results)
|
||||
: formatSearchResultList(searchResult.results);
|
||||
|
||||
const result = trimLines(`
|
||||
${headerText}
|
||||
\`\`\`js
|
||||
${resultList}
|
||||
\`\`\`
|
||||
`);
|
||||
|
||||
const searchMsg = await searchMsgPromise;
|
||||
searchMsg.edit(result);
|
||||
|
||||
// Set up pagination reactions if needed. The reactions are cleared after a timeout.
|
||||
if (searchResult.totalResults > perPage) {
|
||||
if (!hasReactions) {
|
||||
hasReactions = true;
|
||||
searchMsg.addReaction("⬅");
|
||||
searchMsg.addReaction("➡");
|
||||
searchMsg.addReaction("🔄");
|
||||
|
||||
const listenerFn = pluginData.events.on("messageReactionAdd", ({ args: { message: rMsg, emoji, userID } }) => {
|
||||
if (rMsg.id !== searchMsg.id) return;
|
||||
if (userID !== msg.author.id) return;
|
||||
if (!["⬅", "➡", "🔄"].includes(emoji.name)) return;
|
||||
|
||||
if (emoji.name === "⬅" && currentPage > 1) {
|
||||
loadSearchPage(currentPage - 1);
|
||||
} else if (emoji.name === "➡" && currentPage < searchResult.lastPage) {
|
||||
loadSearchPage(currentPage + 1);
|
||||
} else if (emoji.name === "🔄") {
|
||||
loadSearchPage(currentPage);
|
||||
}
|
||||
|
||||
if (isFullMessage(rMsg)) {
|
||||
rMsg.removeReaction(emoji.name, userID);
|
||||
}
|
||||
});
|
||||
|
||||
clearReactionsFn = async () => {
|
||||
searchMsg.removeReactions().catch(noop);
|
||||
pluginData.events.off("messageReactionAdd", listenerFn);
|
||||
};
|
||||
}
|
||||
|
||||
clearTimeout(clearReactionsTimeout);
|
||||
clearReactionsTimeout = setTimeout(clearReactionsFn, 5 * MINUTES);
|
||||
}
|
||||
|
||||
currentPage = searchResult.page;
|
||||
searching = false;
|
||||
};
|
||||
|
||||
loadSearchPage(currentPage);
|
||||
}
|
||||
|
||||
export async function archiveSearch(
|
||||
pluginData: PluginData<UtilityPluginType>,
|
||||
args: MemberSearchParams,
|
||||
searchType: SearchType.MemberSearch,
|
||||
msg: Message,
|
||||
);
|
||||
export async function archiveSearch(
|
||||
pluginData: PluginData<UtilityPluginType>,
|
||||
args: BanSearchParams,
|
||||
searchType: SearchType.BanSearch,
|
||||
msg: Message,
|
||||
);
|
||||
export async function archiveSearch(
|
||||
pluginData: PluginData<UtilityPluginType>,
|
||||
args: MemberSearchParams | BanSearchParams,
|
||||
searchType: SearchType,
|
||||
msg: Message,
|
||||
) {
|
||||
let results;
|
||||
try {
|
||||
switch (searchType) {
|
||||
case SearchType.MemberSearch:
|
||||
results = await performMemberSearch(pluginData, args as MemberSearchParams, 1, SEARCH_EXPORT_LIMIT);
|
||||
break;
|
||||
case SearchType.BanSearch:
|
||||
results = await performBanSearch(pluginData, args as BanSearchParams, 1, SEARCH_EXPORT_LIMIT);
|
||||
break;
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof SearchError) {
|
||||
return sendErrorMessage(pluginData, msg.channel, e.message);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
|
||||
if (results.totalResults === 0) {
|
||||
return sendErrorMessage(pluginData, msg.channel, "No results found");
|
||||
}
|
||||
|
||||
const resultList = args.ids ? formatSearchResultIdList(results.results) : formatSearchResultList(results.results);
|
||||
|
||||
const archiveId = await pluginData.state.archives.create(
|
||||
trimLines(`
|
||||
Search results (total ${results.totalResults}):
|
||||
|
||||
${resultList}
|
||||
`),
|
||||
moment().add(1, "hour"),
|
||||
);
|
||||
|
||||
const baseUrl = (pluginData.getKnubInstance().getGlobalConfig() as any).url; // FIXME: No any cast
|
||||
const url = await pluginData.state.archives.getUrl(baseUrl, archiveId);
|
||||
|
||||
msg.channel.createMessage(`Exported search results: ${url}`);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
async function performMemberSearch(
|
||||
pluginData: PluginData<UtilityPluginType>,
|
||||
args: MemberSearchParams,
|
||||
page = 1,
|
||||
perPage = SEARCH_RESULTS_PER_PAGE,
|
||||
): Promise<{ results: Member[]; totalResults: number; page: number; lastPage: number; from: number; to: number }> {
|
||||
refreshMembersIfNeeded(pluginData.guild);
|
||||
|
||||
let matchingMembers = Array.from(pluginData.guild.members.values());
|
||||
|
||||
if (args.role) {
|
||||
const roleIds = args.role.split(",");
|
||||
matchingMembers = matchingMembers.filter(member => {
|
||||
for (const role of roleIds) {
|
||||
if (!member.roles.includes(role)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
if (args.voice) {
|
||||
matchingMembers = matchingMembers.filter(m => m.voiceState.channelID != null);
|
||||
}
|
||||
|
||||
if (args.bot) {
|
||||
matchingMembers = matchingMembers.filter(m => m.bot);
|
||||
}
|
||||
|
||||
if (args.query) {
|
||||
let queryRegex: RegExp;
|
||||
if (args.regex) {
|
||||
queryRegex = new RegExp(args.query.trimStart(), args["case-sensitive"] ? "" : "i");
|
||||
} else {
|
||||
queryRegex = new RegExp(escapeStringRegexp(args.query.trimStart()), args["case-sensitive"] ? "" : "i");
|
||||
}
|
||||
|
||||
if (!safeRegex(queryRegex)) {
|
||||
throw new SearchError("Unsafe/too complex regex (star depth is limited to 1)");
|
||||
}
|
||||
|
||||
if (args["status-search"]) {
|
||||
matchingMembers = matchingMembers.filter(member => {
|
||||
if (member.game) {
|
||||
if (member.game.name && member.game.name.match(queryRegex)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (member.game.state && member.game.state.match(queryRegex)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (member.game.details && member.game.details.match(queryRegex)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (member.game.assets) {
|
||||
if (member.game.assets.small_text && member.game.assets.small_text.match(queryRegex)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (member.game.assets.large_text && member.game.assets.large_text.match(queryRegex)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (member.game.emoji && member.game.emoji.name.match(queryRegex)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
} else {
|
||||
matchingMembers = matchingMembers.filter(member => {
|
||||
if (member.nick && member.nick.match(queryRegex)) return true;
|
||||
|
||||
const fullUsername = `${member.user.username}#${member.user.discriminator}`;
|
||||
if (fullUsername.match(queryRegex)) return true;
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const [, sortDir, sortBy] = args.sort ? args.sort.match(/^(-?)(.*)$/) : [null, "ASC", "name"];
|
||||
const realSortDir = sortDir === "-" ? "DESC" : "ASC";
|
||||
|
||||
if (sortBy === "id") {
|
||||
matchingMembers.sort(sorter(m => BigInt(m.id), realSortDir));
|
||||
} else {
|
||||
matchingMembers.sort(
|
||||
multiSorter([
|
||||
[m => m.username.toLowerCase(), realSortDir],
|
||||
[m => m.discriminator, realSortDir],
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
const lastPage = Math.max(1, Math.ceil(matchingMembers.length / perPage));
|
||||
page = Math.min(lastPage, Math.max(1, page));
|
||||
|
||||
const from = (page - 1) * perPage;
|
||||
const to = Math.min(from + perPage, matchingMembers.length);
|
||||
|
||||
const pageMembers = matchingMembers.slice(from, to);
|
||||
|
||||
return {
|
||||
results: pageMembers,
|
||||
totalResults: matchingMembers.length,
|
||||
page,
|
||||
lastPage,
|
||||
from: from + 1,
|
||||
to,
|
||||
};
|
||||
}
|
||||
|
||||
async function performBanSearch(
|
||||
pluginData: PluginData<UtilityPluginType>,
|
||||
args: BanSearchParams,
|
||||
page = 1,
|
||||
perPage = SEARCH_RESULTS_PER_PAGE,
|
||||
): Promise<{ results: User[]; totalResults: number; page: number; lastPage: number; from: number; to: number }> {
|
||||
let matchingBans = (await pluginData.guild.getBans()).map(x => x.user);
|
||||
|
||||
if (args.query) {
|
||||
let queryRegex: RegExp;
|
||||
if (args.regex) {
|
||||
queryRegex = new RegExp(args.query.trimStart(), args["case-sensitive"] ? "" : "i");
|
||||
} else {
|
||||
queryRegex = new RegExp(escapeStringRegexp(args.query.trimStart()), args["case-sensitive"] ? "" : "i");
|
||||
}
|
||||
|
||||
if (!safeRegex(queryRegex)) {
|
||||
throw new SearchError("Unsafe/too complex regex (star depth is limited to 1)");
|
||||
}
|
||||
|
||||
matchingBans = matchingBans.filter(user => {
|
||||
const fullUsername = `${user.username}#${user.discriminator}`;
|
||||
if (fullUsername.match(queryRegex)) return true;
|
||||
});
|
||||
}
|
||||
|
||||
const [, sortDir, sortBy] = args.sort ? args.sort.match(/^(-?)(.*)$/) : [null, "ASC", "name"];
|
||||
const realSortDir = sortDir === "-" ? "DESC" : "ASC";
|
||||
|
||||
if (sortBy === "id") {
|
||||
matchingBans.sort(sorter(m => BigInt(m.id), realSortDir));
|
||||
} else {
|
||||
matchingBans.sort(
|
||||
multiSorter([
|
||||
[m => m.username.toLowerCase(), realSortDir],
|
||||
[m => m.discriminator, realSortDir],
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
const lastPage = Math.max(1, Math.ceil(matchingBans.length / perPage));
|
||||
page = Math.min(lastPage, Math.max(1, page));
|
||||
|
||||
const from = (page - 1) * perPage;
|
||||
const to = Math.min(from + perPage, matchingBans.length);
|
||||
|
||||
const pageMembers = matchingBans.slice(from, to);
|
||||
|
||||
return {
|
||||
results: pageMembers,
|
||||
totalResults: matchingBans.length,
|
||||
page,
|
||||
lastPage,
|
||||
from: from + 1,
|
||||
to,
|
||||
};
|
||||
}
|
||||
|
||||
function formatSearchResultList(members: Array<Member | User>): string {
|
||||
const longestId = members.reduce((longest, member) => Math.max(longest, member.id.length), 0);
|
||||
const lines = members.map(member => {
|
||||
const paddedId = member.id.padEnd(longestId, " ");
|
||||
let line;
|
||||
if (member instanceof Member) {
|
||||
line = `${paddedId} ${member.user.username}#${member.user.discriminator}`;
|
||||
if (member.nick) line += ` (${member.nick})`;
|
||||
} else {
|
||||
line = `${paddedId} ${member.username}#${member.discriminator}`;
|
||||
}
|
||||
return line;
|
||||
});
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
function formatSearchResultIdList(members: Array<Member | User>): string {
|
||||
return members.map(m => m.id).join(" ");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue