2023-12-24 10:21:40 -05:00
import Discord from 'discord.js' ;
import TClient from '../client.js' ;
import ConfigHelper from '../helpers/ConfigHelper.js' ;
const config = ConfigHelper . readConfig ( ) ;
type ChannelList = keyof typeof config . dcServer . channels ;
export default class HookMgr {
private client :TClient ;
private channel :ChannelList ;
private webhookId :Discord.Snowflake ;
constructor ( client :TClient , channel :ChannelList , webhookId :Discord.Snowflake ) {
this . client = client ;
this . channel = channel ;
this . webhookId = webhookId ;
}
2024-05-04 22:53:01 -04:00
protected channelFetch = ( channel :ChannelList ) = > this . client . channels . cache . get ( config . dcServer . channels [ channel ] ) as Discord . TextChannel ;
2024-01-16 14:33:06 -05:00
protected async fetch ( channel :ChannelList , webhookId :Discord.Snowflake ) {
2024-05-04 22:53:01 -04:00
const hookInstance = await this . channelFetch ( channel ) . fetchWebhooks ( ) . then ( x = > x . get ( webhookId ) ) ;
2023-12-24 10:21:40 -05:00
if ( ! hookInstance ) throw new Error ( '[HookManager] Webhook not found.' ) ;
return hookInstance ;
}
async send ( message :string | Discord . MessagePayload | Discord . WebhookMessageCreateOptions ) {
2024-01-16 14:33:06 -05:00
const hook = await this . fetch ( this . channel , this . webhookId ) ;
2024-05-04 22:53:01 -04:00
return hook . send ( message ) . catch ( err = > ( this . client . channels . cache . get ( config . dcServer . channels . errors ) as Discord . TextChannel ) . send ( ` [HookManager] Failed to send a webhook message in # ${ this . channel } : \ n \` \` \` \ n ${ err . message } \ n \` \` \` ` ) ) ;
2023-12-24 10:21:40 -05:00
}
async edit ( messageId :Discord.Snowflake , message :string | Discord . MessagePayload | Discord . WebhookMessageEditOptions ) {
2024-01-16 14:33:06 -05:00
const hook = await this . fetch ( this . channel , this . webhookId ) ;
2024-05-04 22:53:01 -04:00
return hook . editMessage ( messageId , message ) . catch ( err = > ( this . client . channels . cache . get ( config . dcServer . channels . errors ) as Discord . TextChannel ) . send ( ` [HookManager] Failed to edit a webhook message in # ${ this . channel } : \ n \` \` \` \ n ${ err . message } \ n \` \` \` ` ) ) ;
2023-12-24 10:21:40 -05:00
}
}