From a8adbb1fe94a98c74c7cdb5cf0f842790c570603 Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Thu, 8 Nov 2018 17:23:44 +0000 Subject: [PATCH] Tweak samples after feedback --- samples/01_basic_ping_bot/Program.cs | 11 ++----- samples/02_commands_framework/Program.cs | 1 + samples/03_sharded_client/Program.cs | 37 ++++++++++++++---------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/samples/01_basic_ping_bot/Program.cs b/samples/01_basic_ping_bot/Program.cs index b0953d115..4d6674e97 100644 --- a/samples/01_basic_ping_bot/Program.cs +++ b/samples/01_basic_ping_bot/Program.cs @@ -14,7 +14,7 @@ namespace _01_basic_ping_bot // - Here, under the 02_commands_framework sample // - https://github.com/foxbot/DiscordBotBase - a bare-bones bot template // - https://github.com/foxbot/patek - a more feature-filled bot, utilizing more aspects of the library - class Program : IDisposable + class Program { private readonly DiscordSocketClient _client; @@ -22,8 +22,7 @@ namespace _01_basic_ping_bot // an asynchronous context from the beginning. static void Main(string[] args) { - using (var program = new Program()) - program.MainAsync().GetAwaiter().GetResult(); + new Program().MainAsync().GetAwaiter().GetResult(); } public Program() @@ -73,11 +72,5 @@ namespace _01_basic_ping_bot if (message.Content == "!ping") await message.Channel.SendMessageAsync("pong!"); } - - public void Dispose() - { - // Dispose of the client when we are done with it - _client.Dispose(); - } } } diff --git a/samples/02_commands_framework/Program.cs b/samples/02_commands_framework/Program.cs index e270e7b56..76c11f9f0 100644 --- a/samples/02_commands_framework/Program.cs +++ b/samples/02_commands_framework/Program.cs @@ -37,6 +37,7 @@ namespace _02_commands_framework client.Log += LogAsync; services.GetRequiredService().Log += LogAsync; + // Tokens should be considered secret data, and never hard-coded. await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); await client.StartAsync(); diff --git a/samples/03_sharded_client/Program.cs b/samples/03_sharded_client/Program.cs index 048145f9f..7a2f99168 100644 --- a/samples/03_sharded_client/Program.cs +++ b/samples/03_sharded_client/Program.cs @@ -13,41 +13,46 @@ namespace _03_sharded_client // DiscordSocketClient instances (or shards) to serve a large number of guilds. class Program { - private DiscordShardedClient _client; - static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult(); public async Task MainAsync() { // You specify the amount of shards you'd like to have with the - // DiscordSocketConfig. Generally, it's recommended to + // DiscordSocketConfig. Generally, it's recommended to // have 1 shard per 1500-2000 guilds your bot is in. var config = new DiscordSocketConfig { TotalShards = 2 }; - _client = new DiscordShardedClient(config); - var services = ConfigureServices(); + // You should dispose a service provider created using ASP.NET + // when you are finished using it, at the end of your app's lifetime. + // If you use another dependency injection framework, you should inspect + // its documentation for the best way to do this. + using (var services = ConfigureServices(config)) + { + var client = services.GetRequiredService(); - // The Sharded Client does not have a Ready event. - // The ShardReady event is used instead, allowing for individual - // control per shard. - _client.ShardReady += ReadyAsync; - _client.Log += LogAsync; + // The Sharded Client does not have a Ready event. + // The ShardReady event is used instead, allowing for individual + // control per shard. + client.ShardReady += ReadyAsync; + client.Log += LogAsync; - await services.GetRequiredService().InitializeAsync(); + await services.GetRequiredService().InitializeAsync(); - await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); - await _client.StartAsync(); + // Tokens should be considered secret data, and never hard-coded. + await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); + await client.StartAsync(); - await Task.Delay(-1); + await Task.Delay(-1); + } } - private IServiceProvider ConfigureServices() + private ServiceProvider ConfigureServices(DiscordSocketConfig config) { return new ServiceCollection() - .AddSingleton(_client) + .AddSingleton(new DiscordShardedClient(config)) .AddSingleton() .AddSingleton() .BuildServiceProvider();