counters: add '!counters add' and '!counters set' commands
This commit is contained in:
parent
a18b093419
commit
0f14f75020
3 changed files with 289 additions and 1 deletions
|
@ -16,6 +16,8 @@ import { validateCondition } from "./functions/validateCondition";
|
|||
import { StrictValidationError } from "../../validatorUtils";
|
||||
import { PluginOptions } from "knub";
|
||||
import { ViewCounterCmd } from "./commands/ViewCounterCmd";
|
||||
import { AddCounterCmd } from "./commands/AddCounterCmd";
|
||||
import { SetCounterCmd } from "./commands/SetCounterCmd";
|
||||
|
||||
const MAX_COUNTERS = 5;
|
||||
const DECAY_APPLY_INTERVAL = 5 * MINUTES;
|
||||
|
@ -87,7 +89,12 @@ export const CountersPlugin = zeppelinGuildPlugin<CountersPluginType>()("counter
|
|||
offCounterEvent: mapToPublicFn(offCounterEvent),
|
||||
},
|
||||
|
||||
commands: [ViewCounterCmd],
|
||||
// prettier-ignore
|
||||
commands: [
|
||||
ViewCounterCmd,
|
||||
AddCounterCmd,
|
||||
SetCounterCmd,
|
||||
],
|
||||
|
||||
async onLoad(pluginData) {
|
||||
pluginData.state.counters = new GuildCounters(pluginData.guild.id);
|
||||
|
|
141
backend/src/plugins/Counters/commands/AddCounterCmd.ts
Normal file
141
backend/src/plugins/Counters/commands/AddCounterCmd.ts
Normal file
|
@ -0,0 +1,141 @@
|
|||
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, User } from "eris";
|
||||
import { resolveUser, UnknownUser } from "../../../utils";
|
||||
import { changeCounterValue } from "../functions/changeCounterValue";
|
||||
|
||||
export const AddCounterCmd = guildCommand<CountersPluginType>()({
|
||||
trigger: ["counters add", "counter add", "addcounter"],
|
||||
permission: "can_edit",
|
||||
|
||||
signature: [
|
||||
{
|
||||
counterName: ct.string(),
|
||||
amount: ct.number(),
|
||||
},
|
||||
{
|
||||
counterName: ct.string(),
|
||||
user: ct.resolvedUser(),
|
||||
amount: ct.number(),
|
||||
},
|
||||
{
|
||||
counterName: ct.string(),
|
||||
channel: ct.textChannel(),
|
||||
amount: ct.number(),
|
||||
},
|
||||
{
|
||||
counterName: ct.string(),
|
||||
channel: ct.textChannel(),
|
||||
user: ct.resolvedUser(),
|
||||
amount: ct.number(),
|
||||
},
|
||||
{
|
||||
counterName: ct.string(),
|
||||
user: ct.resolvedUser(),
|
||||
channel: ct.textChannel(),
|
||||
amount: ct.number(),
|
||||
},
|
||||
],
|
||||
|
||||
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 edit 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 add to?`);
|
||||
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 add to?`);
|
||||
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;
|
||||
}
|
||||
|
||||
let amount = args.amount;
|
||||
if (!amount) {
|
||||
message.channel.createMessage("How much would you like to add to the counter's value?");
|
||||
const reply = await waitForReply(pluginData.client, message.channel, message.author.id);
|
||||
if (!reply || !reply.content) {
|
||||
sendErrorMessage(pluginData, message.channel, "Cancelling");
|
||||
return;
|
||||
}
|
||||
|
||||
const potentialAmount = parseInt(reply.content, 10);
|
||||
if (!potentialAmount) {
|
||||
sendErrorMessage(pluginData, message.channel, "Not a number, cancelling");
|
||||
return;
|
||||
}
|
||||
|
||||
amount = potentialAmount;
|
||||
}
|
||||
|
||||
await changeCounterValue(pluginData, args.counterName, channel?.id ?? null, user?.id ?? null, amount);
|
||||
const newValue = await pluginData.state.counters.getCurrentValue(counterId, channel?.id ?? null, user?.id ?? null);
|
||||
const counterName = counter.name || args.counterName;
|
||||
|
||||
if (channel && user) {
|
||||
message.channel.createMessage(
|
||||
`Added ${amount} to **${counterName}** for <@!${user.id}> in <#${channel.id}>. The value is now ${newValue}.`,
|
||||
);
|
||||
} else if (channel) {
|
||||
message.channel.createMessage(
|
||||
`Added ${amount} to **${counterName}** in <#${channel.id}>. The value is now ${newValue}.`,
|
||||
);
|
||||
} else if (user) {
|
||||
message.channel.createMessage(
|
||||
`Added ${amount} to **${counterName}** for <@!${user.id}>. The value is now ${newValue}.`,
|
||||
);
|
||||
} else {
|
||||
message.channel.createMessage(`Added ${amount} to **${counterName}**. The value is now ${newValue}.`);
|
||||
}
|
||||
},
|
||||
});
|
140
backend/src/plugins/Counters/commands/SetCounterCmd.ts
Normal file
140
backend/src/plugins/Counters/commands/SetCounterCmd.ts
Normal file
|
@ -0,0 +1,140 @@
|
|||
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, User } from "eris";
|
||||
import { resolveUser, UnknownUser } from "../../../utils";
|
||||
import { changeCounterValue } from "../functions/changeCounterValue";
|
||||
import { setCounterValue } from "../functions/setCounterValue";
|
||||
|
||||
export const SetCounterCmd = guildCommand<CountersPluginType>()({
|
||||
trigger: ["counters set", "counter set", "setcounter"],
|
||||
permission: "can_edit",
|
||||
|
||||
signature: [
|
||||
{
|
||||
counterName: ct.string(),
|
||||
value: ct.number(),
|
||||
},
|
||||
{
|
||||
counterName: ct.string(),
|
||||
user: ct.resolvedUser(),
|
||||
value: ct.number(),
|
||||
},
|
||||
{
|
||||
counterName: ct.string(),
|
||||
channel: ct.textChannel(),
|
||||
value: ct.number(),
|
||||
},
|
||||
{
|
||||
counterName: ct.string(),
|
||||
channel: ct.textChannel(),
|
||||
user: ct.resolvedUser(),
|
||||
value: ct.number(),
|
||||
},
|
||||
{
|
||||
counterName: ct.string(),
|
||||
user: ct.resolvedUser(),
|
||||
channel: ct.textChannel(),
|
||||
value: ct.number(),
|
||||
},
|
||||
],
|
||||
|
||||
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 edit 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 add to?`);
|
||||
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 add to?`);
|
||||
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;
|
||||
}
|
||||
|
||||
let value = args.value;
|
||||
if (!value) {
|
||||
message.channel.createMessage("How much would you like to add to the counter's value?");
|
||||
const reply = await waitForReply(pluginData.client, message.channel, message.author.id);
|
||||
if (!reply || !reply.content) {
|
||||
sendErrorMessage(pluginData, message.channel, "Cancelling");
|
||||
return;
|
||||
}
|
||||
|
||||
const potentialValue = parseInt(reply.content, 10);
|
||||
if (!potentialValue) {
|
||||
sendErrorMessage(pluginData, message.channel, "Not a number, cancelling");
|
||||
return;
|
||||
}
|
||||
|
||||
value = potentialValue;
|
||||
}
|
||||
|
||||
if (value < 0) {
|
||||
sendErrorMessage(pluginData, message.channel, "Cannot set counter value below 0");
|
||||
return;
|
||||
}
|
||||
|
||||
await setCounterValue(pluginData, args.counterName, channel?.id ?? null, user?.id ?? null, value);
|
||||
const counterName = counter.name || args.counterName;
|
||||
|
||||
if (channel && user) {
|
||||
message.channel.createMessage(`Set **${counterName}** for <@!${user.id}> in <#${channel.id}> to ${value}`);
|
||||
} else if (channel) {
|
||||
message.channel.createMessage(`Set **${counterName}** in <#${channel.id}> to ${value}`);
|
||||
} else if (user) {
|
||||
message.channel.createMessage(`Set **${counterName}** for <@!${user.id}> to ${value}`);
|
||||
} else {
|
||||
message.channel.createMessage(`Set **${counterName}** to ${value}`);
|
||||
}
|
||||
},
|
||||
});
|
Loading…
Add table
Reference in a new issue