Skip to content

Commit 340f177

Browse files
authored
Merge pull request #8 from johnnyFR26/discord
[fix: discord controller]
2 parents 38542a4 + 79d0f67 commit 340f177

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

controllers/discord.controller.ts

+30-20
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,55 @@ import { client } from "../lib/bot";
33
import { TextChannel, NewsChannel, Channel } from "discord.js";
44

55
interface GitHubWebhookPayload {
6-
action?: string
6+
action?: string;
77
pull_request?: {
88
title: string;
9-
html_url: string
9+
html_url: string;
1010
user: {
11-
login: string
11+
login: string;
1212
};
1313
};
1414
}
1515

16-
export async function handleGitHubWebhook(
16+
async function handleGitHubWebhook(
1717
request: FastifyRequest<{ Body: GitHubWebhookPayload }>,
1818
reply: FastifyReply
1919
) {
2020
const payload = request.body;
2121

2222
if (payload?.action === "opened" && payload.pull_request) {
2323
try {
24-
const channelId = "1354221727823171624";
24+
const channelId = process.env.CHANNEL_ID;
25+
if (!channelId) throw new Error("⚠️ CHANNEL_ID não definido no .env");
2526

26-
if (!channelId) {
27-
console.error("⚠️ CHANNEL_ID não está definido no .env");
28-
return reply.status(500).send({ error: "Configuração inválida" })
29-
}
27+
console.log(`🔍 Buscando canal com ID: ${channelId}`);
3028

31-
const channel: Channel | null = await client.channels.fetch(channelId)
29+
// Verifica se o bot está pronto antes de buscar o canal
30+
if (!client.isReady()) {
31+
throw new Error("🤖 O bot ainda não está pronto para receber comandos.");
32+
}
3233

33-
if (channel instanceof TextChannel) {
34-
const pr = payload.pull_request;
35-
await channel.send(
36-
`🔔 Novo Pull Request aberto! **${pr.title}**\n🔗 ${pr.html_url}\n👤 Autor: ${pr.user.login}`
37-
);
38-
} else {
39-
console.error("⚠️ O canal do Discord não é um canal de texto ou não foi encontrado.")
34+
const channel: Channel | null = await client.channels.fetch(channelId);
35+
if (!channel || !(channel instanceof TextChannel || channel instanceof NewsChannel)) {
36+
throw new Error(`❌ Canal ${channelId} não encontrado ou não é de texto.`);
4037
}
38+
39+
console.log(`✅ Canal encontrado: ${channel.name} (${channel.id})`);
40+
41+
const pr = payload.pull_request;
42+
await channel.send(
43+
`🔔 Novo Pull Request aberto! **${pr.title}**\n🔗 ${pr.html_url}\n👤 Autor: ${pr.user.login}`
44+
).catch(console.error);
45+
46+
console.log(`✅ Mensagem enviada para ${channel.name}`);
47+
4148
} catch (error) {
42-
console.error("❌ Erro ao enviar mensagem para o Discord:", error);
43-
return reply.status(500).send({ error: "Erro ao processar webhook" })
49+
console.error("❌ Erro ao processar webhook:", error);
50+
return reply.status(500).send({ error: error.message });
4451
}
4552
}
46-
reply.send({ success: true })
53+
54+
reply.send({ success: true });
4755
}
56+
57+
export { handleGitHubWebhook };

0 commit comments

Comments
 (0)