-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe61c2a
commit b281fe6
Showing
7 changed files
with
629 additions
and
8 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
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,25 @@ | ||
import { Controller, Get, Logger } from '@nestjs/common'; | ||
import { ApiOperation, ApiTags } from '@nestjs/swagger'; | ||
|
||
import { StatusService } from './status.service'; | ||
|
||
@ApiTags('Main - Bot Status API') | ||
@Controller('www/status') | ||
export class StatusController { | ||
private readonly logger = new Logger(StatusController.name); | ||
|
||
constructor(private readonly statusService: StatusService) {} | ||
|
||
@Get() | ||
@ApiOperation({ | ||
summary: 'Bot Status', | ||
description: | ||
'한디리 (https://koreanbots.dev)에서 봇의 상태(서버수, 하트수)를 가져옵니다.', | ||
}) | ||
async getStatus(): Promise<{ | ||
votes?: number; | ||
servers?: number; | ||
}> { | ||
return await this.statusService.getStatus(); | ||
} | ||
} |
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,54 @@ | ||
import { Inject, Injectable, Logger } from '@nestjs/common'; | ||
import { CACHE_MANAGER } from '@nestjs/cache-manager'; | ||
|
||
import { Cache } from 'cache-manager'; | ||
import { Bot, Koreanbots } from 'koreanbots'; | ||
|
||
@Injectable() | ||
export class StatusService { | ||
private readonly logger = new Logger(StatusService.name); | ||
|
||
private readonly koreanbotsClient = new Koreanbots({ | ||
clientID: process.env.DISCORD_CLIENT_ID, | ||
api: { | ||
token: process.env.KOREANBOTS_TOKEN, | ||
}, | ||
}); | ||
|
||
constructor(@Inject(CACHE_MANAGER) private readonly cacheManager: Cache) {} | ||
|
||
async getStatus(): Promise<{ | ||
votes?: number; | ||
servers?: number; | ||
}> { | ||
let koreanbots: Bot; | ||
|
||
const redisCachedKoreanbots = | ||
await this.cacheManager.get<Bot>('koreanbots'); | ||
|
||
if (redisCachedKoreanbots) { | ||
koreanbots = redisCachedKoreanbots; | ||
} else { | ||
const locallyCachedKoreanbots = this.koreanbotsClient.bots.cache.get( | ||
process.env.DISCORD_CLIENT_ID, | ||
); | ||
|
||
if (locallyCachedKoreanbots) { | ||
koreanbots = locallyCachedKoreanbots; | ||
} else { | ||
koreanbots = await this.koreanbotsClient.bots.fetch( | ||
process.env.DISCORD_CLIENT_ID, | ||
); | ||
|
||
await this.cacheManager.set('koreanbots', koreanbots, 600000); | ||
} | ||
} | ||
|
||
this.logger.debug(koreanbots); | ||
|
||
return { | ||
servers: koreanbots.servers, | ||
votes: koreanbots.votes, | ||
}; | ||
} | ||
} |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { StatusController } from './status/status.controller'; | ||
import { StatusService } from './status/status.service'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [StatusController], | ||
providers: [StatusService], | ||
}) | ||
export class WwwModule {} |
Oops, something went wrong.