Skip to content

Commit

Permalink
feat: change cycling activity presence
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelMedeiros committed Jan 27, 2024
1 parent d09bde6 commit 5c88e15
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/domain/bot/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ export class BotService {
const cronjobList = [
{
webhook: `${this.webhookUrl}/new-fees`,
interval: '0 * * * * *',
interval: '*/10 * * * * *',
type: 'fees',
},
{
webhook: `${this.webhookUrl}/new-block`,
interval: '15 * * * * *',
interval: '*/10 * * * * *',
type: 'block',
},
{
webhook: `${this.webhookUrl}/new-price`,
interval: '30 * * * * *',
interval: '*/10 * * * * *',
type: 'price',
},
{
webhook: `${this.webhookUrl}/new-mempool`,
interval: '45 * * * * *',
interval: '*/10 * * * * *',
type: 'mempool',
},
]
Expand Down
82 changes: 60 additions & 22 deletions src/domain/webhooks/webhooks.service.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,74 @@
import { InjectDiscordClient } from '@discord-nestjs/core'
import { Injectable, Logger } from '@nestjs/common'
import { ActivityType, Client, EmbedBuilder } from 'discord.js'
import { ActivityType, Client } from 'discord.js'
import { progressBar } from 'src/utils'
import { createResponse } from 'src/utils/default-response'
import { NumbersService } from 'src/utils/numbers/numbers.service'
import { PriceBodyDto, BlockBodyDto, MessageResponseDto, FeesBodyDto, MempoolBodyDto } from './dto'
import { ConfigService } from '@nestjs/config'
import { setTimeout } from 'timers/promises'

@Injectable()
export class WebhooksService {
private readonly tbdGuildId = this.cfgService.get<string>('TBD_GUILD_ID')

private readonly logger = new Logger(WebhooksService.name)

private currentMempoolInfo: MempoolBodyDto | null = null
private currentFeesInfo: FeesBodyDto | null = null
private currentPriceInfo: PriceBodyDto | null = null
private currentBlockInfo: BlockBodyDto | null = null

constructor(
@InjectDiscordClient()
private readonly client: Client,
private readonly numbersService: NumbersService,
private readonly cfgService: ConfigService,
) {}
) {
this.startUpdateLoop()
}

private async startUpdateLoop() {
await this.updateInformation()
setInterval(async () => {
await this.updateInformation()
}, 60000)
}

private async updateInformation() {
if (this.currentBlockInfo) {
this.client.user.setActivity(`${this.currentBlockInfo.height} block height`, {
type: ActivityType.Watching,
})
await setTimeout(5000)
}

if (this.currentMempoolInfo) {
this.client.user.setActivity(` ${this.currentMempoolInfo.count} txs unconfirmed`, {
type: ActivityType.Watching,
})
await setTimeout(5000)
}

if (this.currentFeesInfo) {
this.client.user.setActivity(`${this.currentFeesInfo.fastestFee} sats/vByte`, {
type: ActivityType.Watching,
})
await setTimeout(5000)
}

if (this.currentPriceInfo) {
const priceChangeUSD = this.currentPriceInfo.usd.priceChangePercent <= 0 ? '▼' : '▲'
const priceChangeBRL = this.currentPriceInfo.brl.priceChangePercent <= 0 ? '▼' : '▲'
const msg = `${priceChangeUSD}$${this.currentPriceInfo.usd.formattedLastPrice} ${priceChangeBRL}R$${this.currentPriceInfo.brl.formattedLastPrice}`
const status = this.currentPriceInfo.usd.priceChangePercent <= 0 ? 'dnd' : 'online'

this.client.user.setStatus(status)
this.client.user.setActivity(msg, {
type: ActivityType.Playing,
})
await setTimeout(45000)
}
}

sendAlertPrices(userId: string, alertPrice: MessageResponseDto) {
try {
Expand Down Expand Up @@ -74,21 +125,18 @@ export class WebhooksService {
async updateNewMempool(mempool: MempoolBodyDto) {
try {
if (mempool && mempool.count) {
this.client.user.setActivity(` ${mempool.count} txs unconfirmed`, {
type: ActivityType.Watching,
})
this.currentMempoolInfo = mempool
this.logger.debug(`NEW WEBHOOK - Mempool Info: ${mempool.count}`)
}
} catch (error) {
this.logger.error(`NEW WEBHOOK - Mempool Info: ${error}`)
}
}

async updateNewBlock(block: BlockBodyDto) {
try {
if (block && block.height) {
this.client.user.setActivity(`${block.height} block height`, {
type: ActivityType.Watching,
})
this.currentBlockInfo = block
this.logger.debug(`NEW WEBHOOK - Block: ${block.height}`)
}
} catch (error) {
Expand All @@ -99,9 +147,7 @@ export class WebhooksService {
async updateNewFees(fees: FeesBodyDto) {
try {
if (fees.fastestFee) {
this.client.user.setActivity(`${fees.fastestFee} sats/vByte`, {
type: ActivityType.Watching,
})
this.currentFeesInfo = fees
this.logger.debug(`NEW WEBHOOK - New Fees: ${fees.fastestFee} sats/vByte`)
}
} catch (error) {
Expand All @@ -111,16 +157,8 @@ export class WebhooksService {

async updateNewPrice(tickers: PriceBodyDto) {
try {
const priceChangeUSD = tickers.usd.priceChangePercent <= 0 ? '▼' : '▲'
const priceChangeBRL = tickers.brl.priceChangePercent <= 0 ? '▼' : '▲'
const msg = `${priceChangeUSD}$${tickers.usd.formattedLastPrice} ${priceChangeBRL}R$${tickers.brl.formattedLastPrice}`
const status = tickers.usd.priceChangePercent <= 0 ? 'dnd' : 'online'

this.client.user.setStatus(status)
this.client.user.setActivity(msg, {
type: ActivityType.Playing,
})
this.logger.debug(`NEW WEBHOOK - New Price: ${msg}`)
this.currentPriceInfo = tickers
this.logger.debug(`NEW WEBHOOK - New Price: ${tickers.usd.formattedLastPrice}`)
} catch (error) {
this.logger.error(`NEW WEBHOOK - New Price: ${error}`)
}
Expand Down

0 comments on commit 5c88e15

Please sign in to comment.