2020-10-20 23:25:34 +02:00
/ * *
* @ name RemoveBlockedMessages
* @ authorId 278543574059057154
* @ invite Jx3TjNS
* @ donate https : //www.paypal.me/MircoWittrien
* @ patreon https : //www.patreon.com/MircoWittrien
* @ website https : //github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/RemoveBlockedMessages
* @ source https : //raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/RemoveBlockedMessages/RemoveBlockedMessages.plugin.js
2020-11-12 17:30:48 +01:00
* @ updateUrl https : //raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/RemoveBlockedMessages/RemoveBlockedMessages.plugin.js
2020-10-20 23:25:34 +02:00
* /
2020-02-04 10:22:02 +01:00
2020-09-19 20:49:33 +02:00
module . exports = ( _ => {
2020-10-09 21:09:35 +02:00
const config = {
2020-09-19 20:49:33 +02:00
"info" : {
"name" : "RemoveBlockedMessages" ,
"author" : "DevilBro" ,
2020-11-17 19:04:03 +01:00
"version" : "1.1.4" ,
2020-10-16 10:25:30 +02:00
"description" : "Completely removes blocked messages"
2020-09-29 08:10:01 +02:00
} ,
"changeLog" : {
2020-10-19 10:38:04 +02:00
"improved" : {
2020-11-13 21:25:46 +01:00
"Voice Channels" : "No longer plays voice notifications for blocked users and localy mutes them when they join the same voice channel as you"
2020-10-19 10:38:04 +02:00
} ,
2020-10-15 14:34:57 +02:00
"fixed" : {
2020-11-16 17:02:03 +01:00
"Voice Channels" : "Fixed issue where sounds didn't play if non-blocked users joins/leaves channel" ,
2020-11-17 19:04:03 +01:00
"Message Groups" : "No longer keeps message groups split if a blocked message between the was removed and doesn't merge messages with different usernames"
2020-09-29 08:10:01 +02:00
}
2020-02-04 10:22:02 +01:00
}
2020-09-19 20:49:33 +02:00
} ;
2020-11-13 19:47:44 +01:00
2020-10-09 21:09:35 +02:00
return ! window . BDFDB _Global || ( ! window . BDFDB _Global . loaded && ! window . BDFDB _Global . started ) ? class {
2020-09-19 20:49:33 +02:00
getName ( ) { return config . info . name ; }
getAuthor ( ) { return config . info . author ; }
getVersion ( ) { return config . info . version ; }
getDescription ( ) { return config . info . description ; }
2020-10-09 21:09:35 +02: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 ;
BdApi . showConfirmationModal ( "Library Missing" , ` The library plugin needed for ${ config . info . name } is missing. Please click "Download Now" to install it. ` , {
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 ;
require ( "request" ) . get ( "https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js" , ( e , r , b ) => {
2020-10-20 23:25:34 +02:00
if ( ! e && b && b . indexOf ( ` * @name BDFDB ` ) > - 1 ) require ( "fs" ) . writeFile ( require ( "path" ) . join ( BdApi . Plugins . folder , "0BDFDB.plugin.js" ) , b , _ => { } ) ;
2020-09-20 08:15:13 +02:00
else BdApi . alert ( "Error" , "Could not download BDFDB library plugin, try again some time later." ) ;
} ) ;
}
2020-09-19 20:49:33 +02:00
} ) ;
2020-02-04 10:22:02 +01:00
}
2020-09-19 20:49:33 +02:00
if ( ! window . BDFDB _Global . pluginQueue . includes ( config . info . name ) ) window . BDFDB _Global . pluginQueue . push ( config . info . name ) ;
2020-10-09 21:09:35 +02:00
}
start ( ) { this . load ( ) ; }
stop ( ) { }
} : ( ( [ Plugin , BDFDB ] ) => {
2020-10-06 09:40:54 +02:00
var settings = { } ;
2020-10-09 21:09:35 +02:00
return class RemoveBlockedMessages extends Plugin {
2020-09-19 20:49:33 +02:00
onLoad ( ) {
2020-10-06 09:40:54 +02:00
this . defaults = {
settings : {
2020-11-19 16:51:14 +01:00
removeMessages : { value : true , description : "Remove messages of blocked users" } ,
removeUsers : { value : true , description : "Remove blocked users (memberlist, voicechannels)" } ,
disableNotifications : { value : true , description : "Disable unread markers for messages of blocked users" } ,
2020-10-06 09:40:54 +02:00
}
} ;
2020-09-19 20:49:33 +02:00
this . patchedModules = {
2020-10-06 09:40:54 +02:00
before : {
2020-10-29 00:15:33 +01:00
ReactorsComponent : "render" ,
2020-10-15 14:34:57 +02:00
ChannelMembers : "render" ,
2020-10-15 19:14:17 +02:00
PrivateChannelRecipients : "default" ,
2020-10-06 09:40:54 +02:00
VoiceUsers : "render" ,
2020-10-15 19:14:17 +02:00
PrivateChannel : "render" ,
2020-10-06 09:40:54 +02:00
PrivateChannelCallParticipants : "render" ,
ChannelCall : "render" ,
UserSummaryItem : "render"
} ,
2020-09-19 20:49:33 +02:00
after : {
2020-11-20 16:52:10 +01:00
ChannelPins : "default" ,
RecentMentions : "default" ,
2020-10-06 09:40:54 +02:00
Messages : "type" ,
MemberListItem : "render" ,
2020-10-15 19:14:17 +02:00
VoiceUser : "render" ,
Mention : "default"
2020-09-19 20:49:33 +02:00
}
} ;
2020-10-15 19:14:17 +02:00
2020-11-13 21:25:46 +01:00
this . patchPriority = 8 ;
2020-02-04 10:22:02 +01:00
}
2020-09-19 20:49:33 +02:00
onStart ( ) {
2020-09-29 08:10:01 +02:00
BDFDB . PatchUtils . patch ( this , BDFDB . LibraryModules . UnreadChannelUtils , "hasUnread" , { after : e => {
2020-10-06 09:40:54 +02:00
if ( e . returnValue && settings . disableNotifications ) {
2020-09-29 08:10:01 +02:00
let count = BDFDB . LibraryModules . UnreadChannelUtils . getUnreadCount ( e . methodArguments [ 0 ] ) ;
if ( count < BDFDB . DiscordConstants . MAX _MESSAGES _PER _CHANNEL ) {
let id = BDFDB . LibraryModules . UnreadChannelUtils . lastMessageId ( e . methodArguments [ 0 ] ) ;
let message = id && BDFDB . LibraryModules . MessageStore . getMessage ( e . methodArguments [ 0 ] , id ) ;
if ( message && message . blocked ) {
let oldestId = BDFDB . LibraryModules . UnreadChannelUtils . getOldestUnreadMessageId ( e . methodArguments [ 0 ] ) ;
let messages = BDFDB . LibraryModules . MessageStore . getMessages ( e . methodArguments [ 0 ] ) ;
if ( messages && oldestId ) {
let index = messages . _array . indexOf ( messages . _array . find ( c => c . id == oldestId ) ) ;
if ( index > - 1 ) return messages . _array . slice ( index ) . some ( c => ! c . blocked ) ;
}
}
}
}
} } ) ;
BDFDB . PatchUtils . patch ( this , BDFDB . LibraryModules . UnreadGuildUtils , "hasUnread" , { after : e => {
2020-10-06 09:40:54 +02:00
if ( e . returnValue && settings . disableNotifications ) {
2020-09-29 08:10:01 +02:00
return BDFDB . LibraryModules . GuildChannelStore . getChannels ( e . methodArguments [ 0 ] ) . SELECTABLE . map ( n => n . channel && n . channel . id ) . filter ( n => n && n != "null" ) . some ( BDFDB . LibraryModules . UnreadChannelUtils . hasUnread ) ;
}
} } ) ;
2020-09-29 08:10:27 +02:00
2020-10-15 19:14:17 +02:00
if ( BDFDB . LibraryModules . AutocompleteOptions && BDFDB . LibraryModules . AutocompleteOptions . AUTOCOMPLETE _OPTIONS ) BDFDB . PatchUtils . patch ( this , BDFDB . LibraryModules . AutocompleteOptions . AUTOCOMPLETE _OPTIONS . MENTIONS , "queryResults" , { after : e => {
if ( settings . removeUsers ) e . returnValue . users = e . returnValue . users . filter ( n => ! n . user || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . user . id ) ) ;
} } ) ;
2020-11-13 21:25:46 +01:00
let muteTimeout ;
let channelId = BDFDB . LibraryModules . CurrentVoiceUtils . getChannelId ( ) ;
let connectedUsers = BDFDB . ObjectUtils . filter ( BDFDB . LibraryModules . VoiceUtils . getVoiceStates ( BDFDB . LibraryModules . CurrentVoiceUtils . getGuildId ( ) ) , n => n && n . channelId == channelId && ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . userId ) ) ;
BDFDB . PatchUtils . patch ( this , BDFDB . LibraryModules . SoundUtils , "playSound" , { instead : e => {
let type = e . methodArguments [ 0 ] ;
if ( settings . removeUsers && type == "user_join" || type == "user_leave" || type == "user_moved" ) {
channelId = BDFDB . LibraryModules . CurrentVoiceUtils . getChannelId ( ) ;
let allConnectedUsers = BDFDB . ObjectUtils . filter ( BDFDB . LibraryModules . VoiceUtils . getVoiceStates ( BDFDB . LibraryModules . CurrentVoiceUtils . getGuildId ( ) ) , n => n && n . channelId == channelId ) ;
let unblockedUsers = BDFDB . ObjectUtils . filter ( allConnectedUsers , n => n && ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . userId ) ) ;
let unmutedBlockedUsers = BDFDB . ObjectUtils . toArray ( allConnectedUsers ) . filter ( n => n && BDFDB . LibraryModules . FriendUtils . isBlocked ( n . userId ) && ! BDFDB . LibraryModules . MediaDeviceUtils . isLocalMute ( n . userId ) ) ;
if ( unmutedBlockedUsers . length ) {
BDFDB . TimeUtils . clear ( muteTimeout ) ;
muteTimeout = BDFDB . TimeUtils . timeout ( _ => {
while ( unmutedBlockedUsers . length ) BDFDB . LibraryModules . MediaDeviceSetUtils . toggleLocalMute ( unmutedBlockedUsers . pop ( ) . userId ) ;
} , 1000 ) ;
}
if ( Object . keys ( unblockedUsers ) . length == Object . keys ( connectedUsers ) . length ) {
e . stopOriginalMethodCall ( ) ;
e . methodArguments [ 0 ] = null ;
}
2020-11-16 17:02:03 +01:00
else e . callOriginalMethodAfterwards ( ) ;
2020-11-13 21:25:46 +01:00
connectedUsers = unblockedUsers ;
}
else e . callOriginalMethodAfterwards ( ) ;
} } ) ;
2020-10-06 09:40:54 +02:00
this . forceUpdateAll ( ) ;
2020-09-19 20:49:33 +02:00
}
onStop ( ) {
2020-10-06 09:40:54 +02:00
this . forceUpdateAll ( ) ;
}
getSettingsPanel ( collapseStates = { } ) {
let settingsPanel , settingsItems = [ ] ;
for ( let key in settings ) settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSaveItem , {
type : "Switch" ,
plugin : this ,
keys : [ "settings" , key ] ,
label : this . defaults . settings [ key ] . description ,
value : settings [ key ]
} ) ) ;
return settingsPanel = BDFDB . PluginUtils . createSettingsPanel ( this , settingsItems ) ;
}
onSettingsClosed ( ) {
if ( this . SettingsUpdated ) {
delete this . SettingsUpdated ;
this . forceUpdateAll ( ) ;
}
}
forceUpdateAll ( ) {
settings = BDFDB . DataUtils . get ( this , "settings" ) ;
BDFDB . PatchUtils . forceAllUpdates ( this ) ;
2020-08-15 14:24:48 +02:00
BDFDB . MessageUtils . rerenderAll ( ) ;
2020-02-04 10:22:02 +01:00
}
2020-09-19 20:49:33 +02:00
processMessages ( e ) {
2020-10-06 09:40:54 +02:00
if ( settings . removeMessages ) {
let messagesIns = e . returnvalue . props . children ;
if ( BDFDB . ArrayUtils . is ( messagesIns . props . channelStream ) ) {
2020-11-13 21:25:46 +01:00
let oldStream = messagesIns . props . channelStream . filter ( n => n . type != "MESSAGE_GROUP_BLOCKED" ) , newStream = [ ] ;
2020-10-06 09:40:54 +02:00
for ( let i in oldStream ) {
let next = parseInt ( i ) + 1 ;
2020-11-13 21:25:46 +01:00
if ( oldStream [ i ] . type != "DIVIDER" || ( oldStream [ next ] && oldStream [ i ] . type == "DIVIDER" && oldStream [ next ] . type != "DIVIDER" && oldStream . slice ( next ) . some ( nextStream => nextStream . type != "DIVIDER" ) ) ) newStream . push ( oldStream [ i ] ) ;
}
2020-11-17 19:04:03 +01:00
let groupId , author ;
2020-11-13 21:25:46 +01:00
for ( let i in newStream ) {
if ( newStream [ i ] . type == "MESSAGE" && newStream [ i ] . content . type == BDFDB . DiscordConstants . MessageTypes . DEFAULT && groupId != newStream [ i ] . groupId ) {
2020-11-17 19:04:03 +01:00
if ( author && author . id == newStream [ i ] . content . author . id && author . username == newStream [ i ] . content . author . username ) newStream [ i ] = Object . assign ( { } , newStream [ i ] , { groupId : groupId } ) ;
author = newStream [ i ] . content . author ;
2020-11-13 21:25:46 +01:00
}
2020-11-17 19:04:03 +01:00
else author = null ; ;
2020-11-13 21:25:46 +01:00
groupId = newStream [ i ] . groupId ;
2020-10-06 09:40:54 +02:00
}
2020-11-13 21:25:46 +01:00
messagesIns . props . channelStream = newStream ;
2020-10-06 09:40:54 +02:00
}
if ( BDFDB . ObjectUtils . is ( messagesIns . props . messages ) && BDFDB . ArrayUtils . is ( messagesIns . props . messages . _array ) ) {
let messages = messagesIns . props . messages ;
messagesIns . props . messages = new BDFDB . DiscordObjects . Messages ( messages ) ;
for ( let key in messages ) messagesIns . props . messages [ key ] = messages [ key ] ;
2020-10-15 19:14:17 +02:00
messagesIns . props . messages . _array = [ ] . concat ( messagesIns . props . messages . _array . filter ( n => ! n . author || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . author . id ) ) ) ;
2020-10-06 09:40:54 +02:00
if ( messagesIns . props . oldestUnreadMessageId && messagesIns . props . messages . _array . every ( n => n . id != messagesIns . props . oldestUnreadMessageId ) ) messagesIns . props . oldestUnreadMessageId = null ;
2020-09-19 20:49:33 +02:00
}
2020-09-16 16:20:38 +02:00
}
2020-02-04 10:22:02 +01:00
}
2020-10-06 09:40:54 +02:00
2020-11-20 16:52:10 +01:00
processChannelPins ( e ) {
if ( settings . removeMessages && e . returnvalue . props && e . returnvalue . props . children && e . returnvalue . props . children . props && BDFDB . ArrayUtils . is ( e . returnvalue . props . children . props . messages ) ) e . returnvalue . props . children . props . messages = e . returnvalue . props . children . props . messages . filter ( n => ! n || ! n . author || ! n . author . id || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . author . id ) ) ;
}
processRecentMentions ( e ) {
if ( settings . removeMessages && BDFDB . ArrayUtils . is ( e . returnvalue . props . messages ) ) e . returnvalue . props . messages = e . returnvalue . props . messages . filter ( n => ! n || ! n . author || ! n . author . id || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . author . id ) ) ;
2020-10-29 00:15:33 +01:00
}
processReactorsComponent ( e ) {
if ( settings . removeUsers && BDFDB . ArrayUtils . is ( e . instance . props . reactors ) ) e . instance . props . reactors = e . instance . props . reactors . filter ( n => ! n || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . id ) ) ;
2020-10-19 10:38:04 +02:00
}
2020-10-15 14:34:57 +02:00
processChannelMembers ( e ) {
if ( settings . removeUsers ) {
e . instance . props . groups = [ ] . concat ( e . instance . props . groups ) ;
2020-10-17 14:47:13 +02:00
e . instance . props . rows = [ ] . concat ( e . instance . props . rows ) ;
let newRows = [ ] , newGroups = [ ] ;
2020-10-15 14:34:57 +02:00
for ( let i in e . instance . props . rows ) {
let row = e . instance . props . rows [ i ] ;
if ( row . type != "MEMBER" ) newRows . push ( row ) ;
else if ( ! row . user || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( row . user . id ) ) newRows . push ( row ) ;
else {
2020-10-17 14:47:13 +02:00
let found = false , rowIndex = i - 1 ;
while ( ! found && rowIndex > - 1 ) {
if ( newRows [ rowIndex ] && newRows [ rowIndex ] . type == "GROUP" ) {
2020-10-15 14:34:57 +02:00
found = true ;
2020-10-17 14:47:13 +02:00
let groupIndex = e . instance . props . groups . findIndex ( r => r . id == newRows [ rowIndex ] . id ) ;
if ( groupIndex ) {
e . instance . props . groups [ groupIndex ] = Object . assign ( { } , e . instance . props . groups [ groupIndex ] , { count : e . instance . props . groups [ groupIndex ] . count - 1 } ) ;
newRows [ rowIndex ] = Object . assign ( { } , newRows [ rowIndex ] , { count : e . instance . props . groups [ groupIndex ] . count } ) ;
}
2020-10-15 14:34:57 +02:00
}
2020-10-17 14:47:13 +02:00
else rowIndex -- ;
2020-10-15 14:34:57 +02:00
}
}
}
let indexSum = 0 ;
for ( let i in e . instance . props . groups ) {
2020-10-17 14:47:13 +02:00
newGroups [ i ] = Object . assign ( { } , e . instance . props . groups [ i ] , { index : indexSum } ) ;
if ( e . instance . props . groups [ i ] . count > 0 ) indexSum += ( e . instance . props . groups [ i ] . count + 1 ) ;
2020-10-15 14:34:57 +02:00
}
2020-10-17 14:47:13 +02:00
e . instance . props . groups = newGroups . filter ( g => g && g . count > 0 ) ;
e . instance . props . rows = newRows . filter ( r => r && ( r . type != "GROUP" || r . count > 0 ) ) ;
2020-10-15 14:34:57 +02:00
}
2020-10-06 09:40:54 +02:00
}
2020-10-15 14:34:57 +02:00
2020-10-15 19:14:17 +02:00
processPrivateChannelRecipients ( e ) {
if ( settings . removeUsers && e . instance . props . channel && e . instance . props . channel . isGroupDM ( ) ) e . instance . props . channel = new BDFDB . DiscordObjects . Channel ( Object . assign ( { } , e . instance . props . channel , { rawRecipients : e . instance . props . channel . rawRecipients . filter ( n => ! n || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . id ) ) , recipients : e . instance . props . channel . recipients . filter ( id => ! id || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( id ) ) } ) ) ;
}
2020-10-15 14:34:57 +02:00
processMemberListItem ( e ) {
2020-10-06 09:40:54 +02:00
if ( settings . removeUsers && e . instance . props . user && BDFDB . LibraryModules . FriendUtils . isBlocked ( e . instance . props . user . id ) ) return null ;
}
processVoiceUsers ( e ) {
if ( settings . removeUsers && BDFDB . ArrayUtils . is ( e . instance . props . voiceStates ) ) e . instance . props . voiceStates = [ ] . concat ( e . instance . props . voiceStates ) . filter ( n => ! n . user || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . user . id ) ) ;
}
2020-10-15 14:34:57 +02:00
processVoiceUser ( e ) {
if ( settings . removeUsers && e . instance . props . user && BDFDB . LibraryModules . FriendUtils . isBlocked ( e . instance . props . user . id ) ) return null ;
}
2020-10-06 09:40:54 +02:00
2020-10-15 19:14:17 +02:00
processPrivateChannel ( e ) {
if ( settings . removeUsers && e . instance . props . channel && e . instance . props . channel . isGroupDM ( ) ) e . instance . props . channel = new BDFDB . DiscordObjects . Channel ( Object . assign ( { } , e . instance . props . channel , { rawRecipients : e . instance . props . channel . rawRecipients . filter ( n => ! n || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . id ) ) , recipients : e . instance . props . channel . recipients . filter ( id => ! id || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( id ) ) } ) ) ;
}
2020-10-06 09:40:54 +02:00
processPrivateChannelCallParticipants ( e ) {
if ( settings . removeUsers && BDFDB . ArrayUtils . is ( e . instance . props . participants ) ) e . instance . props . participants = [ ] . concat ( e . instance . props . participants ) . filter ( n => ! n . user || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . user . id ) ) ;
}
processChannelCall ( e ) {
if ( settings . removeUsers && BDFDB . ArrayUtils . is ( e . instance . props . participants ) ) e . instance . props . participants = [ ] . concat ( e . instance . props . participants ) . filter ( n => ! n . user || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . user . id ) ) ;
}
processUserSummaryItem ( e ) {
2020-10-15 14:34:57 +02:00
if ( settings . removeUsers && BDFDB . ArrayUtils . is ( e . instance . props . users ) ) e . instance . props . users = [ ] . concat ( e . instance . props . users ) . filter ( n => ! n || ! BDFDB . LibraryModules . FriendUtils . isBlocked ( n . id ) ) ;
2020-10-06 09:40:54 +02:00
}
2020-10-15 19:14:17 +02:00
processMention ( e ) {
if ( settings . removeUsers && e . instance . props . userId && BDFDB . LibraryModules . FriendUtils . isBlocked ( e . instance . props . userId ) ) return BDFDB . ReactUtils . createElement ( "span" , {
className : BDFDB . disCNS . mention + BDFDB . disCN . mentionwrapper ,
children : [ "@" + BDFDB _Global . LanguageUtils . LanguageStrings . UNKNOWN _USER ]
} ) ;
}
2020-09-19 20:49:33 +02:00
} ;
2020-10-09 21:09:35 +02:00
} ) ( window . BDFDB _Global . PluginUtils . buildPlugin ( config ) ) ;
2020-09-19 20:49:33 +02:00
} ) ( ) ;