Пример #1
0
        /// <summary>
        /// A pseudo unit test; should be decoupled into a full fledged test.
        /// Connects to the primary guild, restores the structures, prints some basic info and terminates.
        /// </summary>
        /// <returns></returns>
        public async Task TestBotAsync()
        {
            client   = new DiscordSocketClient();
            commands = new CommandService();
            services = new ServiceCollection()
                       .AddSingleton(client)
                       .AddSingleton(commands)
                       .BuildServiceProvider();
            client.Log += Log;
            await RegisterCommandsAsync();

            await client.LoginAsync(Discord.TokenType.Bot, Secret.botToken);

            await client.StartAsync();

            await client.SetGameAsync(get_botStatus());

            // We wish to set the bot's resident Discord guild and initialize.
            // This is however only possible when the client is ready.
            // See https://docs.stillu.cc/guides/concepts/events.html for further documentation.

            client.Ready += async() =>
            {
                if (_primary == null)
                {
                    _primary             = new PrimaryDiscordGuild(client);
                    _primaryServerLoaded = true;
                }
                if (!constructionComplete)
                {
                    await DelayedConstruction();

                    if (Settings.UsingExtensionRoleHighlights)
                    {
                        client.MessageReceived += _highlighter.Filter;
                    }
                }
                return;
            };


            while (true)
            {
                if (constructionComplete)
                {
                    Console.WriteLine("Server list:");
                    int i = 0;
                    foreach (DiscordGuild dg in guilds.byID.Values)
                    {
                        Console.WriteLine($"{i++}: {dg.GetName()}");
                    }
                }
                await Task.Delay(Settings.updatePeriod);
            }
        }
Пример #2
0
        public async Task RunBotAsync()
        {
            Timer banTimer = null;

            client   = new DiscordSocketClient();
            commands = new CommandService();
            services = new ServiceCollection()
                       .AddSingleton(client)
                       .AddSingleton(commands)
                       .BuildServiceProvider();
            client.Log += Log;
            await RegisterCommandsAsync();

            await client.LoginAsync(Discord.TokenType.Bot, Secret.botToken);

            await client.StartAsync();

            await client.SetGameAsync(get_botStatus());

            // We wish to set the bot's resident Discord guild and initialize.
            // This is however only possible when the client is ready.
            // See https://docs.stillu.cc/guides/concepts/events.html for further documentation.

            client.Ready += async() =>
            {
                if (_primary == null)
                {
                    _primary             = new PrimaryDiscordGuild(client);
                    _primaryServerLoaded = true;
                }
                if (!constructionComplete)
                {
                    await DelayedConstruction();

                    if (Settings.UsingExtensionRoleHighlights)
                    {
                        client.MessageReceived += _highlighter.Filter;
                    }

                    if (Settings.UsingExtensionBanTracking)
                    {
                        banTimer = new Timer(new TimerCallback(bt.UpdateStructure), null, TimeSpan.FromMinutes(2), TimeSpan.FromHours(12));
                    }
                }
                if (!roleInitComplete)
                {
                    _ = SyncRankRolesAndData();
                }

                return;
            };


            // Timer updateTimer = new Timer(new TimerCallback(UpdateAndBackup), null, TimeSpan.FromMinutes(2), TimeSpan.FromMinutes(60));
            while (true)
            {
                // We do the big update also in a separate thread.
                await Task.Delay(Settings.updatePeriod);

                _ = UpdateAndBackup(null);
            }
            // await Task.Delay();
        }