import Eris , { GuildTextableChannel } from 'eris' ; import ECH from 'eris-command-handler' ; import fetch from 'node-fetch' ; const webhookName = 'Dad Bot Webhook™' ; export default class Dadhook { public avatar ? : string ; public channel_id : string ; public guild_id : string ; public id : string ; public name : string ; public token : string ; public user : Eris . PartialUser ; public static async getGuildDadhook ( guild ) { let dadhook = ( await guild . getWebhooks ( ) ) . find ( w => w . name === webhookName ) ; if ( ! dadhook ) { let avatar = `data:image/png;base64,${ ( await ( await fetch ( ( guild as any ) . _client . user . dynamicAvatarURL ( 'png' , 2048 ) ) ) . buffer ( ) ) . toString ( 'base64' ) }` ; let validChannel = guild . channels . filter ( c => c . type === Eris . Constants . ChannelTypes . GUILD_TEXT && c . permissionsOf ( ( guild as any ) . _client . user . id ) . has ( 'manageWebhooks' ) && c . permissionsOf ( ( guild as any ) . _client . user . id ) . has ( 'readMessages' ) ) [ 0 ] as GuildTextableChannel ; let newWebhook = await validChannel . createWebhook ( { name : webhookName , avatar } ) ; return new Dadhook ( newWebhook , ( guild as any ) . _client ) ; } return new Dadhook ( dadhook , ( guild as any ) . _client ) ; } private client : ECH . CommandClient ; constructor ( webhookObj , client ) { Object . assign ( this , webhookObj ) ; this . client = client ; } public async changeChannel ( channel ) { let permsOfBot = channel . permissionsOf ( this . client . user . id ) , currentChannelPerms = this . client . guilds . get ( this . guild_id ) . channels . get ( this . channel_id ) . permissionsOf ( this . client . user . id ) ; let res ; if ( permsOfBot . has ( 'manageWebhooks' ) && permsOfBot . has ( 'readMessages' ) && currentChannelPerms . has ( 'manageWebhooks' ) && currentChannelPerms . has ( 'readMessages' ) ) { try { res = await this . client . editWebhook ( this . id , { channelID : channel . id } ) ; } catch ( err ) { throw err ; } } Object . assign ( this , res ) ; return this ; } async edit ( options , reason ) { let res ; try { res = await this . client . editWebhook ( this . id , options , this . token , reason ) ; } catch ( err ) { throw err ; } Object . assign ( this , res ) ; return this ; } async delete ( reason ) { try { await this . client . deleteWebhook ( this . id , this . token , reason ) ; } catch ( err ) { throw err ; } return ; } async send ( stuff ) { await this . client . executeWebhook ( this . id , this . token , stuff ) ; } }