Add pagination to !cases -mod
This commit is contained in:
parent
0c6ec9cef0
commit
5056b4376a
5 changed files with 87 additions and 31 deletions
|
@ -82,7 +82,17 @@ export class GuildCases extends BaseGuildRepository {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRecentByModId(modId: string, count: number): Promise<Case[]> {
|
async getTotalCasesByModId(modId: string): Promise<number> {
|
||||||
|
return this.cases.count({
|
||||||
|
where: {
|
||||||
|
guild_id: this.guildId,
|
||||||
|
mod_id: modId,
|
||||||
|
is_hidden: 0,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getRecentByModId(modId: string, count: number, skip = 0): Promise<Case[]> {
|
||||||
return this.cases.find({
|
return this.cases.find({
|
||||||
relations: this.getRelations(),
|
relations: this.getRelations(),
|
||||||
where: {
|
where: {
|
||||||
|
@ -90,6 +100,7 @@ export class GuildCases extends BaseGuildRepository {
|
||||||
mod_id: modId,
|
mod_id: modId,
|
||||||
is_hidden: 0,
|
is_hidden: 0,
|
||||||
},
|
},
|
||||||
|
skip,
|
||||||
take: count,
|
take: count,
|
||||||
order: {
|
order: {
|
||||||
case_number: "DESC",
|
case_number: "DESC",
|
||||||
|
|
|
@ -14,6 +14,8 @@ import { trimPluginDescription } from "../../utils";
|
||||||
import { getCaseSummary } from "./functions/getCaseSummary";
|
import { getCaseSummary } from "./functions/getCaseSummary";
|
||||||
import { TimeAndDatePlugin } from "../TimeAndDate/TimeAndDatePlugin";
|
import { TimeAndDatePlugin } from "../TimeAndDate/TimeAndDatePlugin";
|
||||||
import { mapToPublicFn } from "../../pluginUtils";
|
import { mapToPublicFn } from "../../pluginUtils";
|
||||||
|
import { getTotalCasesByMod } from "./functions/getTotalCasesByMod";
|
||||||
|
import { getRecentCasesByMod } from "./functions/getRecentCasesByMod";
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
config: {
|
config: {
|
||||||
|
@ -64,6 +66,9 @@ export const CasesPlugin = zeppelinGuildPlugin<CasesPluginType>()("cases", {
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getTotalCasesByMod: mapToPublicFn(getTotalCasesByMod),
|
||||||
|
getRecentCasesByMod: mapToPublicFn(getRecentCasesByMod),
|
||||||
|
|
||||||
getCaseEmbed: mapToPublicFn(getCaseEmbed),
|
getCaseEmbed: mapToPublicFn(getCaseEmbed),
|
||||||
getCaseSummary: mapToPublicFn(getCaseSummary),
|
getCaseSummary: mapToPublicFn(getCaseSummary),
|
||||||
},
|
},
|
||||||
|
|
12
backend/src/plugins/Cases/functions/getRecentCasesByMod.ts
Normal file
12
backend/src/plugins/Cases/functions/getRecentCasesByMod.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import { GuildPluginData } from "knub";
|
||||||
|
import { CasesPluginType } from "../types";
|
||||||
|
import { Case } from "../../../data/entities/Case";
|
||||||
|
|
||||||
|
export function getRecentCasesByMod(
|
||||||
|
pluginData: GuildPluginData<CasesPluginType>,
|
||||||
|
modId: string,
|
||||||
|
count: number,
|
||||||
|
skip = 0,
|
||||||
|
): Promise<Case[]> {
|
||||||
|
return pluginData.state.cases.getRecentByModId(modId, count, skip);
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { GuildPluginData } from "knub";
|
||||||
|
import { CasesPluginType } from "../types";
|
||||||
|
|
||||||
|
export function getTotalCasesByMod(pluginData: GuildPluginData<CasesPluginType>, modId: string): Promise<number> {
|
||||||
|
return pluginData.state.cases.getTotalCasesByModId(modId);
|
||||||
|
}
|
|
@ -1,18 +1,21 @@
|
||||||
import { modActionsCmd } from "../types";
|
import { modActionsCmd } from "../types";
|
||||||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||||
import { sendErrorMessage } from "../../../pluginUtils";
|
import { sendErrorMessage } from "../../../pluginUtils";
|
||||||
import { trimLines, createChunkedMessage, emptyEmbedValue, sorter } from "../../../utils";
|
import { trimLines, createChunkedMessage, emptyEmbedValue, sorter, resolveUser } from "../../../utils";
|
||||||
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
||||||
import { asyncMap } from "../../../utils/async";
|
import { asyncMap } from "../../../utils/async";
|
||||||
import { EmbedOptions } from "eris";
|
import { EmbedOptions, User } from "eris";
|
||||||
import { getChunkedEmbedFields } from "../../../utils/getChunkedEmbedFields";
|
import { getChunkedEmbedFields } from "../../../utils/getChunkedEmbedFields";
|
||||||
import { getDefaultPrefix } from "knub/dist/commands/commandUtils";
|
import { getDefaultPrefix } from "knub/dist/commands/commandUtils";
|
||||||
import { getGuildPrefix } from "../../../utils/getGuildPrefix";
|
import { getGuildPrefix } from "../../../utils/getGuildPrefix";
|
||||||
|
import { createPaginatedMessage } from "../../../utils/createPaginatedMessage";
|
||||||
|
|
||||||
const opts = {
|
const opts = {
|
||||||
mod: ct.resolvedMember({ option: true }),
|
mod: ct.userId({ option: true }),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const casesPerPage = 5;
|
||||||
|
|
||||||
export const CasesModCmd = modActionsCmd({
|
export const CasesModCmd = modActionsCmd({
|
||||||
trigger: ["cases", "modlogs"],
|
trigger: ["cases", "modlogs"],
|
||||||
permission: "can_view",
|
permission: "can_view",
|
||||||
|
@ -25,36 +28,55 @@ export const CasesModCmd = modActionsCmd({
|
||||||
],
|
],
|
||||||
|
|
||||||
async run({ pluginData, message: msg, args }) {
|
async run({ pluginData, message: msg, args }) {
|
||||||
const modId = args.mod ? args.mod.id : msg.author.id;
|
const modId = args.mod || msg.author.id;
|
||||||
const recentCases = await pluginData.state.cases.with("notes").getRecentByModId(modId, 5);
|
const mod = await resolveUser(pluginData.client, modId);
|
||||||
recentCases.sort(sorter("case_number", "ASC"));
|
const modName = mod instanceof User ? `${mod.username}#${mod.discriminator}` : modId;
|
||||||
|
|
||||||
const mod = pluginData.client.users.get(modId);
|
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
||||||
const modName = mod ? `${mod.username}#${mod.discriminator}` : modId;
|
const totalCases = await casesPlugin.getTotalCasesByMod(modId);
|
||||||
|
|
||||||
if (recentCases.length === 0) {
|
if (totalCases === 0) {
|
||||||
sendErrorMessage(pluginData, msg.channel, `No cases by **${modName}**`);
|
sendErrorMessage(pluginData, msg.channel, `No cases by **${modName}**`);
|
||||||
} else {
|
return;
|
||||||
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
|
||||||
const lines = await asyncMap(recentCases, c => casesPlugin.getCaseSummary(c, true, msg.author.id));
|
|
||||||
const prefix = getGuildPrefix(pluginData);
|
|
||||||
const embed: EmbedOptions = {
|
|
||||||
author: {
|
|
||||||
name: `Most recent 5 cases by ${modName}`,
|
|
||||||
icon_url: mod ? mod.avatarURL || mod.defaultAvatarURL : undefined,
|
|
||||||
},
|
|
||||||
fields: [
|
|
||||||
...getChunkedEmbedFields(emptyEmbedValue, lines.join("\n")),
|
|
||||||
{
|
|
||||||
name: emptyEmbedValue,
|
|
||||||
value: trimLines(`
|
|
||||||
Use \`${prefix}case <num>\` to see more information about an individual case
|
|
||||||
Use \`${prefix}cases <user>\` to see a specific user's cases
|
|
||||||
`),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
msg.channel.createMessage({ embed });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const totalPages = Math.max(Math.ceil(totalCases / casesPerPage), 1);
|
||||||
|
const prefix = getGuildPrefix(pluginData);
|
||||||
|
|
||||||
|
createPaginatedMessage(
|
||||||
|
pluginData.client,
|
||||||
|
msg.channel,
|
||||||
|
totalPages,
|
||||||
|
async page => {
|
||||||
|
const cases = await casesPlugin.getRecentCasesByMod(modId, casesPerPage, (page - 1) * casesPerPage);
|
||||||
|
const lines = await asyncMap(cases, c => casesPlugin.getCaseSummary(c, true, msg.author.id));
|
||||||
|
|
||||||
|
const firstCaseNum = (page - 1) * casesPerPage + 1;
|
||||||
|
const lastCaseNum = page * casesPerPage;
|
||||||
|
const title = `Most recent cases ${firstCaseNum}-${lastCaseNum} of ${totalCases} by ${modName}`;
|
||||||
|
|
||||||
|
const embed: EmbedOptions = {
|
||||||
|
author: {
|
||||||
|
name: title,
|
||||||
|
icon_url: mod instanceof User ? mod.avatarURL || mod.defaultAvatarURL : undefined,
|
||||||
|
},
|
||||||
|
fields: [
|
||||||
|
...getChunkedEmbedFields(emptyEmbedValue, lines.join("\n")),
|
||||||
|
{
|
||||||
|
name: emptyEmbedValue,
|
||||||
|
value: trimLines(`
|
||||||
|
Use \`${prefix}case <num>\` to see more information about an individual case
|
||||||
|
Use \`${prefix}cases <user>\` to see a specific user's cases
|
||||||
|
`),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
return { embed };
|
||||||
|
},
|
||||||
|
{
|
||||||
|
limitToUserId: msg.author.id,
|
||||||
|
},
|
||||||
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue