示例#1
0
        public async Task RunTestCommandAsync(CommandContext ctx,
                                              [Description("Donor run to test as")]
                                              int donorRun = 0,

                                              [Description("Ignore owner comparison checks?")]
                                              bool ignoreOwner = false,

                                              [Description("Ignore cache comparison checks?")]
                                              bool ignoreCache = false)
        {
            List <DevelopmentStressTestChannel>?data = await DevelopmentStressTestDataManager.GetDataAsync();

            if (data is null)
            {
                await RespondError("No saved dataset to run a test on.");

                return;
            }

            await RespondSuccess("Starting Test Run...");

            Stopwatch timer = new();

            timer.Start();

            await this._sender.ExecuteAsync(new()
            {
                DevelopmentStressTest = true,
                DonorRun         = donorRun,
                IgnoreOwnerMatch = ignoreOwner,
                IgnoreCacheMatch = ignoreCache
            });

            await RespondSuccess($"Completed {timer.Elapsed}");

            timer.Stop();
        }
        public async Task SetupTestServers(CommandContext ctx, params DiscordGuild[] guilds)
        {
            List <string> names = new();

            foreach (DiscordGuild?g in guilds)
            {
                names.Add(g.Name);
            }

            DSharpPlus.Interactivity.InteractivityExtension?interact = ctx.Client.GetInteractivity();

            DiscordEmbedBuilder?embed = new DiscordEmbedBuilder()
                                        .WithColor(DiscordColor.Red)
                                        .WithTitle("Partner Test Server Setup")
                                        .WithDescription("Setting up Partner Bot Test Server. Please ensure the bot has admin on all servers. Type `continue` to start.\n\n" +
                                                         "**THIS WILL DELETE ALL CONTENT FROM THE FOLLOWING GUILDS:**")
                                        .AddField("Guilds: ", string.Join(",", names));

            DiscordMessage?message = await ctx.RespondAsync(embed);

            DSharpPlus.Interactivity.InteractivityResult <DiscordMessage> response = await interact.WaitForMessageAsync(x => x.Channel.Id == ctx.Channel.Id && x.Author.Id == ctx.Member.Id);

            if (response.TimedOut)
            {
                await InteractTimeout();

                return;
            }
            else if (!response.Result.Content.ToLower().Equals("continue"))
            {
                await RespondError("Aborting...");

                return;
            }

            var queue = new Queue <DiscordGuild>(guilds);

            List <DevelopmentStressTestChannel>?data = await DevelopmentStressTestDataManager.GetDataAsync();

            if (data is null)
            {
                data = new();
            }

            DiscordMessage?msg = null;

            while (queue.TryDequeue(out DiscordGuild? guild))
            {
                int channelStage = 0;
                int webhookStage = 0;
                msg = await Update(ctx, guild, queue, true, channelStage, webhookStage, msg);

                await CleanupGuild(guild);

                var baseChan = new DevelopmentStressTestChannel()
                {
                    GuildId   = guild.Id,
                    GuildName = Names[ThreadSafeRandom.Next(0, Names.Count)]
                };

                await guild.ModifyAsync(x => x.Name = $"Test Server {baseChan.GuildName}");

                msg = await Update(ctx, guild, queue, false, channelStage, webhookStage, msg);

                List <DevelopmentStressTestChannel> createdChannels = new();
                List <DiscordChannel> rawChannels = new();
                // build new channels
                int x = 1;
                for (int i = 0; i < 50; i++)
                {
                    if (x % 5 == 0)
                    {
                        msg = await Update(ctx, guild, queue, false, ++channelStage, webhookStage, msg);
                    }

                    (DevelopmentStressTestChannel, DiscordChannel)chan = await BuildChannel(guild, baseChan);

                    createdChannels.Add(chan.Item1);
                    rawChannels.Add(chan.Item2);

                    x++;

                    await Task.Delay(TimeSpan.FromSeconds(.5));
                }

                msg = await Update(ctx, guild, queue, false, channelStage, webhookStage, msg);

                // build new webhooks
                x = 1;
                int rc = 0;
                foreach (DevelopmentStressTestChannel?c in createdChannels)
                {
                    if (x % 5 == 0)
                    {
                        msg = await Update(ctx, guild, queue, false, channelStage, ++webhookStage, msg);
                    }

                    await ApplyWebhookData(c, rawChannels[rc++]);

                    x++;

                    await Task.Delay(TimeSpan.FromSeconds(.5));
                }

                // save data
                data.AddRange(createdChannels);
            }

            await msg.ModifyAsync(embed : new DiscordEmbedBuilder()
                                  .WithColor(DiscordColor.Orange)
                                  .WithTitle("Partner Test Server Cleanup")
                                  .WithDescription("Cleaning up test server data...")
                                  .Build());

            data.RemoveAll(x =>
            {
                bool found = false;
                foreach (DiscordClient?shard in this._client.ShardClients.Values)
                {
                    if (shard.Guilds.ContainsKey(x.GuildId))
                    {
                        found = true;
                        break;
                    }
                }

                return(!found);
            });

            await DevelopmentStressTestDataManager.SaveDataAsync(data);

            await RespondSuccess("Setup Complete.");
        }