Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling incoming slash command events #12

Open
Fuitad opened this issue Mar 10, 2023 · 9 comments
Open

Handling incoming slash command events #12

Fuitad opened this issue Mar 10, 2023 · 9 comments

Comments

@Fuitad
Copy link

Fuitad commented Mar 10, 2023

Hello. I'm trying to setup my laravel to handle incoming slash command.

I was able to register the commands and execute then on Discord. However, when Discord queries my endpoint, nothing I do seems to be working.

Right now, I have a controller with the following:

class DiscordInteractionController extends Controller
{
    protected DiscordInteractionServiceContract $interactionService;

    public function __construct(DiscordInteractionServiceContract $interactionService)
    {
        $this->interactionService = $interactionService;
    }

    public function handleDiscordInteraction(Request $request)
    {
        $response = $this->interactionService->handleInteractionRequest($request);
        return response()->json($response->toArray(), $response->getStatus());
    }
}

Then I have a Listener with this:

class MwCommandListener implements ShouldQueue, ApplicationCommandInteractionEventListenerContract
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    public function replyContent(ApplicationCommandInteractionEvent $event): ?string
    {
        return 'loading';
    }

    public function behavior(ApplicationCommandInteractionEvent $event): int
    {
        return static::REPLY_TO_MESSAGE;
    }

    public function command(): ?string
    {
        return env('DISCORD_COMMAND_PREFIX').'mw';
    }

    public function handle(ApplicationCommandInteractionEvent $event)
    {
        dd('test');
    }
}

And finally I modified my EventServiceProvider to this:

protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class
        ],
        ApplicationCommandInteractionEvent::class => [
            MwCommandListener::class
        ]
    ];

However, my endpoint calls from discord return an error. If I set shouldDiscoverEvents to true, the error is

Illuminate\Contracts\Container\BindingResolutionException
Target class [App\Listeners\MWCommandListener@handle] does not exist.

If I set it to false, then I get :

TypeError
Illegal offset type in isset or empty
/Users/pierre/Workspace/service/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:916
/Users/pierre/Workspace/service/vendor/nwilging/laravel-discord-bot/src/Support/Traits/HasInteractionListeners.php:23
/Users/pierre/Workspace/service/vendor/nwilging/laravel-discord-bot/src/Support/Traits/HasInteractionListeners.php:30

I'm about to throw my laptop across the room... is there any way you can help? :P

@nwilging
Copy link
Owner

is your queue driver/connection set to sync? I reproduced this locally, but only when the queue driver is sync -- the dd in the handle method kills the request.

The sync queue processes jobs synchronously, so if a process fails in that job (or script is killed with dd) then the entire request will fail.

If you remove the dd and replace it with a Log::debug('test') for example, you should be good to go. Or change the queue driver to something async like redis, and you can keep the dd

@nwilging
Copy link
Owner

As far as shouldDiscoverEvents, that's actually already false on my end. That shouldn't cause an issue, what laravel version are you on?

@Fuitad
Copy link
Author

Fuitad commented Mar 10, 2023

The dd() was out of despair but changing it and/or moving from sync to redis did not change the result.

Laravel 10.3.3
PHP 8.2.3

@nwilging
Copy link
Owner

ok i'll upgrade to 10 and take a look this weekend, i think my testing app is v9

@Fuitad
Copy link
Author

Fuitad commented Mar 10, 2023 via email

@Fuitad
Copy link
Author

Fuitad commented Mar 13, 2023

Here is a longer stack trace, if it helps.

/Users/pierre/Workspace/service/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:916
/Users/pierre/Workspace/service/vendor/nwilging/laravel-discord-bot/src/Support/Traits/HasInteractionListeners.php:23
/Users/pierre/Workspace/service/vendor/nwilging/laravel-discord-bot/src/Support/Traits/HasInteractionListeners.php:30
/Users/pierre/Workspace/service/vendor/nwilging/laravel-discord-bot/src/Support/Traits/HasInteractionListeners.php:29
/Users/pierre/Workspace/service/vendor/nwilging/laravel-discord-bot/src/Support/Traits/HasInteractionListeners.php:64
/Users/pierre/Workspace/service/vendor/nwilging/laravel-discord-bot/src/Support/Interactions/Handlers/ApplicationCommandHandler.php:46
/Users/pierre/Workspace/service/vendor/nwilging/laravel-discord-bot/src/Support/Interactions/Handlers/ApplicationCommandHandler.php:30
/Users/pierre/Workspace/service/vendor/nwilging/laravel-discord-bot/src/Services/DiscordInteractionService.php:50
/Users/pierre/Workspace/service/app/Http/Controllers/DiscordInteractionController.php:19
/Users/pierre/Workspace/service/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54

@nwilging
Copy link
Owner

apologies for the late response, but I've been unable to reproduce this issue even on L10. The only time I can get similar behavior is if I use the sync queue or otherwise kill the request with an exception, dd, or something similar

I know it's a lot to ask, but would you be able to see if you can replicate this on a fresh laravel project with just your new command and necessary supporting classes?

@Brianelvio
Copy link

Hi,

I am running into the same issue on a L10 installation.
My controller, listener are practically the same. And the listener is also registered with the event service provider.

Telescope informs me of a 500 error, and just as above TypeError: Illegal offset type in isset or empty.

Been debugging for hours, with no luck.

@Brianelvio
Copy link

So...

Laravel telescope injects as a listener, which was causing the TypeError for me.

HasInteractionListener:25 return $this->laravel->make($attributes['listener']);

Nevertheless I am still getting Interaction Failed from discord, but can finally continue debugging this properly.

Funny how this debug tool caused more issues.... I uninstalled it completely, not sure how to circumvent this one otherwise.
Already spent enough time on this lol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants