3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

Add commands to list and reset counters

This commit is contained in:
Dragory 2021-05-03 19:33:30 +03:00
parent 25a3350196
commit a568e86d78
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
8 changed files with 258 additions and 2 deletions

View file

@ -527,4 +527,10 @@ export class GuildCounters extends BaseGuildRepository {
return value?.value; return value?.value;
} }
async resetAllCounterValues(counterId: number): Promise<void> {
await this.counterValues.delete({
counter_id: counterId,
});
}
} }

View file

@ -25,6 +25,9 @@ import {
import { getPrettyNameForCounter } from "./functions/getPrettyNameForCounter"; import { getPrettyNameForCounter } from "./functions/getPrettyNameForCounter";
import { getPrettyNameForCounterTrigger } from "./functions/getPrettyNameForCounterTrigger"; import { getPrettyNameForCounterTrigger } from "./functions/getPrettyNameForCounterTrigger";
import { counterExists } from "./functions/counterExists"; 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_COUNTERS = 5;
const MAX_TRIGGERS_PER_COUNTER = 5; const MAX_TRIGGERS_PER_COUNTER = 5;
@ -35,6 +38,7 @@ const defaultOptions: PluginOptions<CountersPluginType> = {
counters: {}, counters: {},
can_view: false, can_view: false,
can_edit: false, can_edit: false,
can_reset_all: false,
}, },
overrides: [ overrides: [
{ {
@ -133,9 +137,12 @@ export const CountersPlugin = zeppelinGuildPlugin<CountersPluginType>()("counter
// prettier-ignore // prettier-ignore
commands: [ commands: [
CountersListCmd,
ViewCounterCmd, ViewCounterCmd,
AddCounterCmd, AddCounterCmd,
SetCounterCmd, SetCounterCmd,
ResetCounterCmd,
ResetAllCounterValuesCmd,
], ],
async onLoad(pluginData) { async onLoad(pluginData) {

View file

@ -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<CountersPluginType>()({
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 <name>\` to view a counter's value`];
if (config.can_edit) {
hintLines.push(`Use \`${getGuildPrefix(pluginData)}counters set <name> <value>\` to change a counter's value`);
}
if (config.can_reset_all) {
hintLines.push(`Use \`${getGuildPrefix(pluginData)}counters reset_all <name>\` to reset a counter entirely`);
}
message.channel.createMessage(
trimMultilineString(`
${counterLines.join("\n\n")}
${hintLines.join("\n")}
`),
);
},
});

View file

@ -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<CountersPluginType>()({
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`);
},
});

View file

@ -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<CountersPluginType>()({
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}**`);
}
},
});

View file

@ -0,0 +1,18 @@
import { GuildPluginData } from "knub";
import { counterIdLock } from "../../../utils/lockNameHelpers";
import { CountersPluginType } from "../types";
export async function resetAllCounterValues(pluginData: GuildPluginData<CountersPluginType>, 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();
}

View file

@ -29,6 +29,7 @@ export const Counter = t.type({
), ),
can_view: tNullable(t.boolean), can_view: tNullable(t.boolean),
can_edit: tNullable(t.boolean), can_edit: tNullable(t.boolean),
can_reset_all: tNullable(t.boolean),
}); });
export type TCounter = t.TypeOf<typeof Counter>; export type TCounter = t.TypeOf<typeof Counter>;
@ -36,6 +37,7 @@ export const ConfigSchema = t.type({
counters: t.record(t.string, Counter), counters: t.record(t.string, Counter),
can_view: t.boolean, can_view: t.boolean,
can_edit: t.boolean, can_edit: t.boolean,
can_reset_all: t.boolean,
}); });
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>; export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;

View file

@ -667,9 +667,10 @@ export function trimEmptyStartEndLines(str: string) {
} }
export function trimIndents(str: string, indentLength: number) { export function trimIndents(str: string, indentLength: number) {
const regex = new RegExp(`^\\s{0,${indentLength}}`, "g");
return str return str
.split("\n") .split("\n")
.map(line => line.slice(indentLength)) .map(line => line.replace(regex, ""))
.join("\n"); .join("\n");
} }
@ -1404,12 +1405,17 @@ export function canUseEmoji(client: Client, emoji: string): boolean {
return false; 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 emptyLinesTrimmed = trimEmptyStartEndLines(str);
const lines = emptyLinesTrimmed.split("\n"); const lines = emptyLinesTrimmed.split("\n");
const firstLineIndentation = (lines[0].match(/^ +/g) || [""])[0].length; const firstLineIndentation = (lines[0].match(/^ +/g) || [""])[0].length;
return trimIndents(emptyLinesTrimmed, firstLineIndentation); return trimIndents(emptyLinesTrimmed, firstLineIndentation);
} }
export const trimPluginDescription = trimMultilineString;
export function isFullMessage(msg: PossiblyUncachedMessage): msg is Message { export function isFullMessage(msg: PossiblyUncachedMessage): msg is Message {
return (msg as Message).createdAt != null; return (msg as Message).createdAt != null;