Skip to content

Commit

Permalink
Merge pull request #297 from Scrumplex/feat/detect-pk
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrumplex authored Nov 17, 2023
2 parents 686f315 + 889ef88 commit d05e1cb
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 11 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
shellHook = ''
${config.pre-commit.installationScript}
'';
packages = with pkgs; [nodePackages.pnpm];
packages = with pkgs; [nodePackages.pnpm redis];
};
formatter = pkgs.alejandra;
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"discord.js": "14.14.1",
"just-random": "3.2.0",
"kleur": "4.1.5",
"redis": "4.6.10",
"tsx": "4.1.1"
},
"devDependencies": {
Expand Down
74 changes: 73 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import {
Events,
} 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 +31,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 Down Expand Up @@ -89,7 +98,16 @@ client.once('ready', async () => {

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

if (await proxied(e)) return;
if (e.webhookId !== null) {
// defer PK detection
setTimeout(async () => {
const pkMessage = await fetchPluralKitMessage(e);
if (pkMessage !== null) storeUserPlurality(pkMessage.sender);
}, 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 +214,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 d05e1cb

Please sign in to comment.