import { Guild } from 'discord.js' ; import { filterAction , filterActions } from './filters' ; import { permLevels } from './permissions' ; import { Bot } from '..' ; import { durations } from './time' ; param { string } s1 param { string } s2 returns function stringSimilarity ( s1 , s2 ) { var longer = s1 ; var shorter = s2 ; if ( s1 . length < s2 . length ) { longer = s2 ; shorter = s1 ; } var longerLength = longer . length ; if ( longerLength == 0 ) { return 1.0 ; } return ( longerLength - editDistance ( longer , shorter ) ) / parseFloat ( longerLength . toString ( ) ) ; } param { string } str param { RegExp } regex returns function extractString ( str , regex ) { let result = regex . exec ( str ) ; if ( ! result ) return undefined ; return result [ result . length - 1 ] ; } param { * } s1 param { * } s2 returns function editDistance ( s1 , s2 ) { s1 = s1 . toLowerCase ( ) ; s2 = s2 . toLowerCase ( ) ; var costs = new Array ( ) ; for ( var i = 0 ; i <= s1 . length ; i ++ ) { var lastValue = i ; for ( var j = 0 ; j <= s2 . length ; j ++ ) { if ( i == 0 ) costs [ j ] = j ; else { if ( j > 0 ) { var newValue = costs [ j - 1 ] ; if ( s1 . charAt ( i - 1 ) != s2 . charAt ( j - 1 ) ) newValue = Math . min ( Math . min ( newValue , lastValue ) , costs [ j ] ) + 1 ; costs [ j - 1 ] = lastValue ; lastValue = newValue ; } } } if ( i > 0 ) costs [ s2 . length ] = lastValue ; } return costs [ s2 . length ] ; } export param { Guild } guild param { string } text param { boolean } byUsername param { boolean } byNickname param { boolean } bySimilar returns export async function stringToMember ( guild , text , byUsername = true , byNickname = true , bySimilar = true ) { text = extractString ( text , /<@!?(\d*)>/ ) || extractString ( text , /([^#@:]{2,32})#\d{4}/ ) || text ; guild = await guild . fetchMembers ( ) ; var member = guild . members . get ( text ) ; if ( ! member && byUsername ) member = guild . members . find ( x => x . user . username == text ) ; if ( ! member && byNickname ) member = guild . members . find ( x => x . nickname == text ) ; if ( ! member && bySimilar ) { member = guild . members . reduce ( function ( prev , curr ) { return ( stringSimilarity ( curr . user . username , text ) > stringSimilarity ( prev . user . username , text ) ? curr : prev ) ; } ) ; if ( stringSimilarity ( member . user . username , text ) < 0.4 ) { member = undefined ; } } return member ; } export param { string } text returns export async function stringToUser ( text ) { text = extractString ( text , /<@!?(\d*)>/ ) || text ; try { return await Bot . client . fetchUser ( text ) ; } catch { return undefined ; } } export param { Guild } guild param { string } text param { boolean } byName param { boolean } bySimilar returns export function stringToRole ( guild , text , byName = true , bySimilar = true ) { if ( text == 'here' || text == '@here' ) { return '@here' ; } if ( text == 'everyone' || text == '@everyone' ) { return '@everyone' ; } text = extractString ( text , /<@&(\d*)>/ ) || text ; var role = guild . roles . get ( text ) ; if ( ! role && byName ) { role = guild . roles . find ( x => x . name == text ) ; } if ( ! role && bySimilar ) { role = guild . roles . reduce ( function ( prev , curr ) { return ( stringSimilarity ( curr . name , text ) > stringSimilarity ( prev . name , text ) ? curr : prev ) ; } ) ; if ( stringSimilarity ( role . name , text ) < 0.4 ) { role = undefined ; } } return role ; } export param { Guild } guild param { string } text returns export function stringToChannel ( guild , text , byName = true , bySimilar = true ) { if ( ! guild ) return null ; text = extractString ( text , /<#(\d*)>/ ) || text ; let channel = guild . channels . get ( text ) ; if ( ! channel && byName ) channel = guild . channels . find ( x => x . name == text ) ; if ( ! channel && bySimilar ) { channel = guild . channels . reduce ( function ( prev , curr ) { return ( stringSimilarity ( curr . name , text ) > stringSimilarity ( prev . name , text ) ? curr : prev ) ; } ) ; if ( stringSimilarity ( channel . name , text ) < 0.4 ) { channel = undefined ; } } return channel ; } export param { string } text returns export function stringToEmbed ( text ) { var embed = null ; try { embed = JSON . parse ( text ) ; } catch ( e ) { return null ; } return embed ; } export param { filterAction } action returns export function actionToString ( action ) { switch ( action . type ) { case filterActions . nothing : return 'nothing' ; case filterActions . delete : if ( action . delay == 0 ) { return 'deleted message' ; } return `deleted message after ${ action . delay }ms` ; case filterActions . send : return `replied with '${ action . message }' to message` ; default : console . warn ( 'actionToString: unknown action' ) ; return 'unknown action' ; } } export param { number } permLevel returns export function permToString ( permLevel ) { switch ( permLevel ) { case permLevels . member : return 'member' ; case permLevels . immune : return 'immune member' ; case permLevels . mod : return 'mod' ; case permLevels . admin : return 'admin' ; case permLevels . botMaster : return 'my master' ; default : Bot . mStats . logError ( new Error ( 'unknown permission level: ' + permLevel ) ) ; console . warn ( 'unknown permission level: ' + permLevel ) ; return 'Unknown PermissionLevel' ; } } export param { number } duration returns export function durationToString ( duration ) { var ms = duration % 1000 ; duration = ( duration - ms ) / 1000 ; var seconds = duration % 60 ; duration = ( duration - seconds ) / 60 ; var minutes = duration % 60 ; duration = ( duration - minutes ) / 60 ; var hours = duration % 24 ; var days = ( duration - hours ) / 24 ; var durationString = '' ; if ( days != 0 ) durationString += days + 'd ' ; if ( hours != 0 ) durationString += hours + 'h ' ; if ( minutes != 0 ) durationString += minutes + 'm ' ; if ( seconds != 0 ) durationString += seconds + 's' ; if ( durationString == '' ) durationString = '0s' ; return durationString . trim ( ) ; } export param { string } text returns export function stringToDuration ( text ) { let ms = 0 ; let seconds = /(\d+)s/ . exec ( text ) ; if ( seconds ) ms += Number ( seconds [ 1 ] ) * durations . second ; let minutes = /(\d+)m/ . exec ( text ) ; if ( minutes ) ms += Number ( minutes [ 1 ] ) * durations . minute ; let hours = /(\d+)h/ . exec ( text ) ; if ( hours ) ms += Number ( hours [ 1 ] ) * durations . hour ; let days = /(\d+)d/ . exec ( text ) ; if ( days ) ms += Number ( days [ 1 ] ) * durations . day ; return ms ; }