Browse Source

Update Command Handler sample

- Update Main for C# 7.1.
- Inject CommandService and DiscordSocketClient into the service collection.
- Add Async suffix to asynchronous methods.
pull/826/head
Hsu Still 8 years ago
parent
commit
939f483d19
1 changed files with 10 additions and 6 deletions
  1. +10
    -6
      docs/guides/commands/samples/command_handler.cs

+ 10
- 6
docs/guides/commands/samples/command_handler.cs View File

@@ -12,9 +12,11 @@ public class Program
private DiscordSocketClient _client; private DiscordSocketClient _client;
private IServiceProvider _services; private IServiceProvider _services;


static void Main(string[] args) => new Program().Start().GetAwaiter().GetResult();
// Async Main is a C#7.1 feature.
// Use "static void Main(string[] args) => new Program().StartAsync().GetAwaiter().GetResult();" for C#7 and prior.
static Task Main(string[] args) => new Program().StartAsync();


public async Task Start()
public async Task StartAsync()
{ {
_client = new DiscordSocketClient(); _client = new DiscordSocketClient();
_commands = new CommandService(); _commands = new CommandService();
@@ -22,9 +24,11 @@ public class Program
string token = "bot token here"; string token = "bot token here";


_services = new ServiceCollection() _services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider(); .BuildServiceProvider();


await InstallCommands();
await InstallCommandsAsync();


await _client.LoginAsync(TokenType.Bot, token); await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync(); await _client.StartAsync();
@@ -32,15 +36,15 @@ public class Program
await Task.Delay(-1); await Task.Delay(-1);
} }


public async Task InstallCommands()
public async Task InstallCommandsAsync()
{ {
// Hook the MessageReceived Event into our Command Handler // Hook the MessageReceived Event into our Command Handler
_client.MessageReceived += HandleCommand;
_client.MessageReceived += HandleCommandAsync;
// Discover all of the commands in this assembly and load them. // Discover all of the commands in this assembly and load them.
await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
} }


public async Task HandleCommand(SocketMessage messageParam)
public async Task HandleCommandAsync(SocketMessage messageParam)
{ {
// Don't process the command if it was a System Message // Don't process the command if it was a System Message
var message = messageParam as SocketUserMessage; var message = messageParam as SocketUserMessage;


Loading…
Cancel
Save