Skip to content

Commit

Permalink
feat: store pluralkit users in redis
Browse files Browse the repository at this point in the history
Signed-off-by: Sefa Eyeoglu <[email protected]>
  • Loading branch information
Scrumplex committed Nov 16, 2023
1 parent c29d5df commit e6337cf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
28 changes: 26 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import {
PermissionFlagsBits,
ChannelType,
Events,
Message,
} from 'discord.js';
import { reuploadCommands } from './_reupload';
import {
connect as connectStorage,
isUserPlural,
storeUserPlurality,
} from './storage';

import * as BuildConfig from './constants';
import { parseLog } from './logs';
Expand All @@ -26,7 +32,11 @@ import { sayCommand } from './commands/say';
import random from 'just-random';
import { green, bold, yellow, cyan } from 'kleur/colors';
import 'dotenv/config';
import { proxied } from './utils/pluralKit';
import {
fetchPluralKitMessage,
isMessageProxied,
pkDelay,
} from './utils/pluralKit';

const client = new Client({
intents: [
Expand All @@ -42,6 +52,11 @@ const client = new Client({
partials: [Partials.Channel],
});

const handleWebhookMessage = async (e: Message<boolean>) => {
const pkMessage = await fetchPluralKitMessage(e);
if (pkMessage !== null) storeUserPlurality(pkMessage.sender);
};

client.once('ready', async () => {
console.log(green('Discord bot ready!'));

Expand Down Expand Up @@ -89,7 +104,15 @@ client.once('ready', async () => {

if (e.author === client.user) return;

if (await proxied(e)) return;
if (e.webhookId !== null) {
// defer PK detection
setTimeout(async () => {
await handleWebhookMessage(e);
}, pkDelay);
}

if ((await isUserPlural(e.author.id)) && (await isMessageProxied(e)))
return;

if (e.cleanContent.match(BuildConfig.ETA_REGEX)) {
await e.reply(
Expand Down Expand Up @@ -196,6 +219,7 @@ client.on(Events.ThreadCreate, async (channel) => {

reuploadCommands()
.then(() => {
connectStorage();
client.login(process.env.DISCORD_TOKEN);
})
.catch((e) => {
Expand Down
22 changes: 22 additions & 0 deletions src/storage.ts
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();
};
26 changes: 19 additions & 7 deletions src/utils/pluralKit.ts
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;
}

0 comments on commit e6337cf

Please sign in to comment.