Disable multiplayer room checking if access token isn't found

This commit is contained in:
Haroon 2024-08-30 16:28:07 +01:00
parent acfb0ae979
commit b0905eca90
Signed by: haroon
GPG key ID: 8B96C44DDF5756E4

View file

@ -5,6 +5,7 @@ import postgres from "postgres";
import "dotenv/config"; import "dotenv/config";
import bcrypt from "bcrypt"; import bcrypt from "bcrypt";
import type { StaticSelectAction } from "@slack/bolt"; import type { StaticSelectAction } from "@slack/bolt";
import { inspect } from "node:util";
const sql = postgres({ const sql = postgres({
host: '/var/run/postgresql', host: '/var/run/postgresql',
@ -161,9 +162,11 @@ async function getTemporaryToken(): Promise<string> {
return data.access_token; return data.access_token;
} }
async function getAccessToken(slack_id: string): Promise<string> { async function getAccessToken(slack_id: string): Promise<string|null> {
const user = await sql`SELECT * FROM links WHERE slack_id = ${slack_id}`; const user = await sql`SELECT * FROM links WHERE slack_id = ${slack_id}`;
if (!user.length) return null
const data = await fetch("https://osu.ppy.sh/oauth/token", { const data = await fetch("https://osu.ppy.sh/oauth/token", {
method: "POST", method: "POST",
headers: { headers: {
@ -259,7 +262,9 @@ async function cacheStuff(): Promise<void> {
multiplayerRoundCache.length = 0; multiplayerRoundCache.length = 0;
const tohken = await getAccessToken("U06TBP41C3E"); const tohken = await getAccessToken("U06TBP41C3E") as string;
if (!tohken) return;
const rooms = await fetch(`https://osu.ppy.sh/api/v2/rooms?category=realtime`, { const rooms = await fetch(`https://osu.ppy.sh/api/v2/rooms?category=realtime`, {
headers: { headers: {
@ -816,7 +821,7 @@ app.command('/osu-eval', async (ctx) => {
if (ctx.context.userId != 'U06TBP41C3E') return; if (ctx.context.userId != 'U06TBP41C3E') return;
const resp = require('util').inspect(await eval(ctx.body.text), undefined, 1) const resp = inspect(await eval(ctx.body.text), undefined, 1)
ctx.respond({ ctx.respond({
text: resp, text: resp,
@ -1093,4 +1098,4 @@ receiver.router.get('*', (req, res) => {
cacheStuff(); cacheStuff();
setTimeout(cacheStuff, 60 * 1000) // Cache every minute. Ratelimit is 1200 req/m anyways. setTimeout(cacheStuff, 60 * 1000) // Cache every minute. Ratelimit is 1200 req/m anyways.
})(); })();