-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
!requests command to show how many requests to the bingAI has been ma…
…de (#94) * docs: Update README.md * feat: !requests command
- Loading branch information
Showing
6 changed files
with
93 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ puppeteer/ | |
|
||
.env | ||
|
||
conversations_cache.sqlite | ||
conversations_cache.sqlite | ||
counter_cache.sqlite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import KeyvSqlite from "@keyv/sqlite"; | ||
import Keyv from "keyv"; | ||
|
||
// Part of the logic to count the requests used | ||
const counterCache = new Keyv({ | ||
store: new KeyvSqlite({ uri: "sqlite://./counter_cache.sqlite" }), | ||
}); | ||
|
||
export let messageCounter = 0; | ||
let lastResetDate = new Date(); | ||
|
||
export async function loadCounterData() { | ||
try { | ||
const counterData = await counterCache.get("counterData"); | ||
console.log("counterData:", counterData); | ||
if (counterData) { | ||
messageCounter = counterData.counter; | ||
lastResetDate = new Date(counterData.lastReset); | ||
} | ||
} catch (error) { | ||
console.log("Failed to load counter data:", error); | ||
} | ||
} | ||
|
||
export async function saveCounterData() { | ||
try { | ||
await counterCache.set("counterData", { | ||
counter: messageCounter, | ||
lastReset: lastResetDate.toISOString(), | ||
}); | ||
} catch (error) { | ||
console.log("Failed to save counter data:", error); | ||
} | ||
} | ||
|
||
export function resetCounter() { | ||
messageCounter = 0; | ||
lastResetDate = new Date(); | ||
saveCounterData(); | ||
} | ||
|
||
export function has24HoursPassed() { | ||
const currentDate = new Date(); | ||
const timeDiff = currentDate.getTime() - lastResetDate.getTime(); | ||
const hoursPassed = timeDiff / (1000 * 3600); | ||
return hoursPassed >= 24; | ||
} | ||
|
||
export function increaseCounter() { | ||
messageCounter++; | ||
} | ||
|
||
export async function counterRequests() { | ||
// Load counter data from the cache | ||
await loadCounterData(); | ||
|
||
// Check if 24 hours have passed since the last reset | ||
if (has24HoursPassed()) { | ||
resetCounter(); | ||
} | ||
|
||
// Increment the counter | ||
increaseCounter(); | ||
|
||
// Rest of the code... | ||
|
||
// Save counter data after each handled message | ||
await saveCounterData(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters