3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 04:25:01 +00:00

Counters v0.9

Includes automod trigger/action. No user-facing commands yet.
This commit is contained in:
Dragory 2021-02-13 17:29:10 +02:00
parent ec37cf27a2
commit c3407e2d5d
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
29 changed files with 1387 additions and 3 deletions

View file

@ -0,0 +1,45 @@
import { GuildPluginData } from "knub";
import { CountersPluginType } from "../types";
import { checkCounterTrigger } from "./checkCounterTrigger";
import { checkReverseCounterTrigger } from "./checkReverseCounterTrigger";
export async function setCounterValue(
pluginData: GuildPluginData<CountersPluginType>,
counterName: string,
channelId: string | null,
userId: string | null,
value: number,
) {
const config = pluginData.config.get();
const counter = config.counters[counterName];
if (!counter) {
throw new Error(`Unknown counter: ${counterName}`);
}
if (counter.per_channel && !channelId) {
throw new Error(`Counter is per channel but no channel ID was supplied`);
}
if (counter.per_user && !userId) {
throw new Error(`Counter is per user but no user ID was supplied`);
}
const counterId = pluginData.state.counterIds[counterName];
const lock = await pluginData.locks.acquire(counterId.toString());
await pluginData.state.counters.setCounterValue(counterId, channelId, userId, value);
// Check for trigger matches, if any, when the counter value changes
const triggers = pluginData.state.counterTriggersByCounterId.get(counterId);
if (triggers) {
const triggersArr = Array.from(triggers.values());
await Promise.all(
triggersArr.map(trigger => checkCounterTrigger(pluginData, counterName, trigger, channelId, userId)),
);
await Promise.all(
triggersArr.map(trigger => checkReverseCounterTrigger(pluginData, counterName, trigger, channelId, userId)),
);
}
lock.unlock();
}