@@ -3,45 +3,55 @@ import { client } from "../lib/bot";
3
3
import { TextChannel , NewsChannel , Channel } from "discord.js" ;
4
4
5
5
interface GitHubWebhookPayload {
6
- action ?: string
6
+ action ?: string ;
7
7
pull_request ?: {
8
8
title : string ;
9
- html_url : string
9
+ html_url : string ;
10
10
user : {
11
- login : string
11
+ login : string ;
12
12
} ;
13
13
} ;
14
14
}
15
15
16
- export async function handleGitHubWebhook (
16
+ async function handleGitHubWebhook (
17
17
request : FastifyRequest < { Body : GitHubWebhookPayload } > ,
18
18
reply : FastifyReply
19
19
) {
20
20
const payload = request . body ;
21
21
22
22
if ( payload ?. action === "opened" && payload . pull_request ) {
23
23
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" ) ;
25
26
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 } ` ) ;
30
28
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
+ }
32
33
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.` ) ;
40
37
}
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
+
41
48
} 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 } ) ;
44
51
}
45
52
}
46
- reply . send ( { success : true } )
53
+
54
+ reply . send ( { success : true } ) ;
47
55
}
56
+
57
+ export { handleGitHubWebhook } ;
0 commit comments