Update cmd docs to use IServiceProvider
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Reflection;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using Discord.Commands;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
public class Program
|
||||
{
|
||||
private CommandService commands;
|
||||
private DiscordSocketClient client;
|
||||
private DependencyMap map;
|
||||
private IServiceProvider services;
|
||||
|
||||
static void Main(string[] args) => new Program().Start().GetAwaiter().GetResult();
|
||||
|
||||
@@ -19,38 +21,40 @@ public class Program
|
||||
|
||||
string token = "bot token here";
|
||||
|
||||
map = new DependencyMap();
|
||||
services = new ServiceCollection()
|
||||
.BuildServiceProvider();
|
||||
|
||||
await InstallCommands();
|
||||
|
||||
await client.LoginAsync(TokenType.Bot, token);
|
||||
await client.ConnectAsync();
|
||||
await client.StartAsync();
|
||||
|
||||
await Task.Delay(-1);
|
||||
}
|
||||
|
||||
public async Task InstallCommands()
|
||||
{
|
||||
// Hook the MessageReceived Event into our Command Handler
|
||||
client.MessageReceived += HandleCommand;
|
||||
// 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());
|
||||
}
|
||||
|
||||
public async Task HandleCommand(SocketMessage messageParam)
|
||||
{
|
||||
{
|
||||
// Don't process the command if it was a System Message
|
||||
var message = messageParam as SocketUserMessage;
|
||||
if (message == null) return;
|
||||
// Create a number to track where the prefix ends and the command begins
|
||||
int argPos = 0;
|
||||
// Determine if the message is a command, based on if it starts with '!' or a mention prefix
|
||||
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return;
|
||||
// Create a number to track where the prefix ends and the command begins
|
||||
int argPos = 0;
|
||||
// Determine if the message is a command, based on if it starts with '!' or a mention prefix
|
||||
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return;
|
||||
// Create a Command Context
|
||||
var context = new CommandContext(client, message);
|
||||
// Execute the command. (result does not indicate a return value,
|
||||
// rather an object stating if the command executed succesfully)
|
||||
var result = await commands.ExecuteAsync(context, argPos, map);
|
||||
// rather an object stating if the command executed successfully)
|
||||
var result = await commands.ExecuteAsync(context, argPos, service);
|
||||
if (!result.IsSuccess)
|
||||
await context.Channel.SendMessageAsync(result.ErrorReason);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,11 @@ public class Commands
|
||||
{
|
||||
public async Task Install(DiscordSocketClient client)
|
||||
{
|
||||
// Here, we will inject the Dependency Map with
|
||||
// Here, we will inject the ServiceProvider with
|
||||
// all of the services our client will use.
|
||||
_map.Add(client);
|
||||
_map.Add(commands);
|
||||
_map.Add(new NotificationService(_map));
|
||||
_map.Add(new DatabaseService(_map));
|
||||
_serviceCollection.AddSingleton(client)
|
||||
_serviceCollection.AddSingleton(new NotificationService())
|
||||
_serviceCollection.AddSingleton(new DatabaseService())
|
||||
// ...
|
||||
await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user