From a568e86d781146cf740d821a27f18fcc3e48e783 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Mon, 3 May 2021 19:33:30 +0300 Subject: [PATCH] Add commands to list and reset counters --- backend/src/data/GuildCounters.ts | 6 + .../src/plugins/Counters/CountersPlugin.ts | 7 ++ .../Counters/commands/CountersListCmd.ts | 52 ++++++++ .../commands/ResetAllCounterValuesCmd.ts | 54 +++++++++ .../Counters/commands/ResetCounterCmd.ts | 111 ++++++++++++++++++ .../functions/resetAllCounterValues.ts | 18 +++ backend/src/plugins/Counters/types.ts | 2 + backend/src/utils.ts | 10 +- 8 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 backend/src/plugins/Counters/commands/CountersListCmd.ts create mode 100644 backend/src/plugins/Counters/commands/ResetAllCounterValuesCmd.ts create mode 100644 backend/src/plugins/Counters/commands/ResetCounterCmd.ts create mode 100644 backend/src/plugins/Counters/functions/resetAllCounterValues.ts diff --git a/backend/src/data/GuildCounters.ts b/backend/src/data/GuildCounters.ts index 8963afcb..92395e0f 100644 --- a/backend/src/data/GuildCounters.ts +++ b/backend/src/data/GuildCounters.ts @@ -527,4 +527,10 @@ export class GuildCounters extends BaseGuildRepository { return value?.value; } + + async resetAllCounterValues(counterId: number): Promise { + await this.counterValues.delete({ + counter_id: counterId, + }); + } } diff --git a/backend/src/plugins/Counters/CountersPlugin.ts b/backend/src/plugins/Counters/CountersPlugin.ts index e9d4ddab..8b8f0f15 100644 --- a/backend/src/plugins/Counters/CountersPlugin.ts +++ b/backend/src/plugins/Counters/CountersPlugin.ts @@ -25,6 +25,9 @@ import { import { getPrettyNameForCounter } from "./functions/getPrettyNameForCounter"; import { getPrettyNameForCounterTrigger } from "./functions/getPrettyNameForCounterTrigger"; import { counterExists } from "./functions/counterExists"; +import { ResetAllCounterValuesCmd } from "./commands/ResetAllCounterValuesCmd"; +import { CountersListCmd } from "./commands/CountersListCmd"; +import { ResetCounterCmd } from "./commands/ResetCounterCmd"; const MAX_COUNTERS = 5; const MAX_TRIGGERS_PER_COUNTER = 5; @@ -35,6 +38,7 @@ const defaultOptions: PluginOptions = { counters: {}, can_view: false, can_edit: false, + can_reset_all: false, }, overrides: [ { @@ -133,9 +137,12 @@ export const CountersPlugin = zeppelinGuildPlugin()("counter // prettier-ignore commands: [ + CountersListCmd, ViewCounterCmd, AddCounterCmd, SetCounterCmd, + ResetCounterCmd, + ResetAllCounterValuesCmd, ], async onLoad(pluginData) { diff --git a/backend/src/plugins/Counters/commands/CountersListCmd.ts b/backend/src/plugins/Counters/commands/CountersListCmd.ts new file mode 100644 index 00000000..d8b82361 --- /dev/null +++ b/backend/src/plugins/Counters/commands/CountersListCmd.ts @@ -0,0 +1,52 @@ +import { guildCommand } from "knub"; +import { CountersPluginType } from "../types"; +import { sendErrorMessage } from "../../../pluginUtils"; +import { trimMultilineString, ucfirst } from "../../../utils"; +import { getGuildPrefix } from "../../../utils/getGuildPrefix"; + +export const CountersListCmd = guildCommand()({ + trigger: ["counters", "counters list", "counter list"], + permission: "can_view", + + signature: {}, + + async run({ pluginData, message, args }) { + const config = pluginData.config.getForMessage(message); + + const countersToShow = Array.from(Object.values(config.counters)).filter(c => c.can_view !== false); + if (!countersToShow.length) { + sendErrorMessage(pluginData, message.channel, "No counters are configured for this server"); + return; + } + + const counterLines = countersToShow.map(counter => { + const title = counter.pretty_name ? `**${counter.pretty_name}** (\`${counter.name}\`)` : `\`${counter.name}\``; + + const types: string[] = []; + if (counter.per_user) types.push("per user"); + if (counter.per_channel) types.push("per channel"); + const typeInfo = types.length ? types.join(", ") : "global"; + + const decayInfo = counter.decay ? `decays ${counter.decay.amount} every ${counter.decay.every}` : null; + + const info = [typeInfo, decayInfo].filter(Boolean); + return `${title}\n${ucfirst(info.join("; "))}`; + }); + + const hintLines = [`Use \`${getGuildPrefix(pluginData)}counters view \` to view a counter's value`]; + if (config.can_edit) { + hintLines.push(`Use \`${getGuildPrefix(pluginData)}counters set \` to change a counter's value`); + } + if (config.can_reset_all) { + hintLines.push(`Use \`${getGuildPrefix(pluginData)}counters reset_all \` to reset a counter entirely`); + } + + message.channel.createMessage( + trimMultilineString(` + ${counterLines.join("\n\n")} + + ${hintLines.join("\n")} + `), + ); + }, +}); diff --git a/backend/src/plugins/Counters/commands/ResetAllCounterValuesCmd.ts b/backend/src/plugins/Counters/commands/ResetAllCounterValuesCmd.ts new file mode 100644 index 00000000..25ff5ec4 --- /dev/null +++ b/backend/src/plugins/Counters/commands/ResetAllCounterValuesCmd.ts @@ -0,0 +1,54 @@ +import { guildCommand } from "knub"; +import { CountersPluginType } from "../types"; +import { commandTypeHelpers as ct } from "../../../commandTypes"; +import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils"; +import { resolveChannel, waitForReply } from "knub/dist/helpers"; +import { TextChannel, User } from "eris"; +import { confirm, resolveUser, trimMultilineString, UnknownUser } from "../../../utils"; +import { changeCounterValue } from "../functions/changeCounterValue"; +import { setCounterValue } from "../functions/setCounterValue"; +import { resetAllCounterValues } from "../functions/resetAllCounterValues"; + +export const ResetAllCounterValuesCmd = guildCommand()({ + trigger: ["counters reset_all"], + permission: "can_reset_all", + + signature: { + counterName: ct.string(), + }, + + async run({ pluginData, message, args }) { + const config = pluginData.config.getForMessage(message); + const counter = config.counters[args.counterName]; + const counterId = pluginData.state.counterIds[args.counterName]; + if (!counter || !counterId) { + sendErrorMessage(pluginData, message.channel, `Unknown counter: ${args.counterName}`); + return; + } + + if (counter.can_reset_all === false) { + sendErrorMessage(pluginData, message.channel, `Missing permissions to reset all of this counter's values`); + return; + } + + const counterName = counter.name || args.counterName; + const confirmed = await confirm( + pluginData.client, + message.channel, + message.author.id, + trimMultilineString(` + Do you want to reset **ALL** values for counter **${counterName}**? + This will reset the counter for **all** users and channels. + **Note:** This will *not* trigger any triggers or counter triggers. + `), + ); + if (!confirmed) { + sendErrorMessage(pluginData, message.channel, "Cancelled"); + return; + } + + await resetAllCounterValues(pluginData, args.counterName); + + sendSuccessMessage(pluginData, message.channel, `All counter values for **${counterName}** have been reset`); + }, +}); diff --git a/backend/src/plugins/Counters/commands/ResetCounterCmd.ts b/backend/src/plugins/Counters/commands/ResetCounterCmd.ts new file mode 100644 index 00000000..cf899fc3 --- /dev/null +++ b/backend/src/plugins/Counters/commands/ResetCounterCmd.ts @@ -0,0 +1,111 @@ +import { guildCommand } from "knub"; +import { CountersPluginType } from "../types"; +import { commandTypeHelpers as ct } from "../../../commandTypes"; +import { sendErrorMessage } from "../../../pluginUtils"; +import { resolveChannel, waitForReply } from "knub/dist/helpers"; +import { TextChannel } from "eris"; +import { resolveUser, UnknownUser } from "../../../utils"; +import { setCounterValue } from "../functions/setCounterValue"; + +export const ResetCounterCmd = guildCommand()({ + trigger: ["counters reset", "counter reset", "resetcounter"], + permission: "can_edit", + + signature: [ + { + counterName: ct.string(), + }, + { + counterName: ct.string(), + user: ct.resolvedUser(), + }, + { + counterName: ct.string(), + channel: ct.textChannel(), + }, + { + counterName: ct.string(), + channel: ct.textChannel(), + user: ct.resolvedUser(), + }, + { + counterName: ct.string(), + user: ct.resolvedUser(), + channel: ct.textChannel(), + }, + ], + + async run({ pluginData, message, args }) { + const config = pluginData.config.getForMessage(message); + const counter = config.counters[args.counterName]; + const counterId = pluginData.state.counterIds[args.counterName]; + if (!counter || !counterId) { + sendErrorMessage(pluginData, message.channel, `Unknown counter: ${args.counterName}`); + return; + } + + if (counter.can_edit === false) { + sendErrorMessage(pluginData, message.channel, `Missing permissions to reset this counter's value`); + return; + } + + if (args.channel && !counter.per_channel) { + sendErrorMessage(pluginData, message.channel, `This counter is not per-channel`); + return; + } + + if (args.user && !counter.per_user) { + sendErrorMessage(pluginData, message.channel, `This counter is not per-user`); + return; + } + + let channel = args.channel; + if (!channel && counter.per_channel) { + message.channel.createMessage(`Which channel's counter value would you like to reset?`); + const reply = await waitForReply(pluginData.client, message.channel, message.author.id); + if (!reply || !reply.content) { + sendErrorMessage(pluginData, message.channel, "Cancelling"); + return; + } + + const potentialChannel = resolveChannel(pluginData.guild, reply.content); + if (!potentialChannel || !(potentialChannel instanceof TextChannel)) { + sendErrorMessage(pluginData, message.channel, "Channel is not a text channel, cancelling"); + return; + } + + channel = potentialChannel; + } + + let user = args.user; + if (!user && counter.per_user) { + message.channel.createMessage(`Which user's counter value would you like to reset?`); + const reply = await waitForReply(pluginData.client, message.channel, message.author.id); + if (!reply || !reply.content) { + sendErrorMessage(pluginData, message.channel, "Cancelling"); + return; + } + + const potentialUser = await resolveUser(pluginData.client, reply.content); + if (!potentialUser || potentialUser instanceof UnknownUser) { + sendErrorMessage(pluginData, message.channel, "Unknown user, cancelling"); + return; + } + + user = potentialUser; + } + + await setCounterValue(pluginData, args.counterName, channel?.id ?? null, user?.id ?? null, counter.initial_value); + const counterName = counter.name || args.counterName; + + if (channel && user) { + message.channel.createMessage(`Reset **${counterName}** for <@!${user.id}> in <#${channel.id}>`); + } else if (channel) { + message.channel.createMessage(`Reset **${counterName}** in <#${channel.id}>`); + } else if (user) { + message.channel.createMessage(`Reset **${counterName}** for <@!${user.id}>`); + } else { + message.channel.createMessage(`Reset **${counterName}**`); + } + }, +}); diff --git a/backend/src/plugins/Counters/functions/resetAllCounterValues.ts b/backend/src/plugins/Counters/functions/resetAllCounterValues.ts new file mode 100644 index 00000000..c145a876 --- /dev/null +++ b/backend/src/plugins/Counters/functions/resetAllCounterValues.ts @@ -0,0 +1,18 @@ +import { GuildPluginData } from "knub"; +import { counterIdLock } from "../../../utils/lockNameHelpers"; +import { CountersPluginType } from "../types"; + +export async function resetAllCounterValues(pluginData: GuildPluginData, counterName: string) { + const config = pluginData.config.get(); + const counter = config.counters[counterName]; + if (!counter) { + throw new Error(`Unknown counter: ${counterName}`); + } + + const counterId = pluginData.state.counterIds[counterName]; + const lock = await pluginData.locks.acquire(counterIdLock(counterId)); + + await pluginData.state.counters.resetAllCounterValues(counterId); + + lock.unlock(); +} diff --git a/backend/src/plugins/Counters/types.ts b/backend/src/plugins/Counters/types.ts index ec534452..1efdc474 100644 --- a/backend/src/plugins/Counters/types.ts +++ b/backend/src/plugins/Counters/types.ts @@ -29,6 +29,7 @@ export const Counter = t.type({ ), can_view: tNullable(t.boolean), can_edit: tNullable(t.boolean), + can_reset_all: tNullable(t.boolean), }); export type TCounter = t.TypeOf; @@ -36,6 +37,7 @@ export const ConfigSchema = t.type({ counters: t.record(t.string, Counter), can_view: t.boolean, can_edit: t.boolean, + can_reset_all: t.boolean, }); export type TConfigSchema = t.TypeOf; diff --git a/backend/src/utils.ts b/backend/src/utils.ts index d8e73a77..5607bfa8 100644 --- a/backend/src/utils.ts +++ b/backend/src/utils.ts @@ -667,9 +667,10 @@ export function trimEmptyStartEndLines(str: string) { } export function trimIndents(str: string, indentLength: number) { + const regex = new RegExp(`^\\s{0,${indentLength}}`, "g"); return str .split("\n") - .map(line => line.slice(indentLength)) + .map(line => line.replace(regex, "")) .join("\n"); } @@ -1404,12 +1405,17 @@ export function canUseEmoji(client: Client, emoji: string): boolean { return false; } -export function trimPluginDescription(str) { +/** + * Trims any empty lines from the beginning and end of the given string + * and indents matching the first line's indent + */ +export function trimMultilineString(str) { const emptyLinesTrimmed = trimEmptyStartEndLines(str); const lines = emptyLinesTrimmed.split("\n"); const firstLineIndentation = (lines[0].match(/^ +/g) || [""])[0].length; return trimIndents(emptyLinesTrimmed, firstLineIndentation); } +export const trimPluginDescription = trimMultilineString; export function isFullMessage(msg: PossiblyUncachedMessage): msg is Message { return (msg as Message).createdAt != null;