Skip to content

Commit 804688b

Browse files
committed
misc 更新 ADVNICKNAME 命令
1 parent 65cd223 commit 804688b

File tree

2 files changed

+75
-35
lines changed

2 files changed

+75
-35
lines changed

ASFEnhance/ASFEnhance.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,12 @@ public Task OnLoaded()
551551
case "RGA" when access >= EAccess.Master:
552552
return await Profile.Command.ResponseSetProfileGameAvatar(Utilities.GetArgsAsText(args, 1, ","), null, null).ConfigureAwait(false);
553553

554-
case "RENAME" when argLength == 2 && access >= EAccess.Owner:
555-
case "RENAME" when access >= EAccess.Owner:
556-
return await Profile.Command.ResponseRename(args[1], Utilities.GetArgsAsText(message, 2)).ConfigureAwait(false);
554+
case "ADVNICKNAME" when argLength > 2 && access >= EAccess.Master:
555+
case "ANN" when argLength > 2 && access >= EAccess.Master:
556+
return await Profile.Command.ResponseAdvNickName(args[1], Utilities.GetArgsAsText(message, 2)).ConfigureAwait(false);
557+
case "ADVNICKNAME" when access >= EAccess.Master:
558+
case "ANN" when access >= EAccess.Master:
559+
return await Profile.Command.ResponseAdvNickName(bot, args[1]).ConfigureAwait(false);
557560

558561
case "REPLAY" when access >= EAccess.Operator:
559562
case "RP" when access >= EAccess.Operator:

ASFEnhance/Profile/Command.cs

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
#pragma warning disable CS8632 // 只能在 "#nullable" 注释上下文内的代码中使用可为 null 的引用类型的注释。
22

33
using AngleSharp.Common;
4+
using AngleSharp.Text;
45
using ArchiSteamFarm.Core;
56
using ArchiSteamFarm.Localization;
67
using ArchiSteamFarm.Steam;
78
using ASFEnhance.Localization;
9+
using System.Text;
810
using System.Text.RegularExpressions;
911
using static ASFEnhance.Utils;
1012

1113

1214
namespace ASFEnhance.Profile
1315
{
14-
internal static class Command
16+
internal static partial class Command
1517
{
1618
/// <summary>
1719
/// 获取个人资料摘要
@@ -45,7 +47,7 @@ internal static class Command
4547

4648
HashSet<Bot>? bots = Bot.GetBots(botNames);
4749

48-
if ((bots == null) || (bots.Count == 0))
50+
if (bots == null || bots.Count == 0)
4951
{
5052
return FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
5153
}
@@ -87,7 +89,7 @@ internal static class Command
8789

8890
HashSet<Bot>? bots = Bot.GetBots(botNames);
8991

90-
if ((bots == null) || (bots.Count == 0))
92+
if (bots == null || bots.Count == 0)
9193
{
9294
return FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
9395
}
@@ -132,7 +134,7 @@ internal static class Command
132134

133135
HashSet<Bot>? bots = Bot.GetBots(botNames);
134136

135-
if ((bots == null) || (bots.Count == 0))
137+
if (bots == null || bots.Count == 0)
136138
{
137139
return FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
138140
}
@@ -176,7 +178,7 @@ internal static class Command
176178

177179
HashSet<Bot>? bots = Bot.GetBots(botNames);
178180

179-
if ((bots == null) || (bots.Count == 0))
181+
if (bots == null || bots.Count == 0)
180182
{
181183
return FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
182184
}
@@ -220,7 +222,7 @@ internal static class Command
220222

221223
HashSet<Bot>? bots = Bot.GetBots(botNames);
222224

223-
if ((bots == null) || (bots.Count == 0))
225+
if (bots == null || bots.Count == 0)
224226
{
225227
return FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
226228
}
@@ -264,7 +266,7 @@ internal static class Command
264266

265267
HashSet<Bot>? bots = Bot.GetBots(botNames);
266268

267-
if ((bots == null) || (bots.Count == 0))
269+
if (bots == null || bots.Count == 0)
268270
{
269271
return FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
270272
}
@@ -320,7 +322,7 @@ internal static class Command
320322

321323
HashSet<Bot>? bots = Bot.GetBots(botNames);
322324

323-
if ((bots == null) || (bots.Count == 0))
325+
if (bots == null || bots.Count == 0)
324326
{
325327
return FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
326328
}
@@ -390,7 +392,7 @@ internal static class Command
390392

391393
HashSet<Bot>? bots = Bot.GetBots(botNames);
392394

393-
if ((bots == null) || (bots.Count == 0))
395+
if (bots == null || bots.Count == 0)
394396
{
395397
return FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
396398
}
@@ -484,7 +486,7 @@ internal static class Command
484486

485487
HashSet<Bot>? bots = Bot.GetBots(botNames);
486488

487-
if ((bots == null) || (bots.Count == 0))
489+
if (bots == null || bots.Count == 0)
488490
{
489491
return FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
490492
}
@@ -496,49 +498,84 @@ internal static class Command
496498
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
497499
}
498500

501+
[GeneratedRegex("%(?:(l|u|d|bot)(\\d*))%")]
502+
private static partial Regex MatchVariables();
503+
499504
/// <summary>
500505
/// 高级重命名命令
501506
/// </summary>
502507
/// <param name="bot"></param>
503-
/// <param name="nickname"></param>
508+
/// <param name="query"></param>
504509
/// <returns></returns>
505-
internal static async Task<string?> ResponseRename(Bot bot, string nickname)
510+
internal static async Task<string?> ResponseAdvNickName(Bot bot, string query)
506511
{
507512
if (!bot.IsConnectedAndLoggedOn)
508513
{
509514
return bot.FormatBotResponse(Strings.BotNotConnected);
510515
}
511516

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)
517520
{
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)
520525
{
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);
522564
}
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);
529565
}
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;
532569
}
533570

534571
/// <summary>
535572
/// 高级重命名命令 (多个Bot)
536573
/// </summary>
537574
/// <param name="botNames"></param>
538-
/// <param name="nickname"></param>
575+
/// <param name="query"></param>
539576
/// <returns></returns>
540577
/// <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)
542579
{
543580
if (string.IsNullOrEmpty(botNames))
544581
{
@@ -547,12 +584,12 @@ internal static class Command
547584

548585
HashSet<Bot>? bots = Bot.GetBots(botNames);
549586

550-
if ((bots == null) || (bots.Count == 0))
587+
if (bots == null || bots.Count == 0)
551588
{
552589
return FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
553590
}
554591

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);
556593

557594
List<string> responses = new(results.Where(result => !string.IsNullOrEmpty(result))!);
558595

0 commit comments

Comments
 (0)