create-gramstax 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,7 +35,7 @@
35
35
  "lint:fix": "eslint . --fix"
36
36
  },
37
37
  "dependencies": {
38
- "gramstax": "^0.6.1",
38
+ "gramstax": "^0.7.1",
39
39
  "dayjs": "^1.11.13"
40
40
  },
41
41
  "peerDependencies": {
@@ -1,5 +1,5 @@
1
- export const env = {
2
- TELEGRAM_BOT_TOKEN: process.env.TELEGRAM_BOT_TOKEN as string,
3
- TELEGRAM_BOT_DEPLOY: process.env.TELEGRAM_BOT_DEPLOY as string,
4
- TELEGRAM_CACHE_SESSION: process.env.TELEGRAM_CACHE_SESSION as string
1
+ export class Env {
2
+ static TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN as string
3
+ static TELEGRAM_BOT_DEPLOY = process.env.TELEGRAM_BOT_DEPLOY as string
4
+ static TELEGRAM_CACHE_SESSION = process.env.TELEGRAM_CACHE_SESSION as string
5
5
  }
@@ -1,2 +1,2 @@
1
1
  import {log} from "gramstax"
2
- export {log}
2
+ export {log as LogHe}
@@ -5,7 +5,7 @@ import utc from "dayjs/plugin/utc"
5
5
  dayjs.extend(utc)
6
6
  dayjs.extend(tz)
7
7
 
8
- export class TimeUt {
8
+ export class TimeHe {
9
9
  static template = `YYYY-MM-DD HH:mm:ss`
10
10
 
11
11
  /**
@@ -1,20 +1,22 @@
1
- import {env} from "./env"
2
- import {log} from "./utils/log"
1
+ import {Env} from "./env"
2
+ import {LogHe} from "./helpers/log"
3
3
  import {BotCore} from "./core/bot"
4
4
  import {CacheExternal} from "gramstax"
5
5
 
6
- log.info(`Bot started..`)
6
+ class Index {
7
+ static async run() {
8
+ LogHe.info(`Bot started..`)
7
9
 
8
- const run = () => {
9
- new BotCore({
10
- log: true,
11
- token: env.TELEGRAM_BOT_TOKEN,
12
- deploy: env.TELEGRAM_BOT_DEPLOY,
13
- cacheSession: new CacheExternal(env.TELEGRAM_CACHE_SESSION, 4 * 60 * 60 * 1000, `s`), // 4 hours
14
- optionsPage: {
15
- shortCallbackData: true
16
- }
17
- })
10
+ new BotCore({
11
+ log: LogHe,
12
+ token: Env.TELEGRAM_BOT_TOKEN,
13
+ deploy: Env.TELEGRAM_BOT_DEPLOY,
14
+ cacheSession: new CacheExternal(Env.TELEGRAM_CACHE_SESSION, 4 * 60 * 60 * 1000, `s`), // 4 hours
15
+ optionsPage: {
16
+ shortCallbackData: true
17
+ }
18
+ })
19
+ }
18
20
  }
19
21
 
20
- run()
22
+ Index.run()
@@ -0,0 +1,9 @@
1
+ import {PageBase} from "~/base/page"
2
+
3
+ export class BlankPage extends PageBase {
4
+ async handleCallbackData() {
5
+ await this.callbackQueryAnswer()
6
+ }
7
+
8
+ static template = `<base></base>`
9
+ }
@@ -1,4 +1,4 @@
1
- import {log} from "~/utils/log"
1
+ import {LogHe} from "~/helpers/log"
2
2
  import {PageBase} from "~/base/page"
3
3
  import {StartPage} from "./start"
4
4
 
@@ -15,7 +15,7 @@ export class GeneralStatusPage extends PageBase {
15
15
  }
16
16
 
17
17
  async toError(funcName: string, error: any, edit = true) {
18
- log.errorMake(error, this.constructorName, funcName)
18
+ LogHe.errorMake(error, this.constructorName, funcName)
19
19
 
20
20
  await this.clearSession()
21
21
  const kb = this.kb()
@@ -15,9 +15,9 @@ export class StartPage extends PageBase {
15
15
  async show(edit = true) {
16
16
  await this.clearSession()
17
17
  const kb = this.inlineKeyboardT([RandomNumberPage, SumNumberPage, HelpPage], `2`)
18
- const data = {userName: this.userName}
19
- if (edit) await this.edit(kb, data)
20
- else await this.reply(kb, data)
18
+ const da = {userName: this.userName}
19
+ if (edit) await this.edit(kb, da)
20
+ else await this.reply(kb, da)
21
21
  }
22
22
 
23
23
  static template = `
@@ -22,17 +22,23 @@ export class SumNumberPage extends PageBase {
22
22
  if (step === `left`) {
23
23
  if (!Number.isFinite(value)) {
24
24
  await this.sessionFreeText({step: `left`})
25
- return await this.reply(this.kb(BASE.pleft), {}, BASE.pleft)
25
+ const bi = BASE.pleft
26
+ const kb = this.kb(bi)
27
+ return await this.reply(kb, {}, bi)
26
28
  }
27
29
 
28
30
  await this.sessionFreeText({step: `right`, left: value})
29
- return await this.reply(this.kb(BASE.pright), {}, BASE.pright)
31
+ const bi = BASE.pright
32
+ const kb = this.kb(bi)
33
+ return await this.reply(kb, {}, bi)
30
34
  }
31
35
 
32
36
  if (step === `right`) {
33
37
  if (!Number.isFinite(value)) {
34
38
  await this.sessionFreeText({step: `right`, left})
35
- return await this.reply(this.kb(BASE.pright), {}, BASE.pright)
39
+ const bi = BASE.pright
40
+ const kb = this.kb(bi)
41
+ return await this.reply(kb, {}, bi)
36
42
  }
37
43
 
38
44
  if (!Number.isFinite(left ?? NaN)) {
@@ -40,19 +46,22 @@ export class SumNumberPage extends PageBase {
40
46
  return await this.first(false)
41
47
  }
42
48
 
43
- const result = left! + value
44
49
  await this.clearSession()
45
- return await this.reply(this.kb(BASE.result), {result}, BASE.result)
50
+ const bi = BASE.result
51
+ const kb = this.kb(bi)
52
+ const da = {result: left! + value}
53
+ await this.reply(kb, da, bi)
46
54
  }
47
55
  }
48
56
 
49
57
  private async first(edit: boolean) {
50
58
  await this.sessionFreeText({step: `left`})
51
59
  const {BASE} = SumNumberPage
52
- const kb = this.kb(BASE.pleft)
60
+ const bi = BASE.pleft
61
+ const kb = this.kb(bi)
53
62
 
54
- if (edit) await this.edit(kb, {}, BASE.pleft)
55
- else await this.reply(kb, {}, BASE.pleft)
63
+ if (edit) await this.edit(kb, {}, bi)
64
+ else await this.reply(kb, {}, bi)
56
65
  }
57
66
 
58
67
  private kb(baseId: string) {
@@ -0,0 +1,24 @@
1
+ export class ArrayUt {
2
+ /**
3
+ * 1. Mengambil satu elemen secara acak
4
+ */
5
+ static random(array: any[]) {
6
+ if (!Array.isArray(array) || array.length === 0) return undefined
7
+ const randomIndex = Math.floor(Math.random() * array.length)
8
+ return array[randomIndex]
9
+ }
10
+
11
+ /**
12
+ * 2. Mengacak urutan array (Fisher-Yates Shuffle)
13
+ * Menghasilkan array baru agar tidak merubah array asli (immutable)
14
+ */
15
+ static shuffle(array: any[]) {
16
+ if (!Array.isArray(array)) return []
17
+ const newArray = [...array] // Clone array
18
+ for (let i = newArray.length - 1; i > 0; i--) {
19
+ const j = Math.floor(Math.random() * (i + 1))
20
+ ;[newArray[i], newArray[j]] = [newArray[j], newArray[i]]
21
+ }
22
+ return newArray
23
+ }
24
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-gramstax",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public",