-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create functions for handlers
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { MyContext } from '../interfaces/context' | ||
|
||
export const saldoHandler = async (ctx: MyContext) => { | ||
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}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { NarrowedContext } from 'telegraf' | ||
import { Message, Update } from 'telegraf/typings/core/types/typegram' | ||
import { MyContext } from '../interfaces/context' | ||
|
||
export const textHandler = async ( | ||
ctx: NarrowedContext< | ||
MyContext, | ||
Update.MessageUpdate<Record<'text', unknown> & Message.TextMessage> | ||
>, | ||
) => { | ||
try { | ||
if (ctx.user.credits === 0) { | ||
return ctx.reply( | ||
'Maaf, saldo anda tidak cukup, silakan isi ulang atau tunggu besok hari', | ||
) | ||
} | ||
|
||
const keyword = ctx.message.text | ||
await app.main(ctx, keyword) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { MyContext } from '../interfaces/context' | ||
import { addUser, findUserByID } from '../user.repository' | ||
|
||
export const checkUserMiddleware = async ( | ||
ctx: MyContext, | ||
next: () => Promise<void>, | ||
) => { | ||
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}`) | ||
} | ||
} |