3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +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

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