Example #1
0
        public async Task StartAsync()
        {
            _client = new DiscordShardedClient(new DiscordSocketConfig
            {
                LogLevel            = LogSeverity.Verbose,
                AlwaysDownloadUsers = true,
                GatewayIntents      = GatewayIntents.All,
                ConnectionTimeout   = int.MaxValue,
                DefaultRetryMode    = RetryMode.AlwaysRetry,
                MessageCacheSize    = 1024
            });

            _commands = new CommandService(new CommandServiceConfig
            {
                ThrowOnError          = true,
                CaseSensitiveCommands = false,
                LogLevel        = LogSeverity.Verbose,
                DefaultRunMode  = RunMode.Async,
                IgnoreExtraArgs = false
            });

            NekoEndpoints nekoEndpoints;

            using (var hc = new HttpClient())
                nekoEndpoints = new NekoEndpoints(JsonConvert.DeserializeObject <JObject>(await hc.GetStringAsync("https://raw.githubusercontent.com/Nekos-life/nekos-dot-life/master/endpoints.json")));

            _services = new ServiceCollection()
                        .AddSingleton(_client)
                        .AddSingleton(_commands)
                        .AddSingleton(new Random())
                        .AddSingleton(new LiteDatabase("database.db"))
                        .AddSingleton <DbService>()
                        .AddSingleton <MiscService>()
                        .AddSingleton(new GuildPrefix {
                GuildId = 0, Prefix = defaultPrefix
            })
                        .AddSingleton <ImageService>()
                        .AddSingleton <ModerationService>()
                        .AddSingleton(nekoEndpoints)
                        .AddSingleton <NetService>()
                        .AddSingleton <RandomService>()
                        .AddSingleton <InteractiveService>()
                        .BuildServiceProvider();

            await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);

            _db = _services.GetService <DbService>();

            Console.WriteLine($"Current directory: {Environment.CurrentDirectory}");
            var apiKey = _db.GetApiKey("discord");

            if (apiKey == null)
            {
                Console.WriteLine("What is the bot's token? (only logged to database.db)");
                apiKey = Console.ReadLine();
                _db.AddApiKey("discord", apiKey);
                Console.Clear();
            }

            await _client.LoginAsync(TokenType.Bot, apiKey);

            await _client.StartAsync();

            await _client.SetActivityAsync(new Game($"myself start up {_client.Shards.Count} shards", ActivityType.Watching));

            _client.Log             += LogAsync;
            _client.MessageReceived += MsgReceivedAsync;

            _client.ShardConnected += async(DiscordSocketClient client) =>
            {
                try
                {
                    await UpdateStatusAsync(client);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    await UpdateStatusAsync(client);
                }
            };

            _commands.Log += LogAsync;

            _commands.CommandExecuted += CommandExecutedAsync;

            if (!File.Exists("nsfw.txt"))
            {
                await File.WriteAllTextAsync("nsfw.txt", await _services.GetService <NetService>().DownloadAsStringAsync("https://paste.jakedacatman.me/raw/YU4vA"));
            }
            if (!File.Exists("phrases.txt"))
            {
                await File.WriteAllTextAsync("phrases.txt", await _services.GetService <NetService>().DownloadAsStringAsync("https://paste.jakedacatman.me/raw/8foSm"));
            }

            await Task.Delay(-1);
        }