feat: 提升优先级匹配小程序 & 增加算法方法

This commit is contained in:
zhiyu1998 2023-04-12 19:08:39 +08:00
parent cc3804d85e
commit c89cf27fa6
2 changed files with 21 additions and 1 deletions

View File

@ -27,7 +27,7 @@ export class tools extends plugin {
name: "R插件工具和学习类", name: "R插件工具和学习类",
dsc: "R插件工具相关指令", dsc: "R插件工具相关指令",
event: "message.group", event: "message.group",
priority: 500, priority: 300,
rule: [ rule: [
{ {
reg: `^(翻|trans)[${tools.Constants.existsTransKey}]`, reg: `^(翻|trans)[${tools.Constants.existsTransKey}]`,

View File

@ -7,6 +7,26 @@ export default class TokenBucket {
this.lastTime = new Date().getTime(); this.lastTime = new Date().getTime();
} }
/**
* 消耗令牌-一个桶
* @param count
* @return {boolean}
*/
consumeSingle(count = 1) {
const now = new Date().getTime();
const elapsed = now - this.lastTime;
const addedTokens = elapsed * (this.rate / 1000 / 60); // 修改为每分钟生成的令牌数量
this.tokens = Math.min(this.tokens + addedTokens, this.capacity);
this.lastTime = now;
if (count <= this.tokens) {
this.tokens -= count;
return true; // 返回 true 表示请求被处理
} else {
return false; // 返回 false 表示请求被限流
}
}
/** /**
* 消耗令牌 * 消耗令牌
* @param id 用户id * @param id 用户id