File size: 3,614 Bytes
bb69f9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const got = require('got')
const Heroku = require('heroku-client')
const {
  secondsToHms,
  isUpdate,
  updateNow,
  bot,
  // genButtonMessage,
} = require('../lib/')
const Config = require('../config')
const heroku = new Heroku({ token: Config.HEROKU_API_KEY })
const baseURI = '/apps/' + Config.HEROKU_APP_NAME

if (Config.HEROKU_API_KEY && Config.HEROKU_APP_NAME) {
  bot(
    {
      pattern: 'restart',
      desc: 'Restart Dyno',
      type: 'heroku',
    },
    async (message, match) => {
      await message.send(`_Restarting_`)
      await heroku.delete(baseURI + '/dynos').catch(async (error) => {
        await message.send(`HEROKU : ${error.body.message}`)
      })
    }
  )

  bot(
    {
      pattern: 'shutdown',
      desc: 'Dyno off',
      type: 'heroku',
    },
    async (message, match) => {
      await heroku
        .get(baseURI + '/formation')
        .then(async (formation) => {
          await message.send(`_Shuttind down._`)
          await heroku.patch(baseURI + '/formation/' + formation[0].id, {
            body: {
              quantity: 0,
            },
          })
        })
        .catch(async (error) => {
          await message.send(`HEROKU : ${error.body.message}`)
        })
    }
  )

  bot(
    {
      pattern: 'dyno',
      desc: 'Show Quota info',
      type: 'heroku',
    },
    async (message, match) => {
      try {
        heroku
          .get('/account')
          .then(async (account) => {
            const url = `https://api.heroku.com/accounts/${account.id}/actions/get-quota`
            headers = {
              'User-Agent': 'Chrome/80.0.3987.149 Mobile Safari/537.36',
              Authorization: 'Bearer ' + Config.HEROKU_API_KEY,
              Accept: 'application/vnd.heroku+json; version=3.account-quotas',
            }
            const res = await got(url, { headers })
            const resp = JSON.parse(res.body)
            const total_quota = Math.floor(resp.account_quota)
            const quota_used = Math.floor(resp.quota_used)
            const remaining = total_quota - quota_used
            const quota = `Total Quota : ${secondsToHms(total_quota)}

Used  Quota : ${secondsToHms(quota_used)}

Remaning    : ${secondsToHms(remaining)}`
            await message.send('```' + quota + '```')
          })
          .catch(async (error) => {
            return await message.send(`HEROKU : ${error.body.message}`)
          })
      } catch (error) {
        await message.send(error)
      }
    }
  )
}

bot(
  {
    pattern: 'update$',
    desc: 'Check new updates.',
    type: 'heroku',
  },
  async (message, match) => {
    const update = await isUpdate()
    if (!update.length) return await message.send('*Bot is up-to-date.*')
    await message.send(`${update.length} updates\n\n${update.join('\n').trim()}`)
    // return await message.send(
    // 	await genButtonMessage(
    // 		[{ id: 'update now', text: 'UPDATE NOW' }],
    // 		`*Updates*\n${update.join('\n').trim()}`,
    // 		`${update.length} updates`
    // 	),
    // 	{},
    // 	'button'
    // )
  }
)

bot(
  {
    pattern: 'update now$',
    desc: 'To-Up-Date bot.',
    type: 'heroku',
  },
  async (message, match) => {
    const isupdate = await isUpdate()
    if (!isupdate.length) return await message.send('*Bot is up-to-date.*\n*Nothing to Update.*')
    await message.send('_Updating..._')
    const e = await updateNow()
    if (e) return await message.send(e)
    return await message.send('_Updated_')
  }
)