Skip to content

Commit f65ed28

Browse files
committed
Switch error notification to use webhook
1 parent bbac8b3 commit f65ed28

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/services/wikiUpdates.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { injectable, singleton } from 'tsyringe'
22
import { ChannelTypes, wikiActivities } from './types/activityResult.js'
33
import NodeCache from 'node-cache'
44
import { gql, request } from 'graphql-request'
5-
import { EmbedBuilder, TextChannel } from 'discord.js'
5+
import { EmbedBuilder, WebhookClient } from 'discord.js'
66
import { client } from '../main.js'
77

88
interface ApiResponse {
@@ -21,6 +21,8 @@ export default class WikiUpdates {
2121
DEV_CHANNEL_ID: string
2222
PROD_CHANNEL_ID: string
2323
REVALIDATE_SECRET: string
24+
DEV_WIKI_WEBHOOK: string
25+
PROD_ALARMS_WEBHOOK: string
2426

2527
private apiHealthStatus = new Map<
2628
ChannelTypes,
@@ -34,6 +36,8 @@ export default class WikiUpdates {
3436
this.DEV_CHANNEL_ID = this.CHANNEL_IDS.DEV.WIKI
3537
this.PROD_CHANNEL_ID = this.CHANNEL_IDS.PROD.WIKI
3638
this.REVALIDATE_SECRET = process.env.REVALIDATE_SECRET
39+
this.DEV_WIKI_WEBHOOK = process.env.DEV_WIKI_WEBHOOK as string
40+
this.PROD_ALARMS_WEBHOOK = process.env.PROD_ALARMS_WEBHOOK as string
3741
}
3842

3943
getUnixtime(time: string): number {
@@ -93,20 +97,20 @@ export default class WikiUpdates {
9397
errorCode: string,
9498
) {
9599
try {
96-
const channelId =
100+
const webhookUrl =
97101
channelType === ChannelTypes.DEV
98-
? this.CHANNEL_IDS.DEV.WIKI
99-
: this.CHANNEL_IDS.PROD.ALARMS
102+
? this.DEV_WIKI_WEBHOOK
103+
: this.DEV_WIKI_WEBHOOK
100104

101-
const channel = client.channels.cache.get(channelId) as TextChannel
102-
103-
if (!channel) {
105+
if (!webhookUrl) {
104106
console.error(
105-
`❌ Discord channel not found for ${channelType}: ${channelId}`,
107+
`❌ Webhook URL not configured for ${channelType}`,
106108
)
107109
return
108110
}
109111

112+
const webhook = new WebhookClient({ url: webhookUrl })
113+
110114
const shouldNotify =
111115
errorCode === 'HEALTH_CHECK_FAILED' || count % notifyCount === 0
112116

@@ -121,14 +125,15 @@ export default class WikiUpdates {
121125

122126
const messageContent = `${mentionText}`
123127

124-
await channel.send({
128+
await webhook.send({
125129
content: messageContent,
126130
embeds: [
127131
await this.messageApiErrorStyle(link, errorCode, channelType),
128132
],
129133
})
130134

131135
console.log('✅ Message sent successfully')
136+
webhook.destroy()
132137
}
133138
} catch (error) {
134139
console.error(`❌ Failed to send error notification:`, error)
@@ -341,15 +346,16 @@ export default class WikiUpdates {
341346

342347
if (previousStatus && !previousStatus.isHealthy) {
343348
console.log(`🎉 API ${channelType} has recovered!`)
344-
const channelId =
349+
const webhookUrl =
345350
channelType === ChannelTypes.DEV
346-
? this.CHANNEL_IDS.DEV.WIKI
347-
: this.CHANNEL_IDS.PROD.ALARMS
348-
const channel = client.channels.cache.get(channelId) as TextChannel
349-
if (channel) {
350-
await channel.send(
351+
? this.DEV_WIKI_WEBHOOK
352+
: this.PROD_ALARMS_WEBHOOK
353+
if (webhookUrl) {
354+
const webhook = new WebhookClient({ url: webhookUrl })
355+
await webhook.send(
351356
`✅ **RECOVERY** - ${channelType} API is back online! 🎉`,
352357
)
358+
webhook.destroy()
353359
}
354360
}
355361
}

0 commit comments

Comments
 (0)