@@ -107,6 +107,7 @@ export async function storeRedditJobPosts(posts: Array<any>) {
107107// Checks Reddit inbox for new messages and stores them
108108export async function checkRedditMessages ( ) {
109109 try {
110+ const redditClient = getRedditClient ( )
110111 const messages = await redditClient . getInbox ( { filter : 'messages' } )
111112 return await storeMessages ( messages )
112113 } catch ( error : any ) {
@@ -118,14 +119,30 @@ import prisma from '@/server/db'
118119import { logger } from '@/server/logger'
119120import { upsertJob } from './jobs'
120121
121- // Initialize Reddit API client with environment variables
122- export const redditClient = new Snoowrap ( {
123- userAgent : 'CodeBuilder by /u/taofullstack' ,
124- clientId : process . env . REDDIT_CLIENT_ID ,
125- clientSecret : process . env . REDDIT_CLIENT_SECRET ,
126- username : process . env . REDDIT_USERNAME ,
127- password : process . env . REDDIT_PASSWORD ,
128- } )
122+ function getRedditCredentials ( ) {
123+ const clientId = process . env . REDDIT_CLIENT_ID
124+ const clientSecret = process . env . REDDIT_CLIENT_SECRET
125+ const username = process . env . REDDIT_USERNAME
126+ const password = process . env . REDDIT_PASSWORD
127+
128+ if ( ! clientId || ! clientSecret || ! username || ! password ) {
129+ throw new Error (
130+ 'Reddit API credentials are not configured. Set REDDIT_CLIENT_ID, REDDIT_CLIENT_SECRET, REDDIT_USERNAME, and REDDIT_PASSWORD.'
131+ )
132+ }
133+
134+ return {
135+ userAgent : 'CodeBuilder by /u/taofullstack' ,
136+ clientId,
137+ clientSecret,
138+ username,
139+ password,
140+ }
141+ }
142+
143+ export function getRedditClient ( ) {
144+ return new Snoowrap ( getRedditCredentials ( ) )
145+ }
129146
130147/**
131148 * Type guard to determine if a Snoowrap item is a PrivateMessage.
@@ -215,6 +232,7 @@ export function startRedditCommentStream(
215232 onComment : ( comment : Snoowrap . Comment ) => void ,
216233 options : Partial < SnooStormOptions > = { }
217234) {
235+ const redditClient = getRedditClient ( )
218236 const stream = new CommentStream ( redditClient , {
219237 subreddit,
220238 limit : options . limit || 10 ,
0 commit comments