|
@@ -8,26 +8,26 @@ using Microsoft.Extensions.DependencyInjection; |
|
|
|
|
|
|
|
|
public class Program |
|
|
public class Program |
|
|
{ |
|
|
{ |
|
|
private CommandService commands; |
|
|
|
|
|
private DiscordSocketClient client; |
|
|
|
|
|
private IServiceProvider services; |
|
|
|
|
|
|
|
|
private CommandService _commands; |
|
|
|
|
|
private DiscordSocketClient _client; |
|
|
|
|
|
private IServiceProvider _services; |
|
|
|
|
|
|
|
|
static void Main(string[] args) => new Program().Start().GetAwaiter().GetResult(); |
|
|
static void Main(string[] args) => new Program().Start().GetAwaiter().GetResult(); |
|
|
|
|
|
|
|
|
public async Task Start() |
|
|
public async Task Start() |
|
|
{ |
|
|
{ |
|
|
client = new DiscordSocketClient(); |
|
|
|
|
|
commands = new CommandService(); |
|
|
|
|
|
|
|
|
_client = new DiscordSocketClient(); |
|
|
|
|
|
_commands = new CommandService(); |
|
|
|
|
|
|
|
|
string token = "bot token here"; |
|
|
string token = "bot token here"; |
|
|
|
|
|
|
|
|
services = new ServiceCollection() |
|
|
|
|
|
.BuildServiceProvider(); |
|
|
|
|
|
|
|
|
_services = new ServiceCollection() |
|
|
|
|
|
.BuildServiceProvider(); |
|
|
|
|
|
|
|
|
await InstallCommands(); |
|
|
await InstallCommands(); |
|
|
|
|
|
|
|
|
await client.LoginAsync(TokenType.Bot, token); |
|
|
|
|
|
await client.StartAsync(); |
|
|
|
|
|
|
|
|
await _client.LoginAsync(TokenType.Bot, token); |
|
|
|
|
|
await _client.StartAsync(); |
|
|
|
|
|
|
|
|
await Task.Delay(-1); |
|
|
await Task.Delay(-1); |
|
|
} |
|
|
} |
|
@@ -35,9 +35,9 @@ public class Program |
|
|
public async Task InstallCommands() |
|
|
public async Task InstallCommands() |
|
|
{ |
|
|
{ |
|
|
// Hook the MessageReceived Event into our Command Handler |
|
|
// Hook the MessageReceived Event into our Command Handler |
|
|
client.MessageReceived += HandleCommand; |
|
|
|
|
|
|
|
|
_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()); |
|
|
|
|
|
|
|
|
await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async Task HandleCommand(SocketMessage messageParam) |
|
|
public async Task HandleCommand(SocketMessage messageParam) |
|
@@ -48,13 +48,13 @@ public class Program |
|
|
// Create a number to track where the prefix ends and the command begins |
|
|
// Create a number to track where the prefix ends and the command begins |
|
|
int argPos = 0; |
|
|
int argPos = 0; |
|
|
// Determine if the message is a command, based on if it starts with '!' or a mention prefix |
|
|
// 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; |
|
|
|
|
|
|
|
|
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return; |
|
|
// Create a Command Context |
|
|
// Create a Command Context |
|
|
var context = new CommandContext(client, message); |
|
|
|
|
|
|
|
|
var context = new SocketCommandContext(_client, message); |
|
|
// Execute the command. (result does not indicate a return value, |
|
|
// Execute the command. (result does not indicate a return value, |
|
|
// rather an object stating if the command executed successfully) |
|
|
// rather an object stating if the command executed successfully) |
|
|
var result = await commands.ExecuteAsync(context, argPos, service); |
|
|
|
|
|
|
|
|
var result = await _commands.ExecuteAsync(context, argPos, _services); |
|
|
if (!result.IsSuccess) |
|
|
if (!result.IsSuccess) |
|
|
await context.Channel.SendMessageAsync(result.ErrorReason); |
|
|
await context.Channel.SendMessageAsync(result.ErrorReason); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |