import { Guild , Role , TextChannel , DMChannel , GroupDMChannel , Message } from 'discord.js' ; import { stringToRole } from './parsers' ; import { Bot } from '..' ; export param { Guild } guild param { TextChannel } channel param { string } content param { * } embed param { Message } editMessage param { number } requestTime param { string } commandName export async function sendMentionMessage ( guild , channel , content , disableEveryone = false , embed ? , editMessage ? , requestTime ? , commandName ? ) { var regex ; const roleRegex = /{{role:(.*)}}/gm ; var mentions = [ ] ; while ( ( regex = roleRegex . exec ( content ) ) !== null ) { if ( regex . index === roleRegex . lastIndex ) roleRegex . lastIndex ++ ; var wholeMatch = regex [ 0 ] . toString ( ) ; var role = stringToRole ( guild , regex [ 1 ] . toString ( ) ) ; if ( role ) { mentions . push ( [ wholeMatch , role ] ) ; } } var changedRoles = [ ] ; var managePerm = guild . me . hasPermission ( 'MANAGE_ROLES' ) ; for ( const obj of mentions ) { if ( typeof ( obj [ 1 ] ) != 'string' && ! obj [ 1 ] . mentionable && managePerm ) { await obj [ 1 ] . setMentionable ( true , 'BulletBot mention' ) ; changedRoles . push ( obj [ 1 ] ) ; } content = content . replace ( obj [ 0 ] , obj [ 1 ] . toString ( ) ) ; } if ( requestTime ) Bot . mStats . logResponseTime ( commandName , requestTime ) ; if ( ! /^\s*$/ . test ( content ) ) { if ( embed ) { embed . disableEveryone = disableEveryone ; } else embed = { disableEveryone : disableEveryone } ; if ( editMessage ) { if ( ! embed . embed ) embed . embed = embed ; await editMessage . edit ( content , embed ) ; } else { await channel . send ( content , embed ) ; } } for ( const role of changedRoles ) { role . setMentionable ( false , 'BulletBot mention revert' ) . catch ( ( reason ) => { console . error ( 'error while reverting mentionable property:' , reason ) ; Bot . mStats . logError ( new Error ( 'error while reverting mentionable property:' + reason ) ) ; } ) ; } } export param { ( TextChannel | DMChannel | GroupDMChannel ) } channel param { * } error returns export function sendError ( channel , error ) { console . error ( error ) ; Bot . mStats . logMessageSend ( ) ; return channel . send ( 'Oops something went wrong. #BlameEvan' ) ; }