Skip to content

Commit b6331dc

Browse files
committed
Switch to webhooks and skip revalidations for dev
1 parent 6590eb2 commit b6331dc

3 files changed

Lines changed: 95 additions & 80 deletions

File tree

src/events/common.ts

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TextChannel } from 'discord.js'
1+
import { WebhookClient } from 'discord.js'
22
import type { ArgsOf } from 'discordx'
33
import { Discord, On } from 'discordx'
44
import { injectable } from 'tsyringe'
@@ -31,19 +31,31 @@ interface ScheduledJobConfig {
3131
@Discord()
3232
@injectable()
3333
export class AppDiscord {
34-
PROD_ALARMS: string
3534
PROD_URL: string
3635
DEV_URL: string
36+
DEV_API_URL: string
3737
private scheduledJobs: schedule.Job[] = []
38+
private webhooks: {
39+
devWiki: string
40+
devHiiq: string
41+
prodWiki: string
42+
prodAlarms: string
43+
}
3844

3945
constructor(
4046
private updates: Updates,
4147
private revalidate: RevalidateService,
4248
private wikiUpdates: WikiUpdates,
4349
) {
44-
this.PROD_ALARMS = JSON.parse(process.env.CHANNELS).ALARMS
4550
this.PROD_URL = process.env.PROD_URL
4651
this.DEV_URL = process.env.DEV_URL
52+
this.DEV_API_URL = process.env.DEV_API_URL
53+
this.webhooks = {
54+
devWiki: process.env.DEV_WIKI_WEBHOOK || '',
55+
devHiiq: process.env.DEV_HIIQ_WEBHOOK || '',
56+
prodWiki: process.env.PROD_WIKI_WEBHOOK || '',
57+
prodAlarms: process.env.PROD_ALARMS_WEBHOOK || '',
58+
}
4759
}
4860

4961
private async executeWithErrorHandling(
@@ -59,12 +71,7 @@ export class AppDiscord {
5971
}
6072
}
6173

62-
private createScheduledJobs(channels: any): ScheduledJobConfig[] {
63-
const devWikiChannel = channels.devWikiChannel
64-
const devHiiqChannel = channels.devHiiqChannel
65-
const prodWikiChannel = channels.prodWikiChannel
66-
const prodAlertChannel = channels.prodAlertChannel
67-
74+
private createScheduledJobs(): ScheduledJobConfig[] {
6875
return [
6976
{
7077
name: 'Wiki Updates Check',
@@ -73,13 +80,13 @@ export class AppDiscord {
7380
task: async () => {
7481
await Promise.all([
7582
this.updates.sendUpdates({
76-
channelId: devWikiChannel,
83+
webhookUrl: this.webhooks.devWiki,
7784
channelType: ChannelTypes.DEV,
7885
url: this.DEV_URL,
7986
updateType: UpdateTypes.WIKI,
8087
}),
8188
this.updates.sendUpdates({
82-
channelId: prodWikiChannel,
89+
webhookUrl: this.webhooks.prodWiki,
8390
channelType: ChannelTypes.PROD,
8491
url: this.PROD_URL,
8592
updateType: UpdateTypes.WIKI,
@@ -93,7 +100,7 @@ export class AppDiscord {
93100
enabled: true,
94101
task: async () => {
95102
await this.updates.sendUpdates({
96-
channelId: devHiiqChannel,
103+
webhookUrl: this.webhooks.devHiiq,
97104
channelType: ChannelTypes.DEV,
98105
url: '',
99106
updateType: UpdateTypes.HIIQ,
@@ -110,10 +117,10 @@ export class AppDiscord {
110117
this.PROD_URL,
111118
`${process.cwd()}/build/utils/prodWikiLinks.js`,
112119
),
113-
this.revalidate.revalidateRandomWiki(
114-
this.DEV_URL,
115-
`${process.cwd()}/build/utils/devWikiLinks.js`,
116-
),
120+
// this.revalidate.revalidateRandomWiki(
121+
// this.DEV_URL,
122+
// `${process.cwd()}/build/utils/devWikiLinks.js`,
123+
// ),
117124
])
118125
},
119126
},
@@ -127,9 +134,9 @@ export class AppDiscord {
127134
...pages.map(page =>
128135
this.revalidate.revalidateWikiPage(this.PROD_URL, page),
129136
),
130-
...pages.map(page =>
131-
this.revalidate.revalidateWikiPage(this.DEV_URL, page),
132-
),
137+
// ...pages.map(page =>
138+
// this.revalidate.revalidateWikiPage(this.DEV_URL, page),
139+
// ),
133140
])
134141
},
135142
},
@@ -138,7 +145,7 @@ export class AppDiscord {
138145
schedule: '*/30 * * * *',
139146
enabled: true,
140147
task: async () => {
141-
await this.checkWebpageStatus(urls, prodAlertChannel)
148+
await this.checkWebpageStatus(urls, this.webhooks.prodAlarms)
142149
},
143150
},
144151
{
@@ -156,26 +163,12 @@ export class AppDiscord {
156163
async isReady([client]: ArgsOf<'clientReady'>) {
157164
console.log('🤖 Bot is ready! Setting up scheduled tasks...')
158165

159-
const channelIds = JSON.parse(process.env.CHANNELS)
160-
161-
const channels = {
162-
devWikiChannel: client.channels.cache.get(
163-
channelIds.DEV.WIKI,
164-
) as TextChannel,
165-
devHiiqChannel: client.channels.cache.get(
166-
channelIds.DEV.HIIQ,
167-
) as TextChannel,
168-
prodWikiChannel: client.channels.cache.get(
169-
channelIds.PROD.WIKI,
170-
) as TextChannel,
171-
prodAlertChannel: client.channels.cache.get(
172-
channelIds.PROD.ALARMS,
173-
) as TextChannel,
174-
}
175-
176-
Object.entries(channels).forEach(([name, channel]) => {
177-
if (!channel) {
178-
console.error(`❌ Channel ${name} not found!`)
166+
// Validate webhook URLs
167+
Object.entries(this.webhooks).forEach(([name, url]) => {
168+
if (!url) {
169+
console.warn(`⚠️ Webhook ${name} not configured`)
170+
} else {
171+
console.log(`✅ Webhook ${name} configured`)
179172
}
180173
})
181174

@@ -187,7 +180,7 @@ export class AppDiscord {
187180
this.wikiUpdates.startApiHealthMonitoring(),
188181
)
189182

190-
const jobConfigs = this.createScheduledJobs(channels)
183+
const jobConfigs = this.createScheduledJobs()
191184

192185
jobConfigs.forEach(config => {
193186
if (config.enabled) {
@@ -213,7 +206,7 @@ export class AppDiscord {
213206

214207
const [extractedProdLinks, extractedDevLinks] = await Promise.all([
215208
this.revalidate.extractLinks(this.PROD_URL),
216-
this.revalidate.extractLinks(this.DEV_URL),
209+
this.revalidate.extractLinks(this.DEV_API_URL),
217210
])
218211

219212
await Promise.all([
@@ -236,7 +229,7 @@ export class AppDiscord {
236229

237230
async checkWebpageStatus(
238231
urls: string[],
239-
channel: TextChannel,
232+
webhookUrl: string,
240233
): Promise<void> {
241234
console.log(`🔍 Checking status of ${urls.length} websites...`)
242235

@@ -283,7 +276,7 @@ export class AppDiscord {
283276
if (result) {
284277
console.log(` - ${result.url}: ${result.status}`)
285278
await this.updates.sendUpdates({
286-
channelId: channel,
279+
webhookUrl: webhookUrl,
287280
channelType: ChannelTypes.PROD,
288281
url: result.url,
289282
updateType: UpdateTypes.DOWNTIME,

src/services/revalidate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export default class RevalidateService {
1818
let url = this.url(path)
1919
let revalidateUrl: string
2020

21+
if (url.includes('dev')) return
22+
2123
if (id === '/' || id === '/activity') {
2224
return
2325
} else {

src/utils/sendUpdates.ts

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
ChannelTypes,
44
UpdateTypes,
55
} from './../services/types/activityResult.js'
6-
import { EmbedBuilder, TextChannel } from 'discord.js'
6+
import { EmbedBuilder, WebhookClient } from 'discord.js'
77
import { singleton } from 'tsyringe'
88
import WikiUpdates from '../services/wikiUpdates.js'
99
import { HiiqAlarm, ScanResult } from '../services/hiiqAlarm.js'
@@ -13,7 +13,7 @@ import WikiUpdatesTweeter from '../services/tweetUpdates.js'
1313
import RevalidateService from '../services/revalidate.js'
1414

1515
interface MessageUpdates {
16-
channelId: TextChannel
16+
webhookUrl: string
1717
channelType: ChannelTypes
1818
url: string
1919
updateType: UpdateTypes
@@ -42,14 +42,12 @@ export default class Updates {
4242
.setImage(`${this.META_URL}${content[0].images[0].id}`)
4343
.setTimestamp()
4444
.setFooter({
45-
text: `${wiki.type.toLowerCase()} by ${
46-
wiki.user.profile?.username ? wiki.user.profile.username : 'user'
47-
} `,
48-
iconURL: `${this.META_URL}${
49-
wiki.user.profile?.avatar
45+
text: `${wiki.type.toLowerCase()} by ${wiki.user.profile?.username ? wiki.user.profile.username : 'user'
46+
} `,
47+
iconURL: `${this.META_URL}${wiki.user.profile?.avatar
5048
? wiki.user.profile.avatar
5149
: 'QmXqCRoaA61P3KamAd8UgGYyrcdb5Fu2REL6jrcVBSawwE'
52-
}`,
50+
}`,
5351
})
5452
return wikiEmbed
5553
}
@@ -107,40 +105,62 @@ export default class Updates {
107105
await this.twitter.tweetWikiActivity(wikiActivity, messageUpdates.url)
108106
}
109107

108+
private getWebhookClient(webhookUrl: string): WebhookClient | null {
109+
if (!webhookUrl) {
110+
return null
111+
}
112+
try {
113+
return new WebhookClient({ url: webhookUrl })
114+
} catch (error) {
115+
console.error('❌ Invalid webhook URL:', error)
116+
return null
117+
}
118+
}
119+
110120
async sendUpdates(messageUpdates: MessageUpdates) {
111-
if (messageUpdates.updateType === UpdateTypes.WIKI) {
112-
const time = await this.wikiUpdates.getTime(messageUpdates.channelType)
113-
const response = await this.wikiUpdates.query(
114-
time,
115-
messageUpdates.channelType,
116-
)
117-
response.forEach(async (activity: wikiActivities) => {
118-
messageUpdates.channelId.send({
119-
embeds: [await this.messageWikiStyle(activity, messageUpdates.url)],
120-
})
121-
await this.checkAndTweet(messageUpdates, activity)
122-
await this.revalidate.revalidateWikiPage(
123-
messageUpdates.url,
124-
activity.wikiId,
125-
)
126-
})
121+
const webhook = this.getWebhookClient(messageUpdates.webhookUrl)
122+
if (!webhook) {
123+
console.error(`❌ Webhook not found for ${messageUpdates.channelType} - ${messageUpdates.updateType}`)
124+
return
127125
}
128126

129-
if (messageUpdates.updateType === UpdateTypes.HIIQ) {
130-
const response = await this.hiiqAlarm.checkHiiq()
131-
response.forEach(async (e: ScanResult) => {
132-
if (e.balance.alarm) {
133-
messageUpdates.channelId.send({
134-
embeds: [await this.messageHiiqStyle(e)],
127+
try {
128+
if (messageUpdates.updateType === UpdateTypes.WIKI) {
129+
const time = await this.wikiUpdates.getTime(messageUpdates.channelType)
130+
const response = await this.wikiUpdates.query(
131+
time,
132+
messageUpdates.channelType,
133+
)
134+
for (const activity of response) {
135+
await webhook.send({
136+
embeds: [await this.messageWikiStyle(activity, messageUpdates.url)],
135137
})
138+
await this.checkAndTweet(messageUpdates, activity)
139+
await this.revalidate.revalidateWikiPage(
140+
messageUpdates.url,
141+
activity.wikiId,
142+
)
136143
}
137-
})
138-
}
144+
}
139145

140-
if (messageUpdates.updateType === UpdateTypes.DOWNTIME) {
141-
messageUpdates.channelId.send({
142-
embeds: [await this.messageDowntimeStyle(messageUpdates.url)],
143-
})
146+
if (messageUpdates.updateType === UpdateTypes.HIIQ) {
147+
const response = await this.hiiqAlarm.checkHiiq()
148+
for (const e of response) {
149+
if (e.balance.alarm) {
150+
await webhook.send({
151+
embeds: [await this.messageHiiqStyle(e)],
152+
})
153+
}
154+
}
155+
}
156+
157+
if (messageUpdates.updateType === UpdateTypes.DOWNTIME) {
158+
await webhook.send({
159+
embeds: [await this.messageDowntimeStyle(messageUpdates.url)],
160+
})
161+
}
162+
} finally {
163+
webhook.destroy()
144164
}
145165
}
146166
}

0 commit comments

Comments
 (0)