mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-16 14:11:50 +00:00
counters: add !counter view command
This commit is contained in:
parent
78ff2641d4
commit
91fde47c97
4 changed files with 153 additions and 1 deletions
|
@ -477,4 +477,20 @@ export class GuildCounters extends BaseGuildRepository {
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getCurrentValue(
|
||||||
|
counterId: number,
|
||||||
|
channelId: string | null,
|
||||||
|
userId: string | null,
|
||||||
|
): Promise<number | undefined> {
|
||||||
|
const value = await this.counterValues.findOne({
|
||||||
|
where: {
|
||||||
|
counter_id: counterId,
|
||||||
|
channel_id: channelId || "0",
|
||||||
|
user_id: userId || "0",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return value?.value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,14 +14,32 @@ import { initCounterTrigger } from "./functions/initCounterTrigger";
|
||||||
import { decayCounter } from "./functions/decayCounter";
|
import { decayCounter } from "./functions/decayCounter";
|
||||||
import { validateCondition } from "./functions/validateCondition";
|
import { validateCondition } from "./functions/validateCondition";
|
||||||
import { StrictValidationError } from "../../validatorUtils";
|
import { StrictValidationError } from "../../validatorUtils";
|
||||||
|
import { PluginOptions } from "knub";
|
||||||
|
import { ViewCounterCmd } from "./commands/ViewCounterCmd";
|
||||||
|
|
||||||
const MAX_COUNTERS = 5;
|
const MAX_COUNTERS = 5;
|
||||||
const DECAY_APPLY_INTERVAL = 5 * MINUTES;
|
const DECAY_APPLY_INTERVAL = 5 * MINUTES;
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions: PluginOptions<CountersPluginType> = {
|
||||||
config: {
|
config: {
|
||||||
counters: {},
|
counters: {},
|
||||||
|
can_view: false,
|
||||||
|
can_edit: false,
|
||||||
},
|
},
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
level: ">=50",
|
||||||
|
config: {
|
||||||
|
can_view: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
level: ">=100",
|
||||||
|
config: {
|
||||||
|
can_edit: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const configPreprocessor: ConfigPreprocessorFn<CountersPluginType> = options => {
|
const configPreprocessor: ConfigPreprocessorFn<CountersPluginType> = options => {
|
||||||
|
@ -69,6 +87,8 @@ export const CountersPlugin = zeppelinGuildPlugin<CountersPluginType>()("counter
|
||||||
offCounterEvent: mapToPublicFn(offCounterEvent),
|
offCounterEvent: mapToPublicFn(offCounterEvent),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
commands: [ViewCounterCmd],
|
||||||
|
|
||||||
async onLoad(pluginData) {
|
async onLoad(pluginData) {
|
||||||
pluginData.state.counters = new GuildCounters(pluginData.guild.id);
|
pluginData.state.counters = new GuildCounters(pluginData.guild.id);
|
||||||
pluginData.state.events = new EventEmitter();
|
pluginData.state.events = new EventEmitter();
|
||||||
|
|
111
backend/src/plugins/Counters/commands/ViewCounterCmd.ts
Normal file
111
backend/src/plugins/Counters/commands/ViewCounterCmd.ts
Normal 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, User } from "eris";
|
||||||
|
import { resolveUser, UnknownUser } from "../../../utils";
|
||||||
|
|
||||||
|
export const ViewCounterCmd = guildCommand<CountersPluginType>()({
|
||||||
|
trigger: ["counters view", "counter view", "viewcounter"],
|
||||||
|
permission: "can_view",
|
||||||
|
|
||||||
|
signature: [
|
||||||
|
{
|
||||||
|
counterName: ct.string(),
|
||||||
|
user: ct.resolvedUserLoose(),
|
||||||
|
channel: ct.textChannel(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
counterName: ct.string(),
|
||||||
|
user: ct.resolvedUserLoose(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
counterName: ct.string(),
|
||||||
|
channel: ct.textChannel(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
counterName: ct.string(),
|
||||||
|
user: ct.resolvedUserLoose(),
|
||||||
|
channel: ct.textChannel(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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_view === false) {
|
||||||
|
sendErrorMessage(pluginData, message.channel, `Missing permissions to view this counter's values`);
|
||||||
|
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 view?`);
|
||||||
|
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 view?`);
|
||||||
|
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) {
|
||||||
|
sendErrorMessage(pluginData, message.channel, "Unknown user, cancelling");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
user = potentialUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = await pluginData.state.counters.getCurrentValue(counterId, channel?.id ?? null, user?.id ?? null);
|
||||||
|
const finalValue = value ?? counter.initial_value;
|
||||||
|
const counterName = counter.name || args.counterName;
|
||||||
|
|
||||||
|
if (channel && user) {
|
||||||
|
message.channel.createMessage(`${counterName} for <@!${user.id}> in <#${channel.id}> is ${finalValue}`);
|
||||||
|
} else if (channel) {
|
||||||
|
message.channel.createMessage(`${counterName} in <#${channel.id}> is ${finalValue}`);
|
||||||
|
} else if (user) {
|
||||||
|
message.channel.createMessage(`${counterName} for <@!${user.id}> is ${finalValue}`);
|
||||||
|
} else {
|
||||||
|
message.channel.createMessage(`${counterName} is ${finalValue}`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
|
@ -7,6 +7,7 @@ import { CounterTrigger } from "../../data/entities/CounterTrigger";
|
||||||
import Timeout = NodeJS.Timeout;
|
import Timeout = NodeJS.Timeout;
|
||||||
|
|
||||||
export const Counter = t.type({
|
export const Counter = t.type({
|
||||||
|
name: tNullable(t.string),
|
||||||
per_channel: t.boolean,
|
per_channel: t.boolean,
|
||||||
per_user: t.boolean,
|
per_user: t.boolean,
|
||||||
initial_value: t.number,
|
initial_value: t.number,
|
||||||
|
@ -16,11 +17,15 @@ export const Counter = t.type({
|
||||||
every: tDelayString,
|
every: tDelayString,
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
|
can_view: tNullable(t.boolean),
|
||||||
|
can_edit: tNullable(t.boolean),
|
||||||
});
|
});
|
||||||
export type TCounter = t.TypeOf<typeof Counter>;
|
export type TCounter = t.TypeOf<typeof Counter>;
|
||||||
|
|
||||||
export const ConfigSchema = t.type({
|
export const ConfigSchema = t.type({
|
||||||
counters: t.record(t.string, Counter),
|
counters: t.record(t.string, Counter),
|
||||||
|
can_view: t.boolean,
|
||||||
|
can_edit: t.boolean,
|
||||||
});
|
});
|
||||||
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue