2020-10-20 23:25:34 +02:00
/ * *
2021-07-11 18:46:40 +02:00
* @ name ShowBadgesInChat
2021-03-05 13:26:41 +01:00
* @ author DevilBro
2020-10-20 23:25:34 +02:00
* @ authorId 278543574059057154
2023-08-16 11:26:41 +02:00
* @ version 2.0 . 1
2021-07-11 18:46:40 +02:00
* @ description Displays Badges ( Nitro , Hypesquad , etc ... ) in the Chat / MemberList / DMList
2020-10-20 23:25:34 +02:00
* @ invite Jx3TjNS
2020-11-19 16:45:36 +01:00
* @ donate https : //www.paypal.me/MircoWittrien
* @ patreon https : //www.patreon.com/MircoWittrien
2021-03-09 15:10:55 +01:00
* @ website https : //mwittrien.github.io/
2021-07-11 18:46:40 +02:00
* @ source https : //github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ShowBadgesInChat/
* @ updateUrl https : //mwittrien.github.io/BetterDiscordAddons/Plugins/ShowBadgesInChat/ShowBadgesInChat.plugin.js
2020-10-20 23:25:34 +02:00
* /
2018-10-16 21:33:23 +02:00
2020-09-19 20:49:33 +02:00
module . exports = ( _ => {
2022-09-01 14:40:11 +02:00
const changeLog = {
2022-09-02 12:37:10 +02:00
2020-09-19 20:49:33 +02:00
} ;
2021-02-05 21:45:34 +01:00
2022-02-05 21:14:17 +01:00
return ! window . BDFDB _Global || ( ! window . BDFDB _Global . loaded && ! window . BDFDB _Global . started ) ? class {
2022-09-01 14:55:22 +02:00
constructor ( meta ) { for ( let key in meta ) this [ key ] = meta [ key ] ; }
getName ( ) { return this . name ; }
getAuthor ( ) { return this . author ; }
getVersion ( ) { return this . version ; }
getDescription ( ) { return ` The Library Plugin needed for ${ this . name } is missing. Open the Plugin Settings to download it. \n \n ${ this . description } ` ; }
2021-02-01 17:13:13 +01:00
downloadLibrary ( ) {
require ( "request" ) . get ( "https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js" , ( e , r , b ) => {
2021-03-05 13:14:18 +01:00
if ( ! e && b && r . statusCode == 200 ) require ( "fs" ) . writeFile ( require ( "path" ) . join ( BdApi . Plugins . folder , "0BDFDB.plugin.js" ) , b , _ => BdApi . showToast ( "Finished downloading BDFDB Library" , { type : "success" } ) ) ;
2021-03-06 14:59:48 +01:00
else BdApi . alert ( "Error" , "Could not download BDFDB Library Plugin. Try again later or download it manually from GitHub: https://mwittrien.github.io/downloader/?library" ) ;
2021-02-01 17:13:13 +01:00
} ) ;
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
load ( ) {
2020-11-19 16:51:14 +01:00
if ( ! window . BDFDB _Global || ! Array . isArray ( window . BDFDB _Global . pluginQueue ) ) window . BDFDB _Global = Object . assign ( { } , window . BDFDB _Global , { pluginQueue : [ ] } ) ;
2020-09-19 20:49:33 +02:00
if ( ! window . BDFDB _Global . downloadModal ) {
window . BDFDB _Global . downloadModal = true ;
2022-09-01 14:55:22 +02:00
BdApi . showConfirmationModal ( "Library Missing" , ` The Library Plugin needed for ${ this . name } is missing. Please click "Download Now" to install it. ` , {
2020-09-19 20:49:33 +02:00
confirmText : "Download Now" ,
cancelText : "Cancel" ,
onCancel : _ => { delete window . BDFDB _Global . downloadModal ; } ,
2020-09-20 08:15:13 +02:00
onConfirm : _ => {
delete window . BDFDB _Global . downloadModal ;
2021-02-01 17:13:13 +01:00
this . downloadLibrary ( ) ;
2020-09-20 08:15:13 +02:00
}
2020-09-19 20:49:33 +02:00
} ) ;
}
2022-09-01 14:55:22 +02:00
if ( ! window . BDFDB _Global . pluginQueue . includes ( this . name ) ) window . BDFDB _Global . pluginQueue . push ( this . name ) ;
2020-10-09 21:09:35 +02:00
}
2021-01-06 12:38:36 +01:00
start ( ) { this . load ( ) ; }
stop ( ) { }
getSettingsPanel ( ) {
2020-11-28 23:12:09 +01:00
let template = document . createElement ( "template" ) ;
2022-09-01 14:55:22 +02:00
template . innerHTML = ` <div style="color: var(--header-primary); font-size: 16px; font-weight: 300; white-space: pre; line-height: 22px;">The Library Plugin needed for ${ this . name } is missing. \n Please click <a style="font-weight: 500;">Download Now</a> to install it.</div> ` ;
2021-02-01 17:13:13 +01:00
template . content . firstElementChild . querySelector ( "a" ) . addEventListener ( "click" , this . downloadLibrary ) ;
2020-11-28 23:12:09 +01:00
return template . content . firstElementChild ;
}
2020-10-09 21:09:35 +02:00
} : ( ( [ Plugin , BDFDB ] ) => {
2021-07-05 21:16:01 +02:00
var _this ;
2022-06-16 00:27:01 +02:00
var badgeConfigs = { } , loadedUsers = { } , queuedInstances = { } , requestQueue = { queue : [ ] , timeout : null , id : null } , cacheTimeout ;
2020-09-19 20:49:33 +02:00
2022-06-16 00:27:01 +02:00
const places = [ "chat" , "memberList" , "dmsList" ] ;
2023-04-19 15:54:16 +02:00
const userBadgeFlagNameMap = {
"BOT_HTTP_INTERACTIONS" : "bot_commands" ,
"HYPESQUAD_ONLINE_HOUSE_1" : "hypesquad_house_1" ,
"HYPESQUAD_ONLINE_HOUSE_2" : "hypesquad_house_2" ,
"HYPESQUAD_ONLINE_HOUSE_3" : "hypesquad_house_3" ,
"bot_commands" : "BOT_HTTP_INTERACTIONS" ,
"hypesquad_house_1" : "HYPESQUAD_ONLINE_HOUSE_1" ,
"hypesquad_house_2" : "HYPESQUAD_ONLINE_HOUSE_2" ,
"hypesquad_house_3" : "HYPESQUAD_ONLINE_HOUSE_3"
} ;
2022-06-16 00:27:01 +02:00
const badges = { } ;
const indicators = {
CURRENT _GUILD _BOOST : { value : true }
} ;
2021-07-11 18:46:40 +02:00
return class ShowBadgesInChat extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad ( ) {
2021-07-05 21:16:01 +02:00
_this = this ;
2022-10-24 10:30:39 +02:00
this . modulePatches = {
after : [
"MemberListItem" ,
2022-12-08 18:26:45 +01:00
"MessageUsername" ,
2022-10-24 10:30:39 +02:00
"PrivateChannel" ,
"UserBadges"
]
2020-09-19 20:49:33 +02:00
} ;
2020-07-01 10:16:47 +02:00
2023-04-19 15:54:16 +02:00
for ( let key in BDFDB . DiscordConstants . UserBadges ) {
let basicKey = key . replace ( /_lvl\d+/g , "" ) ;
2022-06-16 00:27:01 +02:00
if ( ! badges [ basicKey ] ) badges [ basicKey ] = { value : true , keys : [ ] } ;
2023-04-19 15:54:16 +02:00
badges [ basicKey ] . keys . push ( key ) ;
2021-05-29 21:33:18 +02:00
}
2020-09-19 20:49:33 +02:00
this . css = `
2021-07-11 18:46:40 +02:00
$ { BDFDB . dotCN . _showbadgesinchatbadges } {
2021-05-29 21:33:18 +02:00
display : inline - flex ! important ;
justify - content : center ;
align - items : center ;
flex - wrap : nowrap ;
position : relative ;
margin : 0 0 0 4 px ;
2022-09-14 09:34:55 +02:00
padding : 0 ;
2021-07-03 17:06:38 +02:00
user - select : none ;
2023-04-19 15:54:16 +02:00
pointer - events : none ! important ;
2021-05-29 21:33:18 +02:00
}
2021-07-11 18:46:40 +02:00
$ { BDFDB . dotCN . _showbadgesinchatbadges } > * {
2021-05-29 21:33:18 +02:00
margin : 0 ;
}
2021-07-11 18:46:40 +02:00
$ { BDFDB . dotCNS . _showbadgesinchatbadges + BDFDB . dotCN . userbadge } {
2021-05-06 17:50:44 +02:00
display : flex ;
justify - content : center ;
align - items : center ;
2021-05-29 21:33:18 +02:00
}
2021-07-11 18:46:40 +02:00
$ { BDFDB . dotCNS . _showbadgesinchatbadges + BDFDB . dotCN . userbadge + BDFDB . dotCN . _showbadgesinchatindicator } : : before {
2021-06-25 18:13:07 +02:00
display : none ;
2021-05-29 21:33:18 +02:00
}
2021-07-11 18:46:40 +02:00
$ { BDFDB . dotCNS . _showbadgesinchatbadgessettings + BDFDB . dotCN . userbadge } {
2021-05-30 16:26:15 +02:00
width : 24 px ! important ;
height : 20 px ! important ;
2021-05-29 21:33:18 +02:00
}
$ { BDFDB . dotCN . memberpremiumicon } {
display : none ;
}
2021-07-11 18:46:40 +02:00
$ { BDFDB . dotCNS . _showbadgesinchatbadges + BDFDB . dotCN . memberpremiumicon } {
2021-05-29 21:33:18 +02:00
display : block ;
position : static ;
margin : 0 ;
}
2022-12-13 20:11:06 +01:00
$ { BDFDB . dotCNS . messageheadertext + BDFDB . dotCN . _showbadgesinchatbadgeschat } {
top : 0.2 rem ;
}
2021-07-11 18:46:40 +02:00
$ { BDFDB . dotCNS . messagerepliedmessage + BDFDB . dotCN . _showbadgesinchatbadgeschat } {
2021-01-11 14:07:57 +01:00
top : 0 ;
}
2023-04-20 11:38:12 +02:00
$ { BDFDB . dotCN . messageheadertext } : has ( $ { BDFDB . dotCN . _showbadgesinchatbadges } ) $ { BDFDB . dotCN . bottag } {
top : 0.4 rem ;
}
2021-07-11 18:46:40 +02:00
$ { BDFDB . dotCNS . messagecompact + BDFDB . dotCN . messageusername } ~ $ { BDFDB . dotCN . _showbadgesinchatbadges } ,
$ { BDFDB . dotCNS . messagerepliedmessage + BDFDB . dotCN . messageusername } ~ $ { BDFDB . dotCN . _showbadgesinchatbadges } {
2020-09-19 20:49:33 +02:00
margin - right : . 25 rem ;
text - indent : 0 ;
}
2021-07-11 18:46:40 +02:00
$ { BDFDB . dotCNS . messagerepliedmessage + BDFDB . dotCN . messageusername } ~ $ { BDFDB . dotCN . _showbadgesinchatbadges } {
2021-01-11 14:07:57 +01:00
margin - left : 0 ;
}
2020-09-19 20:49:33 +02:00
2021-07-11 18:46:40 +02:00
$ { BDFDB . dotCN . _showbadgesinchatbadgessettings } {
2020-11-23 19:20:23 +01:00
color : var ( -- header - primary ) ;
}
2022-06-16 00:27:01 +02:00
$ { BDFDB . dotCN . _showbadgesinchatbadgessettings } * {
cursor : default ;
}
$ { BDFDB . dotCN . _showbadgesinchatbadgessettings } : last - child {
margin - right : 8 px ;
}
2022-09-14 09:34:55 +02:00
$ { BDFDB . dotCN . _showbadgesinchatbadges } . bd - profile - badge {
height : 15 px ;
}
$ { BDFDB . dotCN . _showbadgesinchatbadgeschat } . bd - profile - badge {
position : relative ;
top : - 1 px ;
}
$ { BDFDB . dotCN . _showbadgesinchatbadgesmemberlist } . bd - profile - badge {
display : none ;
}
2020-09-19 20:49:33 +02:00
` ;
2020-02-04 08:20:40 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStart ( ) {
2021-07-05 21:36:33 +02:00
queuedInstances = { } , loadedUsers = { } ;
2020-11-19 16:51:14 +01:00
requestQueue = { queue : [ ] , timeout : null , id : null } ;
2020-06-05 12:00:46 +02:00
2022-06-16 00:27:01 +02:00
badgeConfigs = BDFDB . DataUtils . load ( this , "badgeConfigs" ) ;
for ( let key in badges ) {
if ( ! badgeConfigs [ key ] ) badgeConfigs [ key ] = { } ;
for ( let key2 of places ) if ( badgeConfigs [ key ] [ key2 ] == undefined ) badgeConfigs [ key ] [ key2 ] = true ;
badgeConfigs [ key ] . key = key ;
}
for ( let key in indicators ) {
if ( ! badgeConfigs [ key ] ) badgeConfigs [ key ] = { } ;
for ( let key2 of places ) if ( badgeConfigs [ key ] [ key2 ] == undefined ) badgeConfigs [ key ] [ key2 ] = true ;
badgeConfigs [ key ] . key = key ;
}
2020-06-05 12:00:46 +02:00
let badgeCache = BDFDB . DataUtils . load ( this , "badgeCache" ) ;
if ( badgeCache ) {
let now = ( new Date ( ) ) . getTime ( ) , month = 1000 * 60 * 60 * 24 * 30 ;
for ( let id in badgeCache ) {
if ( now - badgeCache [ id ] . date > month ) delete badgeCache [ id ] ;
else loadedUsers [ id ] = badgeCache [ id ] ;
}
BDFDB . DataUtils . save ( badgeCache , this , "badgeCache" ) ;
}
2021-07-08 22:05:16 +02:00
const processUser = ( id , data ) => {
let userCopy = Object . assign ( { } , data . user ) ;
userCopy . premium _since = data . premium _since ;
userCopy . premium _guild _since = data . premium _guild _since ;
loadedUsers [ id ] = BDFDB . ObjectUtils . extract ( userCopy , "flags" , "premium_since" , "premium_guild_since" ) ;
loadedUsers [ id ] . date = ( new Date ( ) ) . getTime ( ) ;
2023-08-16 11:26:41 +02:00
if ( data . badges && data . badges . find ( n => n . id == "automod" ) ) loadedUsers [ id ] . automod = true ;
2023-04-27 09:24:15 +02:00
if ( data . badges ) for ( let badge of data . badges ) {
let userFlag = BDFDB . DiscordConstants . UserFlags [ ( userBadgeFlagNameMap [ badge . id ] || badge . id || "" ) . toUpperCase ( ) ] ;
if ( userFlag && ( loadedUsers [ id ] . flags | userFlag ) != loadedUsers [ id ] . flags ) loadedUsers [ id ] . flags += userFlag ;
}
2021-07-08 22:05:16 +02:00
BDFDB . TimeUtils . clear ( cacheTimeout ) ;
cacheTimeout = BDFDB . TimeUtils . timeout ( _ => BDFDB . DataUtils . save ( loadedUsers , this , "badgeCache" ) , 5000 ) ;
if ( requestQueue . id && requestQueue . id == id ) {
BDFDB . ReactUtils . forceUpdate ( queuedInstances [ requestQueue . id ] ) ;
delete queuedInstances [ requestQueue . id ] ;
requestQueue . id = null ;
BDFDB . TimeUtils . timeout ( _ => this . runQueue ( ) , 1000 ) ;
}
} ;
2020-09-11 19:31:36 +02:00
BDFDB . PatchUtils . patch ( this , BDFDB . LibraryModules . DispatchApiUtils , "dispatch" , { after : e => {
2022-08-01 22:55:52 +02:00
if ( BDFDB . ObjectUtils . is ( e . methodArguments [ 0 ] ) && e . methodArguments [ 0 ] . type == "USER_PROFILE_FETCH_FAILURE" && e . methodArguments [ 0 ] . userId ) {
2022-09-27 16:53:10 +02:00
const user = BDFDB . LibraryStores . UserStore . getUser ( e . methodArguments [ 0 ] . userId ) ;
2023-04-19 15:54:16 +02:00
if ( ! loadedUsers [ user . id ] ) processUser ( e . methodArguments [ 0 ] . userId , { user : user || { } , flags : user ? user . publicFlags : 0 } ) ;
}
else if ( BDFDB . ObjectUtils . is ( e . methodArguments [ 0 ] ) && e . methodArguments [ 0 ] . type == "USER_PROFILE_FETCH_SUCCESS" && e . methodArguments [ 0 ] . user ) {
processUser ( e . methodArguments [ 0 ] . user . id , e . methodArguments [ 0 ] ) ;
}
} } ) ;
BDFDB . PatchUtils . patch ( this , BDFDB . LibraryModules . MemberDisplayUtils , "getUserProfile" , { after : e => {
2023-04-20 11:38:12 +02:00
if ( typeof e . methodArguments [ 0 ] != "string" || ! e . methodArguments [ 0 ] . startsWith ( "SHOWBADGES__" ) ) return ;
if ( e . methodArguments [ 0 ] . startsWith ( "SHOWBADGES__USER__" ) ) {
let realUserId = e . methodArguments [ 0 ] . split ( "__" ) . pop ( ) ;
if ( ! loadedUsers [ realUserId ] ) return ;
else if ( ! e . returnValue || e . returnValue . _userProfile && e . returnValue . _userProfile . profileFetchFailed ) {
let foundBadges = [ ] ;
for ( let key in BDFDB . DiscordConstants . UserFlags ) if ( BDFDB . DiscordConstants . UserFlags [ key ] < 100000000000 ) {
if ( ( loadedUsers [ realUserId ] . flags | BDFDB . DiscordConstants . UserFlags [ key ] ) == loadedUsers [ realUserId ] . flags ) {
let keyName = key . replace ( "_LEVEL_" , "_LVL" ) ;
keyName = ( userBadgeFlagNameMap [ keyName ] || keyName || "" ) . toLowerCase ( ) ;
if ( badges [ keyName ] ) foundBadges . push ( { icon : BDFDB . DiscordConstants . UserBadges [ keyName ] , id : keyName } ) ;
}
}
2023-08-16 11:26:41 +02:00
if ( loadedUsers [ realUserId ] . automod ) foundBadges . push ( { icon : BDFDB . DiscordConstants . UserBadges . automod , id : "automod" } ) ;
2023-04-20 11:38:12 +02:00
if ( loadedUsers [ realUserId ] . premium _since ) foundBadges . push ( { icon : BDFDB . DiscordConstants . UserBadges . premium , id : "premium" } ) ;
if ( loadedUsers [ realUserId ] . premium _guild _since ) {
let level = this . getBoostLevel ( new Date ( loadedUsers [ realUserId ] . premium _guild _since ) ) ;
if ( level ) foundBadges . push ( { icon : BDFDB . DiscordConstants . UserBadges [ ` guild_booster_lvl ${ level } ` ] , id : ` guild_booster_lvl ${ level } ` } ) ;
}
if ( ! foundBadges . length ) return ;
if ( ! e . returnValue ) return {
getBadges : _ => foundBadges ,
getBannerURL : _ => null ,
premiumSince : loadedUsers [ realUserId ] . premium _since ? new Date ( loadedUsers [ realUserId ] . premium _since ) : null ,
premiumGuildSince : loadedUsers [ realUserId ] . premium _guild _since ? new Date ( loadedUsers [ realUserId ] . premium _guild _since ) : null ,
_userProfile : {
badges : foundBadges ,
premiumSince : loadedUsers [ realUserId ] . premium _since ? new Date ( loadedUsers [ realUserId ] . premium _since ) : null ,
premiumGuildSince : loadedUsers [ realUserId ] . premium _guild _since ? new Date ( loadedUsers [ realUserId ] . premium _guild _since ) : null
}
} ;
else {
let newProfileObject = BDFDB . ObjectUtils . copy ( e . returnValue ) ;
newProfileObject . getBadges = _ => foundBadges ;
newProfileObject . _userProfile . badges = foundBadges ;
if ( loadedUsers [ realUserId ] . premium _since ) newProfileObject . premiumSince = newProfileObject . _userProfile . premiumSince = new Date ( loadedUsers [ realUserId ] . premium _since ) ;
if ( loadedUsers [ realUserId ] . premium _guild _since ) newProfileObject . premiumGuildSince = newProfileObject . _userProfile . premiumGuildSince = new Date ( loadedUsers [ realUserId ] . premium _guild _since ) ;
return newProfileObject ;
}
}
}
else if ( e . methodArguments [ 0 ] . startsWith ( "SHOWBADGES__" ) ) {
2023-04-19 15:54:16 +02:00
let keyName = "" ;
2023-08-16 11:26:41 +02:00
if ( e . methodArguments [ 0 ] == "SHOWBADGES__AUTOMOD" ) keyName = "automod" ;
else if ( e . methodArguments [ 0 ] == "SHOWBADGES__NITRO" ) keyName = "premium" ;
2023-04-19 15:54:16 +02:00
else if ( e . methodArguments [ 0 ] . endsWith ( "__FLAG" ) ) {
let flag = e . methodArguments [ 0 ] . split ( "__" ) [ 1 ] ;
let userFlag = flag !== undefined && Object . entries ( BDFDB . DiscordConstants . UserFlags ) . find ( n => n && n [ 1 ] == flag ) ;
keyName = userFlag && userFlag [ 0 ] . replace ( "_LEVEL_" , "_LVL" ) ;
keyName = ( userBadgeFlagNameMap [ keyName ] || keyName || "" ) . toLowerCase ( ) ;
}
else if ( e . methodArguments [ 0 ] . startsWith ( "SHOWBADGES__GUILD_BOOST__" ) ) {
keyName = ` guild_booster_lvl ${ e . methodArguments [ 0 ] . split ( "__" ) . pop ( ) } ` ;
}
if ( keyName ) return {
getBadges : _ => [ { icon : BDFDB . DiscordConstants . UserBadges [ keyName ] , id : keyName } ] ,
getBannerURL : _ => null ,
_userProfile : { badges : [ { icon : BDFDB . DiscordConstants . UserBadges [ keyName ] , id : keyName } ] }
} ;
}
2020-06-05 12:00:46 +02:00
} } ) ;
2020-02-04 08:20:40 +01:00
this . forceUpdateAll ( ) ;
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStop ( ) {
2020-02-04 08:20:40 +01:00
BDFDB . TimeUtils . clear ( requestQueue . timeout ) ;
this . forceUpdateAll ( ) ;
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
getSettingsPanel ( collapseStates = { } ) {
2021-05-19 20:22:29 +02:00
let settingsPanel ;
return settingsPanel = BDFDB . PluginUtils . createSettingsPanel ( this , {
collapseStates : collapseStates ,
children : _ => {
let settingsItems = [ ] ;
2022-06-16 00:27:01 +02:00
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FormComponents . FormTitle , {
className : BDFDB . disCN . marginbottom4 ,
2022-11-22 01:30:30 +01:00
tag : BDFDB . LibraryComponents . FormComponents . FormTags . H3 ,
2022-06-16 00:27:01 +02:00
children : "Show Badges in"
2021-05-19 20:22:29 +02:00
} ) ) ;
2022-06-16 00:27:01 +02:00
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsList , {
settings : places ,
2022-10-24 10:30:39 +02:00
data : Object . keys ( badges ) . filter ( n => n . indexOf ( "BOT_" ) != 0 ) . concat ( Object . keys ( indicators ) ) . map ( key => badgeConfigs [ key ] ) ,
2022-06-16 00:27:01 +02:00
noRemove : true ,
renderLabel : ( cardData , instance ) => BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Flex , {
children : [
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Flex . Child , {
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Clickable , {
onClick : _ => {
for ( let settingId of places ) badgeConfigs [ cardData . key ] [ settingId ] = true ;
BDFDB . DataUtils . save ( badgeConfigs , this , "badgeConfigs" ) ;
BDFDB . ReactUtils . forceUpdate ( instance ) ;
this . SettingsUpdated = true ;
} ,
onContextMenu : _ => {
for ( let settingId of places ) badgeConfigs [ cardData . key ] [ settingId ] = false ;
BDFDB . DataUtils . save ( badgeConfigs , this , "badgeConfigs" ) ;
BDFDB . ReactUtils . forceUpdate ( instance ) ;
this . SettingsUpdated = true ;
} ,
2022-09-27 11:53:04 +02:00
children : cardData . key . split ( "_" ) . map ( n => BDFDB . StringUtils . upperCaseFirstChar ( n . toLowerCase ( ) ) ) . join ( " " )
2022-06-16 00:27:01 +02:00
} )
} ) ,
this . createSettingsBadges ( cardData . key )
]
} ) ,
onHeaderClick : ( settingId , instance ) => {
for ( let key in badgeConfigs ) badgeConfigs [ key ] [ settingId ] = true ;
BDFDB . DataUtils . save ( badgeConfigs , this , "badgeConfigs" ) ;
BDFDB . ReactUtils . forceUpdate ( instance ) ;
this . SettingsUpdated = true ;
} ,
onHeaderContextMenu : ( settingId , instance ) => {
for ( let key in badgeConfigs ) badgeConfigs [ key ] [ settingId ] = false ;
BDFDB . DataUtils . save ( badgeConfigs , this , "badgeConfigs" ) ;
BDFDB . ReactUtils . forceUpdate ( instance ) ;
this . SettingsUpdated = true ;
} ,
onCheckboxChange : ( value , instance ) => {
badgeConfigs [ instance . props . cardId ] [ instance . props . settingId ] = value ;
BDFDB . DataUtils . save ( badgeConfigs , this , "badgeConfigs" ) ;
this . SettingsUpdated = true ;
}
2021-05-19 20:22:29 +02:00
} ) ) ;
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsItem , {
type : "Button" ,
color : BDFDB . LibraryComponents . Button . Colors . RED ,
label : "Reset cached Badge Data" ,
onClick : _ => BDFDB . ModalUtils . confirm ( this , "Are you sure you want to reset the Badge Cache? This will force all Badges to rerender." , _ => {
BDFDB . DataUtils . remove ( this , "badgeCache" ) ;
2022-06-16 00:27:01 +02:00
this . SettingsUpdated = true ;
2021-05-19 20:22:29 +02:00
} ) ,
children : BDFDB . LanguageUtils . LanguageStrings . RESET
} ) ) ;
return settingsItems ;
}
} ) ;
2020-02-04 08:20:40 +01:00
}
2019-10-30 14:03:32 +01:00
2021-01-06 12:38:36 +01:00
onSettingsClosed ( ) {
2020-09-19 20:49:33 +02:00
if ( this . SettingsUpdated ) {
delete this . SettingsUpdated ;
this . forceUpdateAll ( ) ;
}
2020-02-04 08:20:40 +01:00
}
2020-09-19 20:49:33 +02:00
2021-05-19 20:22:29 +02:00
forceUpdateAll ( ) {
2020-09-19 20:49:33 +02:00
BDFDB . PatchUtils . forceAllUpdates ( this ) ;
BDFDB . MessageUtils . rerenderAll ( ) ;
2020-02-04 08:20:40 +01:00
}
2019-09-04 12:34:02 +02:00
2022-12-08 18:26:45 +01:00
processMessageUsername ( e ) {
2022-06-16 00:27:01 +02:00
if ( ! e . instance . props . message ) return ;
2022-12-08 18:26:45 +01:00
let [ children , index ] = BDFDB . ReactUtils . findParent ( e . returnvalue , { filter : n => n && n . props && typeof n . props . renderPopout == "function" } ) ;
2022-05-27 19:30:13 +02:00
if ( index == - 1 ) return ;
2021-06-11 22:06:02 +02:00
const author = e . instance . props . userOverride || e . instance . props . message . author ;
2022-09-27 14:48:10 +02:00
this . injectBadges ( children , author , ( BDFDB . LibraryStores . ChannelStore . getChannel ( e . instance . props . message . channel _id ) || { } ) . guild _id , "chat" ) ;
2020-02-04 08:20:40 +01:00
}
2021-07-11 18:46:40 +02:00
processMemberListItem ( e ) {
2022-06-16 00:27:01 +02:00
if ( ! e . instance . props . user ) return ;
this . injectBadges ( BDFDB . ObjectUtils . get ( e . returnvalue , "props.decorators.props.children" ) , e . instance . props . user , e . instance . props . channel . guild _id , "memberList" ) ;
2021-07-11 18:46:40 +02:00
}
processPrivateChannel ( e ) {
2022-06-16 00:27:01 +02:00
if ( ! e . instance . props . user ) return ;
2022-02-25 16:12:53 +01:00
let wrapper = e . returnvalue && e . returnvalue . props . children && e . returnvalue . props . children . props && typeof e . returnvalue . props . children . props . children == "function" ? e . returnvalue . props . children : e . returnvalue ;
if ( typeof wrapper . props . children == "function" ) {
let childrenRender = wrapper . props . children ;
wrapper . props . children = BDFDB . TimeUtils . suppress ( ( ... args ) => {
2021-11-12 16:30:05 +01:00
let children = childrenRender ( ... args ) ;
2022-02-11 15:49:25 +01:00
this . _processPrivateChannel ( e . instance , children ) ;
2021-11-12 16:30:05 +01:00
return children ;
2022-02-11 15:49:25 +01:00
} , "Error in Children Render of PrivateChannel!" , this ) ;
2021-11-12 16:30:05 +01:00
}
2022-02-25 16:12:53 +01:00
else this . _processPrivateChannel ( e . instance , wrapper ) ;
2022-02-11 15:49:25 +01:00
}
_processPrivateChannel ( instance , returnvalue , a ) {
const wrapper = returnvalue . props . decorators ? returnvalue : BDFDB . ReactUtils . findChild ( returnvalue , { props : [ "decorators" ] } ) || returnvalue ;
if ( ! wrapper ) return ;
wrapper . props . decorators = [ wrapper . props . decorators ] . flat ( 10 ) ;
2022-06-16 00:27:01 +02:00
this . injectBadges ( wrapper . props . decorators , instance . props . user , null , "dmsList" ) ;
2021-07-11 18:46:40 +02:00
}
2021-05-29 21:33:18 +02:00
2022-10-24 10:30:39 +02:00
processUserBadges ( e ) {
2021-05-29 21:33:18 +02:00
if ( e . instance . props . custom ) {
2022-06-16 00:27:01 +02:00
let filter = e . instance . props . place != "settings" ;
2021-05-29 21:33:18 +02:00
for ( let i in e . returnvalue . props . children ) if ( e . returnvalue . props . children [ i ] ) {
2023-04-19 15:54:16 +02:00
let keyName = filter && Object . keys ( badges ) . find ( n => badges [ n ] . keys . includes ( e . returnvalue . props . children [ i ] . key ) ) ;
2022-06-16 00:27:01 +02:00
if ( keyName && badgeConfigs [ keyName ] && ! badgeConfigs [ keyName ] [ e . instance . props . place ] ) e . returnvalue . props . children [ i ] = null ;
2022-12-10 11:15:13 +01:00
else if ( typeof e . returnvalue . props . children [ i ] . props . children == "function" && e . returnvalue . props . children [ i ] . props . text ) {
2021-05-30 17:50:07 +02:00
e . returnvalue . props . children [ i ] = BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TooltipContainer , e . returnvalue . props . children [ i ] . props ) ;
}
2021-05-29 21:33:18 +02:00
}
2022-06-16 00:27:01 +02:00
if ( e . instance . props . premiumCurrentGuildSince && ! ( filter && badgeConfigs . CURRENT _GUILD _BOOST && ! badgeConfigs . CURRENT _GUILD _BOOST [ e . instance . props . place ] ) ) e . returnvalue . props . children . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TooltipContainer , {
2021-05-29 22:16:50 +02:00
text : BDFDB . LanguageUtils . LanguageStringsFormat ( "PREMIUM_GUILD_SUBSCRIPTION_TOOLTIP" , e . instance . props . premiumCurrentGuildSince ) ,
2021-05-29 21:33:18 +02:00
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Clickable , {
2021-06-25 18:13:07 +02:00
className : BDFDB . disCN . userbadgeouter ,
children : BDFDB . ReactUtils . createElement ( "div" , {
2021-07-11 18:46:40 +02:00
className : BDFDB . disCNS . userbadge + BDFDB . disCN . _showbadgesinchatindicator ,
2021-06-25 18:13:07 +02:00
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SvgIcon , {
className : BDFDB . disCN . memberpremiumicon ,
name : BDFDB . LibraryComponents . SvgIcon . Names . BOOST
} )
2021-05-29 21:33:18 +02:00
} )
} )
} ) ) ;
if ( ! e . returnvalue . props . children . filter ( n => n ) . length ) return null ;
}
}
2020-09-19 20:49:33 +02:00
2022-06-16 00:27:01 +02:00
injectBadges ( children , user , guildId , place ) {
2021-07-05 21:16:01 +02:00
if ( ! BDFDB . ArrayUtils . is ( children ) || ! user || user . isNonUserBot ( ) ) return ;
if ( ! loadedUsers [ user . id ] || ( ( new Date ( ) ) . getTime ( ) - loadedUsers [ user . id ] . date >= 1000 * 60 * 60 * 24 * 7 ) ) {
2021-07-05 21:36:33 +02:00
queuedInstances [ user . id ] = [ ] . concat ( queuedInstances [ user . id ] ) . filter ( n => n ) ;
2021-07-08 22:05:16 +02:00
if ( requestQueue . queue . indexOf ( user . id ) == - 1 ) requestQueue . queue . push ( user . id ) ;
2020-09-19 20:49:33 +02:00
this . runQueue ( ) ;
}
2021-07-05 21:16:01 +02:00
children . push ( BDFDB . ReactUtils . createElement ( class extends BDFDB . ReactUtils . Component {
render ( ) {
if ( ! loadedUsers [ user . id ] || ( ( new Date ( ) ) . getTime ( ) - loadedUsers [ user . id ] . date >= 1000 * 60 * 60 * 24 * 7 ) ) {
2023-05-31 08:36:35 +02:00
queuedInstances [ user . id ] = [ ] . concat ( queuedInstances [ user . id ] ) . filter ( n => n ) ;
2021-07-05 21:36:33 +02:00
if ( queuedInstances [ user . id ] . indexOf ( this ) == - 1 ) queuedInstances [ user . id ] . push ( this ) ;
2021-07-05 21:16:01 +02:00
return null ;
}
2023-04-20 11:38:12 +02:00
else {
user = BDFDB . ObjectUtils . copy ( user ) ;
user . id = "SHOWBADGES__USER__" + user . id ;
return _this . createBadges ( user , guildId , place ) ;
}
2021-07-05 21:16:01 +02:00
}
} , { } , true ) ) ;
2020-02-04 08:20:40 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
runQueue ( ) {
2020-09-19 20:49:33 +02:00
if ( ! requestQueue . id ) {
let id = requestQueue . queue . shift ( ) ;
if ( id ) {
requestQueue . id = id ;
BDFDB . TimeUtils . clear ( requestQueue . timeout ) ;
requestQueue . timeout = BDFDB . TimeUtils . timeout ( _ => {
requestQueue . id = null ;
this . runQueue ( ) ;
} , 30000 ) ;
2023-04-19 15:54:16 +02:00
2020-09-19 20:49:33 +02:00
BDFDB . LibraryModules . UserProfileUtils . fetchProfile ( id ) ;
}
}
2020-02-04 08:20:40 +01:00
}
2020-09-19 20:49:33 +02:00
2022-06-16 00:27:01 +02:00
createBadges ( user , guildId , place ) {
2021-05-29 21:33:18 +02:00
let fakeGuildBoostDate ;
2023-04-19 15:54:16 +02:00
if ( typeof user . id == "string" && user . id . startsWith ( "SHOWBADGES__GUILD_BOOST__" ) ) {
let level = parseInt ( user . id . split ( "__" ) . pop ( ) ) ;
2021-05-29 21:33:18 +02:00
for ( let i = 0 ; i < 100 && ! fakeGuildBoostDate ; i ++ ) {
let date = new Date ( ) - 1000 * 60 * 60 * 24 * 15 * i ;
2023-04-19 15:54:16 +02:00
if ( level == this . getBoostLevel ( date ) ) fakeGuildBoostDate = date ;
2021-05-29 21:33:18 +02:00
}
2021-05-29 21:07:54 +02:00
}
2023-04-20 11:38:12 +02:00
let realUserId = typeof user . id == "string" && user . id . startsWith ( "SHOWBADGES__USER__" ) ? user . id . split ( "__" ) . pop ( ) : user . id ;
let member = guildId && BDFDB . LibraryStores . GuildMemberStore . getMember ( guildId , realUserId ) ;
2022-10-02 17:41:38 +02:00
return BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . UserBadgeComponents . UserBadges , {
2022-06-16 00:27:01 +02:00
className : BDFDB . DOMUtils . formatClassName ( BDFDB . disCN . _showbadgesinchatbadges , BDFDB . disCN [ ` _showbadgesinchatbadges ${ place . toLowerCase ( ) } ` ] ) ,
2021-05-29 21:33:18 +02:00
user : user ,
2022-10-02 17:41:38 +02:00
size : BDFDB . LibraryComponents . UserBadgeComponents . Sizes . SIZE _18 ,
2021-05-29 21:33:18 +02:00
custom : true ,
2022-06-16 00:27:01 +02:00
place : place ,
2023-08-16 11:26:41 +02:00
automod : loadedUsers [ realUserId ] && loadedUsers [ realUserId ] . automod ? true : ( realUserId == "SHOWBADGES__AUTOMOD" ? true : null ) ,
2023-04-20 11:38:12 +02:00
premiumSince : loadedUsers [ realUserId ] && loadedUsers [ realUserId ] . premium _since ? new Date ( loadedUsers [ realUserId ] . premium _since ) : ( realUserId == "SHOWBADGES__NITRO" ? new Date ( ) : null ) ,
premiumGuildSince : fakeGuildBoostDate || ( loadedUsers [ realUserId ] && loadedUsers [ realUserId ] . premium _guild _since ? new Date ( loadedUsers [ realUserId ] . premium _guild _since ) : null ) ,
premiumCurrentGuildSince : member && member . premiumSince && new Date ( member . premiumSince ) || realUserId == "SHOWBADGES__CURRENT_GUILD_BOOST" && new Date ( )
2020-09-19 20:49:33 +02:00
} ) ;
2019-06-07 11:39:54 +02:00
}
2020-06-08 10:34:09 +02:00
2020-09-19 20:49:33 +02:00
createSettingsBadges ( flag ) {
2021-05-29 21:33:18 +02:00
let wrappers = [ ] ;
2022-06-16 00:27:01 +02:00
if ( indicators [ flag ] ) {
2023-04-19 15:54:16 +02:00
let user = new BDFDB . DiscordObjects . User ( { flags : 0 , id : "SHOWBADGES__" + flag } ) ;
2021-06-25 18:13:07 +02:00
wrappers . push ( this . createBadges ( user , null , "settings" ) ) ;
2020-09-19 20:49:33 +02:00
}
2022-06-16 00:27:01 +02:00
else for ( let key of badges [ flag ] . keys ) {
2023-04-19 15:54:16 +02:00
let keyName = key . replace ( "_lvl" , "_level_" ) ;
keyName = ( userBadgeFlagNameMap [ keyName ] || keyName || "" ) . toUpperCase ( ) ;
2023-08-16 11:26:41 +02:00
let userFlag = flag == "automod" || flag == "premium" || flag == "guild_booster" ? 0 : BDFDB . DiscordConstants . UserFlags [ keyName . toUpperCase ( ) ] ;
2021-05-29 21:33:18 +02:00
if ( userFlag == null && keyName ) userFlag = BDFDB . DiscordConstants . UserFlags [ keyName ] != null ? BDFDB . DiscordConstants . UserFlags [ keyName ] : BDFDB . DiscordConstants . UserFlags [ Object . keys ( BDFDB . DiscordConstants . UserFlags ) . find ( f => f . indexOf ( keyName ) > - 1 || keyName . indexOf ( f ) > - 1 ) ] ;
if ( userFlag != null ) {
2023-04-19 15:54:16 +02:00
let id = "SHOWBADGES__" + userFlag + "__FLAG" ;
2023-08-16 11:26:41 +02:00
if ( flag == "automod" ) id = "SHOWBADGES__AUTOMOD" ;
else if ( flag == "premium" ) id = "SHOWBADGES__NITRO" ;
2023-04-19 15:54:16 +02:00
else if ( flag == "guild_booster" && keyName ) id = "SHOWBADGES__GUILD_BOOST__" + keyName . split ( "_" ) . pop ( ) ;
2021-05-29 21:33:18 +02:00
let user = new BDFDB . DiscordObjects . User ( { flags : userFlag , id : id } ) ;
2021-06-25 18:13:07 +02:00
wrappers . push ( this . createBadges ( user , null , "settings" ) ) ;
2021-05-29 21:33:18 +02:00
}
2020-09-19 20:49:33 +02:00
}
2021-05-29 21:33:18 +02:00
return wrappers ;
2020-09-19 20:49:33 +02:00
}
2023-04-19 15:54:16 +02:00
getBoostLevel ( date ) {
let level = 1 ;
let monthDifference = BDFDB . LibraryModules . GuildBoostUtils . getUserLevel ( date ) ;
for ( let i = 0 , levels = Object . keys ( BDFDB . DiscordConstants . UserPremiumLevels ) ; i < levels . length ; i ++ ) if ( BDFDB . DiscordConstants . UserPremiumLevels [ levels [ i ] ] < monthDifference ) level = parseInt ( levels [ i ] ) ;
return level ;
}
2020-09-19 20:49:33 +02:00
} ;
2022-09-01 14:40:11 +02:00
} ) ( window . BDFDB _Global . PluginUtils . buildPlugin ( changeLog ) ) ;
2021-05-30 01:59:06 +02:00
} ) ( ) ;