From 51e19e28ab5ead154804f8b48e593f20e828eafe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A7=81Ash=C3=BB=EA=A7=82?= <30575805+Ashu11-A@users.noreply.github.com> Date: Thu, 2 May 2024 17:52:03 -0400 Subject: [PATCH] =?UTF-8?q?=E2=98=81=EF=B8=8F=20Cloudflare:=20Warn=20in=20?= =?UTF-8?q?Discord?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/crons/CloudflareUpdate.ts | 30 +++++++++++++++++++++-- src/discord/commands/cloudflare/config.ts | 12 ++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/crons/CloudflareUpdate.ts b/src/crons/CloudflareUpdate.ts index b5b0d6ac..85c4a586 100644 --- a/src/crons/CloudflareUpdate.ts +++ b/src/crons/CloudflareUpdate.ts @@ -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' @@ -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> = {} + + 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 @@ -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:** \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] }) + } } } } diff --git a/src/discord/commands/cloudflare/config.ts b/src/discord/commands/cloudflare/config.ts index 259dc6ef..58d6fb1b 100644 --- a/src/discord/commands/cloudflare/config.ts +++ b/src/discord/commands/cloudflare/config.ts @@ -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', @@ -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!', @@ -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 }