mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-17 18:11:23 -06:00
Add redis ^& discord env vars, basic test functions
This commit is contained in:
14
src/db.ts
Normal file
14
src/db.ts
Normal 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
3
src/env/schema.mjs
vendored
@@ -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
3
src/env/server.mjs
vendored
@@ -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
26
src/notify.ts
Normal 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}`
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user