Add redis ^& discord env vars, basic test functions

This commit is contained in:
Xevion
2023-02-25 02:38:42 -06:00
parent de18ce26f2
commit 3d98f1c1ce
6 changed files with 257 additions and 3 deletions

14
src/db.ts Normal file
View File

@@ -0,0 +1,14 @@
import { Redis } from 'ioredis';
import { env } from '@/env/server.mjs';
import { format } from 'date-fns';
console.log(env.REDIS_URL);
const redis = new Redis(env.REDIS_URL, { maxRetriesPerRequest: 2 });
export async function test() {
const now = new Date();
const key = format(now, 'yyyy-MM-dd');
const current = await redis.incr(key);
return current;
}

3
src/env/schema.mjs vendored
View File

@@ -16,5 +16,6 @@ export const serverSchema = z.object({
CENTER_LATITUDE: z.coerce.number().min(-90).max(90),
CENTER_LONGITUDE: z.coerce.number().min(-180).max(180),
EDGE_CACHE_TIME_SECONDS: z.coerce.number().int().nonnegative().default(60),
REDIS_URL: z.string().url()
REDIS_URL: z.string().url(),
DISCORD_TOKEN: z.string()
});

3
src/env/server.mjs vendored
View File

@@ -17,7 +17,8 @@ const _serverEnv = serverSchema.safeParse({
CENTER_LATITUDE: process.env.CENTER_LATITUDE,
CENTER_LONGITUDE: process.env.CENTER_LONGITUDE,
EDGE_CACHE_TIME_SECONDS: process.env.EDGE_CACHE_TIME_SECONDS,
REDIS_URL: process.env.REDIS_URL
REDIS_URL: process.env.REDIS_URL,
DISCORD_TOKEN: process.env.DISCORD_TOKEN
});
if (_serverEnv.success === false) {

26
src/notify.ts Normal file
View File

@@ -0,0 +1,26 @@
import { Client, Events, GatewayIntentBits } from 'discord.js';
import { env } from '@/env/server.mjs';
export async function sendNotification(message: string): Promise<void> {
const client = new Client({
intents: []
});
try {
await client.login(env.DISCORD_TOKEN);
} catch (e) {
throw new Error(
`Failed while logging in to Discord: ${
e instanceof Error ? e.message : e
}`
);
}
try {
await client.users.send('184118083143598081', message);
} catch (e) {
throw new Error(
`Failed while sending message: ${e instanceof Error ? e.message : e}`
);
}
}