@@ -19,6 +19,7 @@ interface WebhookEntry {
1919
2020interface WebhookData {
2121 webhooks : WebhookEntry [ ] ;
22+ nextHeraldNumber : number ;
2223}
2324
2425export class WebhookManager {
@@ -35,9 +36,14 @@ export class WebhookManager {
3536 private static loadWebhookData ( ) : void {
3637 try {
3738 const data = readFileSync ( this . WEBHOOK_DATA_FILE , "utf-8" ) ;
38- this . webhookData = JSON . parse ( data ) ;
39+ const parsed = JSON . parse ( data ) ;
40+ this . webhookData = parsed && parsed . webhooks ? parsed : { webhooks : [ ] , nextHeraldNumber : 1 } ;
41+ if ( this . webhookData && typeof this . webhookData . nextHeraldNumber !== 'number' ) {
42+ this . webhookData . nextHeraldNumber = 1 ;
43+ }
3944 } catch ( error ) {
40- this . webhookData = { webhooks : [ ] } ;
45+ console . log ( "Initializing new webhook data file" ) ;
46+ this . webhookData = { webhooks : [ ] , nextHeraldNumber : 1 } ;
4147 }
4248 }
4349
@@ -106,13 +112,19 @@ export class WebhookManager {
106112 this . loadWebhookData ( ) ;
107113 }
108114
109- const existingWebhook = this . webhookData ! . webhooks . find (
110- ( w ) => w . guildId === channel . guild . id && w . name === customName ,
115+ if ( ! this . webhookData ) {
116+ throw new Error ( "Failed to initialize webhook data" ) ;
117+ }
118+
119+ const finalName = customName || `herald_${ this . webhookData . nextHeraldNumber } ` ;
120+
121+ const existingWebhook = this . webhookData . webhooks . find (
122+ ( w ) => w . guildId === channel . guild . id && w . name === finalName ,
111123 ) ;
112124
113125 if ( existingWebhook ) {
114126 throw new Error (
115- `Webhook with name "${ customName } " already exists in this guild` ,
127+ `Webhook with name "${ finalName } " already exists in this guild` ,
116128 ) ;
117129 }
118130
@@ -129,11 +141,16 @@ export class WebhookManager {
129141 token : webhook . token ! ,
130142 channelId : channel . id ,
131143 guildId : channel . guild . id ,
132- name : customName || `webhook_ ${ Date . now ( ) } ` ,
144+ name : finalName ,
133145 createdAt : Date . now ( ) ,
134146 } ;
135147
136- this . webhookData ! . webhooks . push ( webhookEntry ) ;
148+ this . webhookData . webhooks . push ( webhookEntry ) ;
149+
150+ if ( ! customName ) {
151+ this . webhookData . nextHeraldNumber ++ ;
152+ }
153+
137154 this . saveWebhookData ( ) ;
138155
139156 return webhook ;
@@ -154,7 +171,12 @@ export class WebhookManager {
154171 this . loadWebhookData ( ) ;
155172 }
156173
157- const guildWebhooks = this . webhookData ! . webhooks . filter (
174+ if ( ! this . webhookData ) {
175+ console . error ( "Failed to load webhook data" ) ;
176+ return null ;
177+ }
178+
179+ const guildWebhooks = this . webhookData . webhooks . filter (
158180 ( w ) => w . guildId === guild . id ,
159181 ) ;
160182
@@ -191,7 +213,12 @@ export class WebhookManager {
191213 this . loadWebhookData ( ) ;
192214 }
193215
194- return this . webhookData ! . webhooks . filter ( ( w ) => w . guildId === guild . id ) ;
216+ if ( ! this . webhookData ) {
217+ console . error ( "Failed to load webhook data" ) ;
218+ return [ ] ;
219+ }
220+
221+ return this . webhookData . webhooks . filter ( ( w ) => w . guildId === guild . id ) ;
195222 }
196223
197224 /**
@@ -205,15 +232,20 @@ export class WebhookManager {
205232 this . loadWebhookData ( ) ;
206233 }
207234
208- const webhookIndex = this . webhookData ! . webhooks . findIndex (
235+ if ( ! this . webhookData ) {
236+ console . error ( "Failed to load webhook data" ) ;
237+ return false ;
238+ }
239+
240+ const webhookIndex = this . webhookData . webhooks . findIndex (
209241 ( w ) => w . guildId === guild . id && ( webhookName ? w . name === webhookName : true ) ,
210242 ) ;
211243
212244 if ( webhookIndex === - 1 ) {
213245 return false ;
214246 }
215247
216- const webhookEntry = this . webhookData ! . webhooks [ webhookIndex ] ;
248+ const webhookEntry = this . webhookData . webhooks [ webhookIndex ] ;
217249
218250 try {
219251 const webhook = await guild . client . fetchWebhook (
@@ -222,12 +254,13 @@ export class WebhookManager {
222254 ) ;
223255 await webhook . delete ( "Cleaning up webhook" ) ;
224256
225- this . webhookData ! . webhooks . splice ( webhookIndex , 1 ) ;
257+ this . webhookData . webhooks . splice ( webhookIndex , 1 ) ;
226258 this . saveWebhookData ( ) ;
227259 return true ;
228260 } catch ( error ) {
229261 console . error ( "Error deleting webhook:" , error ) ;
230- this . webhookData ! . webhooks . splice ( webhookIndex , 1 ) ;
262+ // Remove from data even if Discord API call failed
263+ this . webhookData . webhooks . splice ( webhookIndex , 1 ) ;
231264 this . saveWebhookData ( ) ;
232265 return true ;
233266 }
@@ -340,11 +373,11 @@ export class WebhookManager {
340373 } ;
341374 }
342375
343- const displayName = webhookName || `webhook_ ${ Date . now ( ) } ` ;
376+ const displayName = webhookName || `herald_ ${ this . webhookData ?. nextHeraldNumber || 1 } ` ;
344377 webhook = await this . createWebhook (
345378 targetChannel ,
346379 "Altershaper Herald" ,
347- displayName ,
380+ webhookName , // Pass the original webhookName since it could be undefined
348381 ) ;
349382
350383 if ( webhook ) {
0 commit comments