-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: store pluralkit users in redis
Signed-off-by: Sefa Eyeoglu <[email protected]>
- Loading branch information
Showing
3 changed files
with
67 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createClient } from 'redis'; | ||
|
||
export const client = createClient({ | ||
url: process.env.REDIS_URL || 'redis://localhost:6379', | ||
}); | ||
|
||
export const storeUserPlurality = async (userId: string) => { | ||
// Just store some value. We only care about the presence of this key | ||
await client | ||
.multi() | ||
.set(`user:${userId}:pk`, '0') | ||
.expire(`user:${userId}:pk`, 7 * 24 * 60 * 60) | ||
.exec(); | ||
}; | ||
|
||
export const isUserPlural = async (userId: string) => { | ||
return (await client.exists(`user:${userId}:pk`)) > 0; | ||
}; | ||
|
||
export const connect = () => { | ||
client.connect(); | ||
}; |
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 |
---|---|---|
@@ -1,10 +1,22 @@ | ||
import { Message } from "discord.js"; | ||
import { Message } from 'discord.js'; | ||
|
||
export async function proxied(message: Message): Promise<boolean> { | ||
if (message.webhookId !== null) | ||
return false; | ||
interface PkMessage { | ||
sender: string; | ||
} | ||
|
||
export const pkDelay = 500; | ||
|
||
export async function fetchPluralKitMessage(message: Message) { | ||
const response = await fetch( | ||
`https://api.pluralkit.me/v2/messages/${message.id}` | ||
); | ||
|
||
if (!response.ok) return null; | ||
|
||
return (await response.json()) as PkMessage; | ||
} | ||
|
||
await new Promise(resolve => setTimeout(resolve, 300)); | ||
const response = await fetch(`https://api.pluralkit.me/v2/messages/${message.id}`); | ||
return response.ok; | ||
export async function isMessageProxied(message: Message) { | ||
await new Promise((resolve) => setTimeout(resolve, pkDelay)); | ||
return (await fetchPluralKitMessage(message)) !== null; | ||
} |