3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-18 23:09:59 +00:00

Fix changeCounterValue() not considering initial value

If a counter was initialized via the INSERT in changeCounterValue(),
its value would be set to the change value rather than the specified
initial value. For example, a counter with an initial value of 1000
and a change of 5 would get initialized to 5, not 1005.
This commit is contained in:
Dragory 2021-05-03 18:37:02 +03:00
parent 49577e37ca
commit 31d7748bf4
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
2 changed files with 9 additions and 3 deletions

View file

@ -120,7 +120,13 @@ export class GuildCounters extends BaseGuildRepository {
.execute(); .execute();
} }
async changeCounterValue(id: number, channelId: string | null, userId: string | null, change: number): Promise<void> { async changeCounterValue(
id: number,
channelId: string | null,
userId: string | null,
change: number,
initialValue: number,
): Promise<void> {
if (typeof change !== "number" || Number.isNaN(change) || !Number.isFinite(change)) { if (typeof change !== "number" || Number.isNaN(change) || !Number.isFinite(change)) {
throw new Error(`changeCounterValue() change argument must be a number`); throw new Error(`changeCounterValue() change argument must be a number`);
} }
@ -137,7 +143,7 @@ export class GuildCounters extends BaseGuildRepository {
VALUES (?, ?, ?, ?) VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE ${rawUpdate} ON DUPLICATE KEY UPDATE ${rawUpdate}
`, `,
[id, channelId, userId, Math.max(change, 0)], [id, channelId, userId, Math.max(initialValue + change, 0)],
); );
} }

View file

@ -31,7 +31,7 @@ export async function changeCounterValue(
const counterId = pluginData.state.counterIds[counterName]; const counterId = pluginData.state.counterIds[counterName];
const lock = await pluginData.locks.acquire(counterIdLock(counterId)); const lock = await pluginData.locks.acquire(counterIdLock(counterId));
await pluginData.state.counters.changeCounterValue(counterId, channelId, userId, change); await pluginData.state.counters.changeCounterValue(counterId, channelId, userId, change, counter.initial_value);
// Check for trigger matches, if any, when the counter value changes // Check for trigger matches, if any, when the counter value changes
const triggers = pluginData.state.counterTriggersByCounterId.get(counterId); const triggers = pluginData.state.counterTriggersByCounterId.get(counterId);