Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feature/contributor…
Browse files Browse the repository at this point in the history
…-roles
  • Loading branch information
TheKodeToad committed Nov 18, 2023
2 parents 9286a05 + 1c7e263 commit 9bb5b4a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 15 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
${config.pre-commit.installationScript}
'';
nativeBuildInputs = [config.proc.groups.daemons.package];
packages = with pkgs; [nodePackages.pnpm];
packages = with pkgs; [nodePackages.pnpm redis];
};
formatter = pkgs.alejandra;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"just-random": "3.2.0",
"kleur": "4.1.5",
"redis": "4.6.10",
"tsx": "3.13.0"
"tsx": "4.1.1"
},
"devDependencies": {
"@types/node": "20.9.0",
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

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

29 changes: 23 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ import {

import config from './config';

import reupload from './_reupload';
import { reuploadCommands } from './_reupload';
import { listen as listenApp } from './server';
import { connect as connectStorage } from './storage';
import {
connect as connectStorage,
isUserPlural,
storeUserPlurality,
} from './storage';
import { scheduleJobs } from './tasks';

import * as BuildConfig from './constants';
Expand All @@ -34,9 +38,13 @@ import { sayCommand } from './commands/say';
import random from 'just-random';
import { green, bold, yellow, cyan } from 'kleur/colors';

import 'dotenv/config';
import {
fetchPluralKitMessage,
isMessageProxied,
pkDelay,
} from './utils/pluralKit';

import { proxied } from './utils/pluralKit';
import 'dotenv/config';

const client = new Client({
intents: [
Expand Down Expand Up @@ -99,7 +107,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 @@ -204,7 +221,7 @@ client.on(Events.ThreadCreate, async (channel) => {
}
});

reupload()
reuploadCommands()
.then(() => {
client.login(config.discord.botToken);
connectStorage();
Expand Down
13 changes: 13 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ export const areContributors = async (
return (await client.smIsMember(key, contributorIds)).includes(true);
};

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 = 1000;

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 9bb5b4a

Please sign in to comment.