1
1
#pragma warning disable CS8632 // 只能在 "#nullable" 注释上下文内的代码中使用可为 null 的引用类型的注释。
2
2
3
3
using AngleSharp . Common ;
4
+ using AngleSharp . Text ;
4
5
using ArchiSteamFarm . Core ;
5
6
using ArchiSteamFarm . Localization ;
6
7
using ArchiSteamFarm . Steam ;
7
8
using ASFEnhance . Localization ;
9
+ using System . Text ;
8
10
using System . Text . RegularExpressions ;
9
11
using static ASFEnhance . Utils ;
10
12
11
13
12
14
namespace ASFEnhance . Profile
13
15
{
14
- internal static class Command
16
+ internal static partial class Command
15
17
{
16
18
/// <summary>
17
19
/// 获取个人资料摘要
@@ -45,7 +47,7 @@ internal static class Command
45
47
46
48
HashSet < Bot > ? bots = Bot . GetBots ( botNames ) ;
47
49
48
- if ( ( bots == null ) || ( bots . Count == 0 ) )
50
+ if ( bots == null || bots . Count == 0 )
49
51
{
50
52
return FormatStaticResponse ( string . Format ( Strings . BotNotFound , botNames ) ) ;
51
53
}
@@ -87,7 +89,7 @@ internal static class Command
87
89
88
90
HashSet < Bot > ? bots = Bot . GetBots ( botNames ) ;
89
91
90
- if ( ( bots == null ) || ( bots . Count == 0 ) )
92
+ if ( bots == null || bots . Count == 0 )
91
93
{
92
94
return FormatStaticResponse ( string . Format ( Strings . BotNotFound , botNames ) ) ;
93
95
}
@@ -132,7 +134,7 @@ internal static class Command
132
134
133
135
HashSet < Bot > ? bots = Bot . GetBots ( botNames ) ;
134
136
135
- if ( ( bots == null ) || ( bots . Count == 0 ) )
137
+ if ( bots == null || bots . Count == 0 )
136
138
{
137
139
return FormatStaticResponse ( string . Format ( Strings . BotNotFound , botNames ) ) ;
138
140
}
@@ -176,7 +178,7 @@ internal static class Command
176
178
177
179
HashSet < Bot > ? bots = Bot . GetBots ( botNames ) ;
178
180
179
- if ( ( bots == null ) || ( bots . Count == 0 ) )
181
+ if ( bots == null || bots . Count == 0 )
180
182
{
181
183
return FormatStaticResponse ( string . Format ( Strings . BotNotFound , botNames ) ) ;
182
184
}
@@ -220,7 +222,7 @@ internal static class Command
220
222
221
223
HashSet < Bot > ? bots = Bot . GetBots ( botNames ) ;
222
224
223
- if ( ( bots == null ) || ( bots . Count == 0 ) )
225
+ if ( bots == null || bots . Count == 0 )
224
226
{
225
227
return FormatStaticResponse ( string . Format ( Strings . BotNotFound , botNames ) ) ;
226
228
}
@@ -264,7 +266,7 @@ internal static class Command
264
266
265
267
HashSet < Bot > ? bots = Bot . GetBots ( botNames ) ;
266
268
267
- if ( ( bots == null ) || ( bots . Count == 0 ) )
269
+ if ( bots == null || bots . Count == 0 )
268
270
{
269
271
return FormatStaticResponse ( string . Format ( Strings . BotNotFound , botNames ) ) ;
270
272
}
@@ -320,7 +322,7 @@ internal static class Command
320
322
321
323
HashSet < Bot > ? bots = Bot . GetBots ( botNames ) ;
322
324
323
- if ( ( bots == null ) || ( bots . Count == 0 ) )
325
+ if ( bots == null || bots . Count == 0 )
324
326
{
325
327
return FormatStaticResponse ( string . Format ( Strings . BotNotFound , botNames ) ) ;
326
328
}
@@ -390,7 +392,7 @@ internal static class Command
390
392
391
393
HashSet < Bot > ? bots = Bot . GetBots ( botNames ) ;
392
394
393
- if ( ( bots == null ) || ( bots . Count == 0 ) )
395
+ if ( bots == null || bots . Count == 0 )
394
396
{
395
397
return FormatStaticResponse ( string . Format ( Strings . BotNotFound , botNames ) ) ;
396
398
}
@@ -484,7 +486,7 @@ internal static class Command
484
486
485
487
HashSet < Bot > ? bots = Bot . GetBots ( botNames ) ;
486
488
487
- if ( ( bots == null ) || ( bots . Count == 0 ) )
489
+ if ( bots == null || bots . Count == 0 )
488
490
{
489
491
return FormatStaticResponse ( string . Format ( Strings . BotNotFound , botNames ) ) ;
490
492
}
@@ -496,49 +498,84 @@ internal static class Command
496
498
return responses . Count > 0 ? string . Join ( Environment . NewLine , responses ) : null ;
497
499
}
498
500
501
+ [ GeneratedRegex ( "%(?:(l|u|d|bot)(\\ d*))%" ) ]
502
+ private static partial Regex MatchVariables ( ) ;
503
+
499
504
/// <summary>
500
505
/// 高级重命名命令
501
506
/// </summary>
502
507
/// <param name="bot"></param>
503
- /// <param name="nickname "></param>
508
+ /// <param name="query "></param>
504
509
/// <returns></returns>
505
- internal static async Task < string ? > ResponseRename ( Bot bot , string nickname )
510
+ internal static async Task < string ? > ResponseAdvNickName ( Bot bot , string query )
506
511
{
507
512
if ( ! bot . IsConnectedAndLoggedOn )
508
513
{
509
514
return bot . FormatBotResponse ( Strings . BotNotConnected ) ;
510
515
}
511
516
512
- Random rd = new ( ) ;
513
- // Logic borrowed from old plugin https://github.com/Zignixx/ASF-RenamePlugin/blob/master/RenamePlugin.cs#L28
514
- Regex regexRandom = new Regex ( @"%RANDOM(\d+)%" ) ;
515
- Match match = regexRandom . Match ( nickname ) ;
516
- if ( match . Success )
517
+ Regex matchVariable = MatchVariables ( ) ;
518
+ var matches = matchVariable . Matches ( query . ToLowerInvariant ( ) ) ;
519
+ if ( matches ? . Count > 0 )
517
520
{
518
- double maxRangeUserInput = double . Parse ( match . Groups [ 1 ] . Value ) ;
519
- if ( maxRangeUserInput > 9 )
521
+ Random rand = new ( ) ;
522
+ Queue < string > replaceMent = new ( ) ;
523
+
524
+ foreach ( Match match in matches )
520
525
{
521
- return bot . FormatBotResponse ( Langs . RenameTooBigRandomNumber ) ;
526
+ StringBuilder sb = new ( ) ;
527
+
528
+ string flag = match . Groups [ 1 ] . Value ;
529
+ string strCount = match . Groups [ 2 ] . Value ;
530
+
531
+ if ( ! uint . TryParse ( strCount , out uint count ) )
532
+ {
533
+ count = 1 ;
534
+ }
535
+
536
+ while ( count -- > 0 )
537
+ {
538
+ if ( flag == "bot" )
539
+ {
540
+ sb . Append ( bot . BotName ) ;
541
+ }
542
+ else
543
+ {
544
+ char ? str = flag switch {
545
+ "l" => ( char ) rand . Next ( 97 , 123 ) ,
546
+ "u" => ( char ) rand . Next ( 65 , 91 ) ,
547
+ "d" => ( char ) rand . Next ( 48 , 58 ) ,
548
+ _ => null ,
549
+ } ;
550
+ sb . Append ( str ) ;
551
+ }
552
+ }
553
+ replaceMent . Enqueue ( sb . ToString ( ) ) ;
554
+ }
555
+
556
+ foreach ( Match match in matches )
557
+ {
558
+ if ( replaceMent . Count == 0 )
559
+ {
560
+ break ;
561
+ }
562
+ string replace = replaceMent . Dequeue ( ) ;
563
+ query = query . ReplaceFirst ( match . Value , replace ) ;
522
564
}
523
- int randomNumber = rd . Next ( 0 , Convert . ToInt32 ( Math . Pow ( 10 , maxRangeUserInput ) - 1 ) ) ;
524
- nickname = Regex . Replace ( nickname , regexRandom . ToString ( ) , randomNumber . ToString ( $ "D{ maxRangeUserInput } ") ) ;
525
- }
526
- if ( new Regex ( "%BOTNAME%" ) . Match ( nickname ) . Success )
527
- {
528
- nickname = Regex . Replace ( nickname , @"%BOTNAME%" , bot . BotName ) ;
529
565
}
530
- string ? result = await bot . Commands . Response ( EAccess . Owner , $ "nickname { bot . BotName } { nickname } ") . ConfigureAwait ( false ) ;
531
- return result ?? bot . FormatBotResponse ( Langs . NetworkError ) ;
566
+
567
+ string ? result = await bot . Commands . Response ( EAccess . Master , $ "NICKNAME { bot . BotName } { query } ") . ConfigureAwait ( false ) ;
568
+ return result ;
532
569
}
533
570
534
571
/// <summary>
535
572
/// 高级重命名命令 (多个Bot)
536
573
/// </summary>
537
574
/// <param name="botNames"></param>
538
- /// <param name="nickname "></param>
575
+ /// <param name="query "></param>
539
576
/// <returns></returns>
540
577
/// <exception cref="ArgumentNullException"></exception>
541
- internal static async Task < string ? > ResponseRename ( string botNames , string nickname )
578
+ internal static async Task < string ? > ResponseAdvNickName ( string botNames , string query )
542
579
{
543
580
if ( string . IsNullOrEmpty ( botNames ) )
544
581
{
@@ -547,12 +584,12 @@ internal static class Command
547
584
548
585
HashSet < Bot > ? bots = Bot . GetBots ( botNames ) ;
549
586
550
- if ( ( bots == null ) || ( bots . Count == 0 ) )
587
+ if ( bots == null || bots . Count == 0 )
551
588
{
552
589
return FormatStaticResponse ( string . Format ( Strings . BotNotFound , botNames ) ) ;
553
590
}
554
591
555
- IList < string ? > results = await Utilities . InParallel ( bots . Select ( bot => ResponseRename ( bot , nickname ) ) ) . ConfigureAwait ( false ) ;
592
+ IList < string ? > results = await Utilities . InParallel ( bots . Select ( bot => ResponseAdvNickName ( bot , query ) ) ) . ConfigureAwait ( false ) ;
556
593
557
594
List < string > responses = new ( results . Where ( result => ! string . IsNullOrEmpty ( result ) ) ! ) ;
558
595
0 commit comments