PHP code example of nwilging / laravel-discord-bot
1. Go to this page and download the library: Download nwilging/laravel-discord-bot library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nwilging / laravel-discord-bot example snippets
t
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Nwilging\LaravelDiscordBot\Contracts\Notifications\DiscordNotificationContract;
use Nwilging\LaravelDiscordBot\Support\Builder\ComponentBuilder;
use Nwilging\LaravelDiscordBot\Support\Builder\EmbedBuilder;
class TestNotification extends Notification implements DiscordNotificationContract
{
use Queueable;
public function via($notifiable)
{
return ['discord'];
}
public function toDiscord($notifiable): array
{
return [
'contentType' => 'plain',
'channelId' => 'channel ID',
'message' => 'message content',
];
}
}
t
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Nwilging\LaravelDiscordBot\Contracts\Notifications\DiscordNotificationContract;
use Nwilging\LaravelDiscordBot\Support\Builder\ComponentBuilder;
use Nwilging\LaravelDiscordBot\Support\Builder\EmbedBuilder;
class TestNotification extends Notification implements DiscordNotificationContract
{
use Queueable;
public function via($notifiable)
{
return ['discord'];
}
public function toDiscord($notifiable): array
{
$embedBuilder = new EmbedBuilder();
$embedBuilder->addAuthor('Me!');
$componentBuilder = new ComponentBuilder();
$componentBuilder->addActionButton('My Button', 'customId');
return [
'contentType' => 'rich',
'channelId' => 'channel id',
'embeds' => $embedBuilder->getEmbeds(),
'components' => [
$componentBuilder->getActionRow(),
],
];
}
}
t
use Nwilging\LaravelDiscordBot\Contracts\Services\DiscordInteractionServiceContract;
class MyController extends Controller
{
private $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());
}
}
t
use Illuminate\Contracts\Queue\ShouldQueue;
use Nwilging\LaravelDiscordBot\Events\MessageComponentInteractionEvent;
use Nwilging\LaravelDiscordBot\Contracts\Listeners\MessageComponentInteractionEventListenerContract;
class MessageComponentInteractionListener implements MessageComponentInteractionEventListenerContract, ShouldQueue
{
public function replyContent(MessageComponentInteractionEvent $event): ?string
{
// return null; - to override and send no reply
return 'my reply message';
}
public function behavior(MessageComponentInteractionEvent $event): int
{
// return static::LOAD_WHILE_HANDLING; // Shows a loading message/status while handling
// return static::REPLY_TO_MESSAGE; // Replies to the interaction with replyContent(). Required if you want to reply to the interaction
return static::DEFER_WHILE_HANDLING; // Shows no loading message/status while handling
}
public function handle(MessageComponentInteractionEvent $event): void
{
// Handle the event like a normal listener
}
}
use Nwilging\LaravelDiscordBot\Support\Commands\SlashCommand;
use Nwilging\LaravelDiscordBot\Support\Commands\Options\ChannelOption;
$command = new SlashCommand('my-command', 'Command description');
$option1 = new ChannelOption('option1', 'description'); // Will allow user to select a channel
$option2 = new StringOption('option2', 'free text'); // Allows text input
$command->option($option1);
$command->option($option2);
use Nwilging\LaravelDiscordBot\Support\Commands\SlashCommand;
use Nwilging\LaravelDiscordBot\Contracts\Services\DiscordApplicationCommandServiceContract;
/** @var DiscordApplicationCommandServiceContract $appCommandService */
$appCommandService = app(DiscordApplicationCommandServiceContract::class);
$command = new SlashCommand('my-command', 'Command description');
// Creates a "global command" - available to any server your bot is a member of
$result = $appCommandService->createGlobalCommand($command);
// Creates a "guild command" - available only to the specified server/guild ID
$result = $appCommandService->createGuildCommand('server id', $command);
use Nwilging\LaravelDiscordBot\Support\Commands\SlashCommand;
use Nwilging\LaravelDiscordBot\Contracts\Services\DiscordApplicationCommandServiceContract;
/** @var DiscordApplicationCommandServiceContract $appCommandService */
$appCommandService = app(DiscordApplicationCommandServiceContract::class);
$command = new SlashCommand('my-command', 'Command description');
// First create the command
$created = $appCommandService->createGlobalCommand($command);
// Get the resulting ID
$id = $created['id'];
// Make a change to the command
$command->version('updated!');
// First method to update a command:
$updated = $appCommandService->createGlobalCommand($command);
// Second method to update a command:
$updated = $appCommandService->updateGlobalCommand($id, $command);
use Nwilging\LaravelDiscordBot\Contracts\Services\DiscordApplicationCommandServiceContract;
/** @var DiscordApplicationCommandServiceContract $appCommandService */
$appCommandService = app(DiscordApplicationCommandServiceContract::class);
$commandId = 'commandId';
$serverId = 'serverId'; // Only necessary for deleting guild commands
// Delete global command
$appCommandService->deleteGlobalCommand($commandId);
// Delete guild command
$appCommandService->deleteGuildCommand($serverId, $commandId);;
declare(strict_types=1);
namespace App\Listeners;
use Nwilging\LaravelDiscordBot\Contracts\Listeners\ApplicationCommandInteractionEventListenerContract;
use Nwilging\LaravelDiscordBot\Events\ApplicationCommandInteractionEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestCommandListener implements ShouldQueue, ApplicationCommandInteractionEventListenerContract
{
public function replyContent(ApplicationCommandInteractionEvent $event): ?string
{
return 'loading';
}
public function behavior(ApplicationCommandInteractionEvent $event): int
{
return static::REPLY_TO_MESSAGE;
}
public function command(): ?string
{
return null;
}
public function handle(ApplicationCommandInteractionEvent $event): void
{
// handle the interaction
}
}
/** @var \Nwilging\LaravelDiscordBot\Events\ApplicationCommandInteractionEvent $event **/
$event->getApplicationId(); // The application ID
$event->getChannelId(); // The channel that the command was run in
$event->getCommandId(); // The command's unique ID
$event->getCommandName(); // The name of the command
$event->getCommandType(); // Returns the command type, an integer
public const TYPE_CHAT_INPUT = 1;
public const TYPE_USER = 2;
public const TYPE_MESSAGE = 3;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.