UNPKG

1.31 kBPlain TextView Raw
1import { Server } from '../types/fastify'
2import fs from 'fs-extra'
3import FileConfig from '../config/FileConfig'
4import path from 'path'
5
6export default async (app: Server) => {
7 app.get('/logs', async (request, reply) => {
8 if (!(config instanceof FileConfig)) return reply.send([])
9 const count = request.query.count || 100
10
11 const logsFolderPath = path.join(path.parse(config.path).dir, 'logs')
12 const logs = await fs.readdir(logsFolderPath)
13 const file = await fs.readFile(path.join(logsFolderPath, logs.slice(-1)[0]), 'utf8')
14 const lines = file.split('\n')
15
16 return lines.slice(-count)
17 })
18
19 app.get('/status', (request, reply) => {
20 reply.send({ status: config.paused ? 'paused' : 'running' })
21 })
22
23 app.post('/pause', (request, reply) => {
24 config.paused = true
25 reply.send({ status: 'paused' })
26 })
27
28 app.post('/unpause', (request, reply) => {
29 config.paused = false
30 reply.send({ status: 'running' })
31 })
32
33 app.post('/restart', async (request, reply) => {
34 reply.send({})
35 await close(2)
36 })
37
38 app.post('/quit', async (request, reply) => {
39 reply.send({})
40 await close(0)
41 })
42}
43
44async function close (code: number) {
45 await discord.client.destroy()
46 logger.success('Logged out from Discord')
47 console.error('close ' + code.toString())
48}