Skip to content

Commit

Permalink
feat: add cron to reset free credits
Browse files Browse the repository at this point in the history
  • Loading branch information
tfkhdyt committed Sep 27, 2023
1 parent 9e84016 commit c60e846
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 42 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@libsql/client": "^0.3.5",
"cheerio": "1.0.0-rc.12",
"cron": "^2.4.4",
"dotenv": "^16.3.1",
"drizzle-orm": "^0.28.6",
"postgres": "^3.3.5",
Expand Down
14 changes: 14 additions & 0 deletions src/cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CronJob } from 'cron'
import { resetFreeCredits } from './user.repository'

export function startCron() {
const job = new CronJob(
'0 0 * * *',
resetFreeCredits,
null,
true,
'Asia/Jakarta',
)

job.start()
}
50 changes: 9 additions & 41 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,28 @@
import { migrate } from 'drizzle-orm/libsql/migrator'

import { message } from 'telegraf/filters'

import Bot from './Bot.js'
import config from './config/config.js'
import { db } from './db/postgres/index.js'
import { addUser, findUserByID } from './user.repository.js'
import { startCron } from './cron.js'
import { startMigration } from './db/postgres/index.js'
import { saldoHandler } from './handlers/saldoHandler.js'
import { checkUserMiddleware } from './middlewares/user.middleware.js'

console.log('Running migration...')
await migrate(db, { migrationsFolder: 'drizzle' })
console.log('Migrations is done')
startMigration()
startCron()

const app = new Bot(config.botToken)
const bot = app.bot

bot.use(async (ctx, next) => {
try {
const userId = ctx.from?.id
const username = ctx.from?.username
if (!userId || !username)
throw new Error('User ID atau Username anda tidak valid')

const user = await findUserByID(userId)
if (user.length === 0) {
await addUser({ id: userId, username })
}

ctx.user = user[0]

await next()
} catch (error) {
console.error(error)
ctx.reply(`Terjadi error yang tak terduga!, ${error}`)
}
})

bot.use(checkUserMiddleware)
bot.start((ctx) => app.sendStartMessage(ctx))

bot.help((ctx) => app.sendHelpMessage(ctx))

bot.command('saldo', async (ctx) => {
try {
const user = ctx.user

ctx.reply(`Jumlah saldo mu adalah: ${user.credits}`)
} catch (error) {
console.error(error)
ctx.reply(`Terjadi error yang tak terduga!, ${error}`)
}
})
bot.command('saldo', saldoHandler)

bot.on(message('text'), async (ctx) => {
try {
if (ctx.user.credits === 0) {
return ctx.reply(
'Maaf, saldo anda tidak cukup, silakan isi ulang atau tunggu besok hari',
'Maaf, saldo anda tidak mencukupi, silakan isi ulang atau tunggu esok hari. Saldo gratis harian akan di-reset setiap jam 00:00',
)
}

Expand Down
5 changes: 4 additions & 1 deletion src/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { eq, sql } from 'drizzle-orm'
import { eq, lt, sql } from 'drizzle-orm'
import { db } from './db/postgres'
import { NewUser, users } from './db/postgres/schemas/user.schema'

Expand All @@ -12,3 +12,6 @@ export const decreaseCredits = (userId: number) =>
.update(users)
.set({ credits: sql`${users.credits} - 1` })
.where(eq(users.id, userId))

export const resetFreeCredits = () =>
db.update(users).set({ credits: 3 }).where(lt(users.credits, 3))

0 comments on commit c60e846

Please sign in to comment.