From 31d7748bf4ba9ec71634ec902ddd7032f78302c2 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Mon, 3 May 2021 18:37:02 +0300 Subject: [PATCH] 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. --- backend/src/data/GuildCounters.ts | 10 ++++++++-- .../plugins/Counters/functions/changeCounterValue.ts | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/backend/src/data/GuildCounters.ts b/backend/src/data/GuildCounters.ts index d3c2389a..8963afcb 100644 --- a/backend/src/data/GuildCounters.ts +++ b/backend/src/data/GuildCounters.ts @@ -120,7 +120,13 @@ export class GuildCounters extends BaseGuildRepository { .execute(); } - async changeCounterValue(id: number, channelId: string | null, userId: string | null, change: number): Promise { + async changeCounterValue( + id: number, + channelId: string | null, + userId: string | null, + change: number, + initialValue: number, + ): Promise { if (typeof change !== "number" || Number.isNaN(change) || !Number.isFinite(change)) { throw new Error(`changeCounterValue() change argument must be a number`); } @@ -137,7 +143,7 @@ export class GuildCounters extends BaseGuildRepository { VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE ${rawUpdate} `, - [id, channelId, userId, Math.max(change, 0)], + [id, channelId, userId, Math.max(initialValue + change, 0)], ); } diff --git a/backend/src/plugins/Counters/functions/changeCounterValue.ts b/backend/src/plugins/Counters/functions/changeCounterValue.ts index d5da6825..de61d9be 100644 --- a/backend/src/plugins/Counters/functions/changeCounterValue.ts +++ b/backend/src/plugins/Counters/functions/changeCounterValue.ts @@ -31,7 +31,7 @@ export async function changeCounterValue( const counterId = pluginData.state.counterIds[counterName]; 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 const triggers = pluginData.state.counterTriggersByCounterId.get(counterId);