diff --git a/samples/ShardedClient/Modules/InteractionModule.cs b/samples/ShardedClient/Modules/InteractionModule.cs new file mode 100644 index 000000000..040adf229 --- /dev/null +++ b/samples/ShardedClient/Modules/InteractionModule.cs @@ -0,0 +1,17 @@ +using System.Threading.Tasks; +using Discord.Interactions; + +namespace ShardedClient.Modules +{ + // A display of portability, which shows how minimal the difference between the 2 frameworks is. + public class PublicModule : InteractionModuleBase> + { + [SlashCommand("info", "Information about this shard.")] + public async Task InfoAsync() + { + var msg = $@"Hi {Context.User}! There are currently {Context.Client.Shards.Count} shards! + This guild is being served by shard number {Context.Client.GetShardFor(Context.Guild).ShardId}"; + await RespondAsync(msg); + } + } +} diff --git a/samples/ShardedClient/Program.cs b/samples/ShardedClient/Program.cs index 4e9f8d775..dba9241f6 100644 --- a/samples/ShardedClient/Program.cs +++ b/samples/ShardedClient/Program.cs @@ -58,7 +58,9 @@ namespace ShardedClient => new ServiceCollection() .AddSingleton(new DiscordShardedClient(config)) .AddSingleton() + .AddSingleton(x => new InteractionService(x.GetRequiredService())) .AddSingleton() + .AddSingleton() .BuildServiceProvider(); diff --git a/samples/ShardedClient/Services/InteractionHandlingService.cs b/samples/ShardedClient/Services/InteractionHandlingService.cs new file mode 100644 index 000000000..f720d6cb3 --- /dev/null +++ b/samples/ShardedClient/Services/InteractionHandlingService.cs @@ -0,0 +1,56 @@ +using System; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Discord; +using Discord.Interactions; +using Discord.WebSocket; + +namespace ShardedClient.Services +{ + public class CommandHandlingService + { + private readonly InteractionService _service; + private readonly DiscordShardedClient _client; + private readonly IServiceProvider _provider; + + public CommandHandlingService(IServiceProvider services) + { + _service = services.GetRequiredService(); + _client = services.GetRequiredService(); + _provider = services; + + _service.CommandExecuted += CommandExecutedAsync; + _service.Log += LogAsync; + _client.InteractionCreated += OnInteractionAsync; + } + + // Register all modules, and add the commands from these modules to either guild or globally depending on the build state. + public async Task InitializeAsync() + { + await _service.AddModulesAsync(typeof(CommandHandlingService).Assembly, _provider); +#if DEBUG + await _service.AddCommandsToGuildAsync(/* debug guild ID */); +#else + await _service.AddCommandsGloballyAsync(); +#endif + } + + private async Task OnInteractionAsync(SocketInteraction interaction) + { + _ = Task.Run(async () => + { + var context = new ShardedInteractionContext(_client, interaction); + await _service.ExecuteCommandAsync(context, _provider); + }); + await Task.CompletedTask; + } + + private Task LogAsync(LogMessage log) + { + Console.WriteLine(log.ToString()); + + return Task.CompletedTask; + } + } +}