@@ -103,15 +103,6 @@ export default class LinkedAccounts extends BotComponent {
103103 return user_id ;
104104 }
105105
106- async get_all_accounts_in_group ( main_account : string ) : Promise < Set < string > > {
107- const alts = await this . database . linked_accounts . find ( { main_account } ) . toArray ( ) ;
108- const accounts = new Set < string > ( [ main_account ] ) ;
109- for ( const alt of alts ) {
110- accounts . add ( alt . alt_account ) ;
111- }
112- return accounts ;
113- }
114-
115106 async get_all_linked_accounts ( user_id : string ) : Promise < Set < string > > {
116107 const main_account = await this . find_main_account ( user_id ) ;
117108 const alts = await this . database . linked_accounts . find ( { main_account } ) . toArray ( ) ;
@@ -133,23 +124,6 @@ export default class LinkedAccounts extends BotComponent {
133124 return count > 0 || main_account !== user_id ;
134125 }
135126
136- create_entry (
137- main_account : string ,
138- alt_account : string ,
139- moderator_id : string ,
140- moderator_name : string ,
141- context : string | null ,
142- ) : linked_accounts_entry {
143- return {
144- main_account,
145- alt_account,
146- added_by : moderator_id ,
147- added_by_name : moderator_name ,
148- added_at : Date . now ( ) ,
149- context : context ?? undefined ,
150- } ;
151- }
152-
153127 async add_link ( command : TextBasedCommand , user : Discord . User , alt : Discord . User , context : string | null ) {
154128 if ( user . id === alt . id ) {
155129 await command . reply ( {
@@ -161,31 +135,35 @@ export default class LinkedAccounts extends BotComponent {
161135 } ) ;
162136 return ;
163137 }
164- const user_main = await this . find_main_account ( user . id ) ;
165- const alt_main = await this . find_main_account ( alt . id ) ;
166- if ( user_main === alt_main ) {
167- await command . reply ( {
168- embeds : [
169- new Discord . EmbedBuilder ( )
170- . setColor ( colors . alert_color )
171- . setDescription ( `${ this . wheatley . emoji . error } These accounts are already linked` ) ,
172- ] ,
173- } ) ;
174- return ;
175- }
176- const user_group = await this . get_all_accounts_in_group ( user_main ) ;
177- const alt_group = await this . get_all_accounts_in_group ( alt_main ) ;
178- const all_accounts = new Set ( [ ...user_group , ...alt_group ] ) ;
179138 const moderator = await command . get_member ( ) ;
180- const entries = Array . from ( all_accounts )
181- . filter ( id => id !== user_main )
182- . map ( id => this . create_entry ( user_main , id , command . user . id , moderator . displayName , context ) ) ;
183139 await this . wheatley . database . lock ( ) ;
184140 try {
185- await this . database . linked_accounts . deleteMany ( {
186- main_account : { $in : [ user_main , alt_main ] } ,
141+ const [ user_main , alt_main ] = await Promise . all ( [
142+ this . find_main_account ( user . id ) ,
143+ this . find_main_account ( alt . id ) ,
144+ ] ) ;
145+ if ( user_main === alt_main ) {
146+ await command . reply ( {
147+ embeds : [
148+ new Discord . EmbedBuilder ( )
149+ . setColor ( colors . alert_color )
150+ . setDescription ( `${ this . wheatley . emoji . error } These accounts are already linked` ) ,
151+ ] ,
152+ } ) ;
153+ return ;
154+ }
155+ await this . database . linked_accounts . updateMany (
156+ { main_account : alt_main } ,
157+ { $set : { main_account : user_main } } ,
158+ ) ;
159+ await this . database . linked_accounts . insertOne ( {
160+ main_account : user_main ,
161+ alt_account : alt_main ,
162+ added_by : command . user . id ,
163+ added_by_name : moderator . displayName ,
164+ added_at : Date . now ( ) ,
165+ context : context ?? undefined ,
187166 } ) ;
188- await this . database . linked_accounts . insertMany ( entries ) ;
189167 } finally {
190168 this . wheatley . database . unlock ( ) ;
191169 }
0 commit comments