#Slash Command not responding

1 messages · Page 1 of 1 (latest)

white nova
#

Why is this slash command in a module not working?

#
using Discord.Commands;
using Discord.Interactions;
using MalinoBot.Services;
using Microsoft.Extensions.DependencyInjection;

namespace MalinoBot.Modules.Commands;

public class MyModule : InteractionModuleBase
{
    private readonly MyService _service;

    public MyModule(IServiceProvider services)
    {
        _service = services.GetRequiredService<MyService>();
    }

    [SlashCommand("ping", "Get a pong")]
    public async Task PingAsync()
        => await RespondAsync("Pong!");
    
    [SlashCommand("things", "Shows things")]
    public async Task ThingsAsync()
    {
        var str = string.Join("\n", _service.Things);
        await RespondAsync(str);
    }
}
#

Program.cs

// See https://aka.ms/new-console-template for more information

using System.Collections;
using Discord;
using Discord.Commands;
using Discord.Interactions;
using Discord.WebSocket;
using MalinoBot.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace MalinoBot;

class Program
{
    static async Task Main(string[] args)
    {
        var services = CreateProvider();
        var app = services.GetRequiredService<Bot>();
        await app.RunAsync();
    }

    static IServiceProvider CreateProvider()
    {
        var services = new ServiceCollection();

        services
            .AddSingleton<IConfiguration>(CreateAppConfig())
            .AddSingleton(CreateDiscordConfig());

        services
            .AddSingleton<MyService>()
            .AddSingleton<LogHandler>();
        
        services
            .AddSingleton<DiscordSocketClient>()
            .AddSingleton<InteractionService>()
            .AddSingleton<Bot>();

        return services.BuildServiceProvider();
    }

    private static DiscordSocketConfig CreateDiscordConfig()
    {
        var config = new DiscordSocketConfig
        {
            GatewayIntents = GatewayIntents.All
        };

        return config;
    }

    private static IConfigurationRoot CreateAppConfig()
    {
        var config = new ConfigurationBuilder();
        config.AddJsonFile("appconfig.json");
        
        return config.Build();
    }
}
#

Result for /ping is the following:

#

It registers the commands successfully but doesn't even call it (tested via debug breakpoints).

naive mulchBOT
#

Showing lines 55 - 84 of dev/samples/InteractionFramework/InteractionHandler.cs ```cs

55 private async Task HandleInteraction(SocketInteraction interaction)
56 {
57 try
58 {
59 // Create an execution context that matches the generic type parameter of your InteractionModuleBase<T> modules.
60 var context = new SocketInteractionContext(_client, interaction);
61
62 // Execute the incoming command.
63 var result = await _handler.ExecuteCommandAsync(context, _services);
64
65 // Due to async nature of InteractionFramework, the result here may always be success.
66 // That's why we also need to handle the InteractionExecuted event.
67 if (!result.IsSuccess)
68 switch (result.Error)
69 {
70 case InteractionCommandError.UnmetPrecondition:
71 // implement
72 break;
73 default:
74 break;
75 }
76 }
77 catch
78 {
79 // 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
80 // response, or at least let the user know that something went wrong during the command execution.
81 if (interaction.Type is InteractionType.ApplicationCommand)
82 await interaction.GetOriginalResponseAsync().ContinueWith(async (msg) => await msg.Result.DeleteAsync());
83 }
84 }

white nova
#

so i assumed it's automatically handled, especially since the commands are registered

naive prawn
white nova
#

Ah i see the issue

white nova
#

Thanks

#

I don't quite understand the difference between created and executed though.

#

Well, it seems like Created is executed first an d Executed afterwards which seems like it's a kind of PreExecution and PostExecution kinda thing.

naive prawn
#

so you can handle it in whatever way you want

#

InteractionService.InteractionExecuted - fired when the service is done handling an interaction

#

so you can hadnle execution errors/failed preconditions/etc