-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement daily limit for chatbot and stablediffusion
- Loading branch information
1 parent
4cad98e
commit 8bccc46
Showing
4 changed files
with
78 additions
and
7 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
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,36 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const commandUsageSchema = new mongoose.Schema({ | ||
memberId: { type: String, required: true }, | ||
commandName: { type: String, required: true }, | ||
usageCount: { type: Number, default: 0 }, | ||
createdAt: { type: Date, default: Date.now, expires: 24 * 60 * 60 }, // Set expiration time to 24 hours | ||
}); | ||
|
||
const CommandUsageModel = mongoose.model("CommandUsage", commandUsageSchema); | ||
|
||
async function getAndIncrementUsageCount(memberId, limit, commandName) { | ||
let commandUsage = await CommandUsageModel.findOne({ | ||
memberId: memberId, | ||
commandName: commandName, | ||
}); | ||
|
||
if (commandUsage?.usageCount >= limit) { | ||
throw new Error( | ||
`You have reached the usage limit for this command. [${limit} uses per day]` | ||
); | ||
} | ||
|
||
commandUsage = await CommandUsageModel.findOneAndUpdate( | ||
{ memberId: memberId, commandName: commandName }, | ||
{ $inc: { usageCount: 1 }, lastUsageTimestamp: new Date() }, | ||
{ upsert: true, new: true } | ||
); | ||
|
||
return commandUsage.usageCount; | ||
} | ||
|
||
module.exports = { | ||
CommandUsageModel, | ||
getAndIncrementUsageCount, | ||
}; |