Browse Source

Sharded Interaction sample

pull/2054/head
Armano den Boef 3 years ago
parent
commit
6ad268bbd3
3 changed files with 75 additions and 0 deletions
  1. +17
    -0
      samples/ShardedClient/Modules/InteractionModule.cs
  2. +2
    -0
      samples/ShardedClient/Program.cs
  3. +56
    -0
      samples/ShardedClient/Services/InteractionHandlingService.cs

+ 17
- 0
samples/ShardedClient/Modules/InteractionModule.cs View File

@@ -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<ShardedInteractionContext<SocketSlashCommand>>
{
[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);
}
}
}

+ 2
- 0
samples/ShardedClient/Program.cs View File

@@ -58,7 +58,9 @@ namespace ShardedClient
=> new ServiceCollection()
.AddSingleton(new DiscordShardedClient(config))
.AddSingleton<CommandService>()
.AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordShardedClient>()))
.AddSingleton<CommandHandlingService>()
.AddSingleton<InteractionHandlingService>()
.BuildServiceProvider();




+ 56
- 0
samples/ShardedClient/Services/InteractionHandlingService.cs View File

@@ -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<InteractionService>();
_client = services.GetRequiredService<DiscordShardedClient>();
_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;
}
}
}

Loading…
Cancel
Save