Skip to content

Commit

Permalink
📝 Add documentation for context menus (Fixes #117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Jul 18, 2024
1 parent b160a96 commit 1ea06e0
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions content/docs/context-menus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
slug: context-menus
title: 'Context Menus'
description: 'Allow interacting with your Discord bot using context menus.'
priority: 3
group: Usage
---

Context menus are similar to slash commands, but are instead shown under `Apps` when right clicking/tapping a user or message.

Unlike slash commands, context menus do not contain options or subcommands.

## Creating a Context Menu

Creating your first context menu can be done using the `make:menu` command:

```sh
$ php laracord make:menu ExampleMenu
```

A context menu in it's simplest form will look something like:

```php
<?php

namespace App\Menus;

use Discord\Parts\Channel\Message;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\User\User;
use Laracord\Commands\ContextMenu;

class ExampleMenu extends ContextMenu
{
/**
* The context menu name.
*
* @var string
*/
protected $name = 'Example Menu';

/**
* The context menu type.
*/
protected string|int $type = 'message';

/**
* Handle the context menu interaction.
*/
public function handle(Interaction $interaction, Message|User|null $target): mixed
{
$interaction->respondWithMessage(
$this
->message()
->title('Example Menu')
->content('Hello world!')
->build()
);
}
}
```

### Context Menu Types

By default, the context menu will be shown when right-clicking a **message**. To have it shown when right-clicking a **user** instead, simply change `$type` to `user`:

```php
/**
* The context menu type.
*/
protected string|int $type = 'user';
```

### Guild Context Menus

To only register a context menu to a specific guild/server, you may set the `$guild` property:

```php
/**
* The guild the context menu belongs to.
*
* @var string
*/
protected $guild = 'your-guild-id';
```

0 comments on commit 1ea06e0

Please sign in to comment.