Update sample projects & samples in docs (#2823)
* update them all * more docs * moar docs
This commit is contained in:
@@ -6,76 +6,90 @@ using System;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace InteractionFramework
|
||||
namespace InteractionFramework;
|
||||
|
||||
public class InteractionHandler
|
||||
{
|
||||
public class InteractionHandler
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly InteractionService _handler;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public InteractionHandler(DiscordSocketClient client, InteractionService handler, IServiceProvider services, IConfiguration config)
|
||||
{
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly InteractionService _handler;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IConfiguration _configuration;
|
||||
_client = client;
|
||||
_handler = handler;
|
||||
_services = services;
|
||||
_configuration = config;
|
||||
}
|
||||
|
||||
public InteractionHandler(DiscordSocketClient client, InteractionService handler, IServiceProvider services, IConfiguration config)
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
// Process when the client is ready, so we can register our commands.
|
||||
_client.Ready += ReadyAsync;
|
||||
_handler.Log += LogAsync;
|
||||
|
||||
// Add the public modules that inherit InteractionModuleBase<T> to the InteractionService
|
||||
await _handler.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
|
||||
|
||||
// Process the InteractionCreated payloads to execute Interactions commands
|
||||
_client.InteractionCreated += HandleInteraction;
|
||||
|
||||
// Also process the result of the command execution.
|
||||
_handler.InteractionExecuted += HandleInteractionExecute;
|
||||
}
|
||||
|
||||
private async Task LogAsync(LogMessage log)
|
||||
=> Console.WriteLine(log);
|
||||
|
||||
private async Task ReadyAsync()
|
||||
{
|
||||
// Register the commands globally.
|
||||
// alternatively you can use _handler.RegisterCommandsGloballyAsync() to register commands to a specific guild.
|
||||
await _handler.RegisterCommandsGloballyAsync();
|
||||
}
|
||||
|
||||
private async Task HandleInteraction(SocketInteraction interaction)
|
||||
{
|
||||
try
|
||||
{
|
||||
_client = client;
|
||||
_handler = handler;
|
||||
_services = services;
|
||||
_configuration = config;
|
||||
// Create an execution context that matches the generic type parameter of your InteractionModuleBase<T> modules.
|
||||
var context = new SocketInteractionContext(_client, interaction);
|
||||
|
||||
// Execute the incoming command.
|
||||
var result = await _handler.ExecuteCommandAsync(context, _services);
|
||||
|
||||
// Due to async nature of InteractionFramework, the result here may always be success.
|
||||
// That's why we also need to handle the InteractionExecuted event.
|
||||
if (!result.IsSuccess)
|
||||
switch (result.Error)
|
||||
{
|
||||
case InteractionCommandError.UnmetPrecondition:
|
||||
// implement
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
catch
|
||||
{
|
||||
// Process when the client is ready, so we can register our commands.
|
||||
_client.Ready += ReadyAsync;
|
||||
_handler.Log += LogAsync;
|
||||
|
||||
// Add the public modules that inherit InteractionModuleBase<T> to the InteractionService
|
||||
await _handler.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
|
||||
|
||||
// Process the InteractionCreated payloads to execute Interactions commands
|
||||
_client.InteractionCreated += HandleInteraction;
|
||||
}
|
||||
|
||||
private async Task LogAsync(LogMessage log)
|
||||
=> Console.WriteLine(log);
|
||||
|
||||
private async Task ReadyAsync()
|
||||
{
|
||||
// Context & Slash commands can be automatically registered, but this process needs to happen after the client enters the READY state.
|
||||
// Since Global Commands take around 1 hour to register, we should use a test guild to instantly update and test our commands.
|
||||
if (Program.IsDebug())
|
||||
await _handler.RegisterCommandsToGuildAsync(_configuration.GetValue<ulong>("testGuild"), true);
|
||||
else
|
||||
await _handler.RegisterCommandsGloballyAsync(true);
|
||||
}
|
||||
|
||||
private async Task HandleInteraction(SocketInteraction interaction)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create an execution context that matches the generic type parameter of your InteractionModuleBase<T> modules.
|
||||
var context = new SocketInteractionContext(_client, interaction);
|
||||
|
||||
// Execute the incoming command.
|
||||
var result = await _handler.ExecuteCommandAsync(context, _services);
|
||||
|
||||
if (!result.IsSuccess)
|
||||
switch (result.Error)
|
||||
{
|
||||
case InteractionCommandError.UnmetPrecondition:
|
||||
// implement
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// If Slash Command execution fails it is most likely that the original interaction acknowledgement will persist. It is a good idea to delete the original
|
||||
// response, or at least let the user know that something went wrong during the command execution.
|
||||
if (interaction.Type is InteractionType.ApplicationCommand)
|
||||
await interaction.GetOriginalResponseAsync().ContinueWith(async (msg) => await msg.Result.DeleteAsync());
|
||||
}
|
||||
// If Slash Command execution fails it is most likely that the original interaction acknowledgement will persist. It is a good idea to delete the original
|
||||
// response, or at least let the user know that something went wrong during the command execution.
|
||||
if (interaction.Type is InteractionType.ApplicationCommand)
|
||||
await interaction.GetOriginalResponseAsync().ContinueWith(async (msg) => await msg.Result.DeleteAsync());
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleInteractionExecute(ICommandInfo commandInfo, IInteractionContext context, IResult result)
|
||||
{
|
||||
if (!result.IsSuccess)
|
||||
switch (result.Error)
|
||||
{
|
||||
case InteractionCommandError.UnmetPrecondition:
|
||||
// implement
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user