Skip to content

Commit

Permalink
☁️ Cloudflare: Warn in Discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed May 2, 2024
1 parent fbb9c42 commit 51e19e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
30 changes: 28 additions & 2 deletions src/crons/CloudflareUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { client, core, db } from '@/app'
import { Crons } from '@/classes/Crons'
import axios from 'axios'
import Cloudflare from 'cloudflare'
import { codeBlock, EmbedBuilder, TextChannel } from 'discord.js'
import { existsSync } from 'fs'
import { resolve } from 'path'

Expand Down Expand Up @@ -47,11 +48,19 @@ new Crons({

for (const guild of guilds.values()) {
const { ipString: oldIp } = (await db.cloudflare.get(`${guild.id}.saved`) ?? { ipString: undefined, ipType: undefined }) as Response
await db.cloudflare.set(`${guild.id}.saved`, response.data)

if (oldIp !== undefined && oldIp !== newIp) {
await db.cloudflare.set(`${guild.id}.saved`, response.data)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const { email, global_api_key } = (await db.cloudflare.get(`${guild.id}.keys`) ?? { email: undefined, global_api_key: undefined }) as DataTokens
const channelId = await db.cloudflare.get(`${guild.id}.config.channelId`) as string | undefined
let channel: TextChannel | undefined | null
const records: Record<string, Array<{ name: string, type: string, id: string }>> = {}

if (channelId !== undefined) {
channel = await guild.channels.fetch(channelId) as TextChannel | null
}

core.warn(`Houve uma alteração no ip! ${oldIp} --> ${newIp}`)

if (email === undefined || global_api_key === undefined) return
Expand All @@ -76,11 +85,28 @@ new Crons({
name,
type,
zone_id: zone_id ?? zone.id
}).then(() => {
}).then(async () => {
records[zone.name] = [
...records[zone_id ?? zone.id],
{ name, type, id }
]
core.info(`Alterando o ip do records: ${name}!`)
})
}
}

if (channel instanceof TextChannel) {
const embed = new EmbedBuilder({
title: '🔄 **Mudança de IP**',
description: `A alteração pode levar alguns minutos para entrar em vigor!\n\n⏳ **Horário da Mudança:** <t:${Math.floor(Date.now() / 1000)}:R>\n**IP Antigo:** ${oldIp} ➡️ **IP Novo:** ${newIp} 🌐`
})

for (const [zoneName, values] of Object.entries(records)) {
embed.addFields({ name: zoneName, value: codeBlock(values.map((value) => value.name).join('\n')) })
}

await channel.send({ embeds: [embed] })
}
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/discord/commands/cloudflare/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { db } from '@/app'
import { Command } from '@/discord/base'
import { Discord } from '@/functions'
import { ApplicationCommandOptionType, ApplicationCommandType, EmbedBuilder } from 'discord.js'
import { ApplicationCommandOptionType, ApplicationCommandType, ChannelType, EmbedBuilder } from 'discord.js'

new Command({
name: 'cloudflare',
Expand Down Expand Up @@ -34,6 +34,12 @@ new Command({
description: '[ ☁️ Cloudflare ] Gerencie o sistema de atualização',
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: 'channel-logs',
description: '[ ☁️ Cloudflare ] Logs de alteração de ip ficaram aqui!',
type: ApplicationCommandOptionType.Channel,
channelTypes: [ChannelType.GuildText]
},
{
name: 'old-ip',
description: '[ ☁️ Cloudflare ] Definir o ip antigo, isso permite implementar outros records a atualização!',
Expand Down Expand Up @@ -74,6 +80,10 @@ new Command({
})
break
}
case 'channel-logs': {
const channel = options.getChannel('channel-logs', true)
await db.cloudflare.set(`${guildId}.config.channelId`, channel.id)
}
}
break
}
Expand Down

0 comments on commit 51e19e2

Please sign in to comment.