-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new and improve anti-spam manager (#167)
feat: Integrate AntiSpamManager in spam check logic fix: Handle error message properly in analyzeImageForNSFW函数 refactor: Remove unused imports and update sendBlacklistNotif usage
- Loading branch information
Showing
7 changed files
with
121 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import getRedis from '#main/utils/Redis.js'; | ||
import { Message } from 'discord.js'; | ||
import type { Redis } from 'ioredis'; | ||
|
||
export default class AntiSpamManager { | ||
private config: SpamConfig; | ||
private redis: Redis; | ||
|
||
constructor(config: SpamConfig, redis = getRedis()) { | ||
this.redis = redis; | ||
this.config = config; | ||
} | ||
|
||
public async handleMessage(message: Message): Promise<UserMessageInfo | undefined> { | ||
const userId = message.author.id; | ||
const currentTime = Date.now(); | ||
const key = `spam:${userId}`; | ||
|
||
const userInfo = await this.getUserInfo(key); | ||
|
||
if (currentTime - userInfo.lastMessage < this.config.timeWindow) { | ||
userInfo.messageCount++; | ||
if (userInfo.messageCount >= this.config.spamThreshold) { | ||
userInfo.lastMessage = currentTime; | ||
await this.incrementSpamCount(message.author.id); | ||
await this.setUserInfo(key, userInfo); | ||
return userInfo; | ||
} | ||
} | ||
else { | ||
userInfo.messageCount = 1; | ||
} | ||
|
||
userInfo.lastMessage = currentTime; | ||
await this.setUserInfo(key, userInfo); | ||
} | ||
|
||
private async getUserInfo(key: string): Promise<UserMessageInfo> { | ||
const data = await this.redis.hgetall(key); | ||
return { | ||
messageCount: parseInt(data.messageCount || '0', 10), | ||
lastMessage: parseInt(data.lastMessage || '0', 10), | ||
}; | ||
} | ||
|
||
private async setUserInfo(key: string, info: UserMessageInfo): Promise<void> { | ||
await this.redis.hmset(key, { | ||
messageCount: info.messageCount.toString(), | ||
lastMessage: info.lastMessage.toString(), | ||
}); | ||
await this.redis.expire(key, this.config.timeWindow / 1000); | ||
} | ||
|
||
private async incrementSpamCount(userId: string): Promise<void> { | ||
const key = `spamcount:${userId}`; | ||
await this.redis.incr(key); | ||
await this.redis.expire(key, this.config.spamCountExpirySecs); | ||
} | ||
|
||
public async getSpamCount(userId: string): Promise<number> { | ||
const key = `spamcount:${userId}`; | ||
const count = await this.redis.get(key); | ||
return parseInt(count || '0', 10); | ||
} | ||
} | ||
|
||
interface UserMessageInfo { | ||
messageCount: number; | ||
lastMessage: number; | ||
} | ||
|
||
interface SpamConfig { | ||
spamThreshold: number; | ||
timeWindow: number; | ||
spamCountExpirySecs: number; | ||
} |
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 was deleted.
Oops, something went wrong.
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