Skip to content

Create a Discord Bot Using Asynchronous PHP in 4 steps

Intro

The landscape right now for Discord bots is filled with python and javascript. These work well but what about our beloved PHP? There is a project named Discord-PHP which bridges the gap for us using asynchronous PHP to keep our bot alive.

Prerequisites

  • PHP 7.2 or higher
  • Composer
  • ext-json
  • ext-zlib

1. Create a new application

The first thing you need to do is head over to the Discord developer portal and create a new application. Open a browser and visit:

Make sure you have logged in. Click on the “New Application” button in the top right corner, give your application a name, and click on the “Create” button.

Now we need to add a bot user to this application. Click on “Bot” in the main menu and then on the “Add Bot” button. As this action is irrevocable, you will have to confirm by clicking the “Yes, do it!” button.

2. Building the bot

Create a directory for the project

mkdir -p $HOME/Code/chuckcd $HOME/Code/chuck

Initialize Composer and require the dependencies

composer init --name="bypaul/chuck" -ncomposer require team-reflex/discord-php

Add a file named “bot.php” to the root of your project with this content:

<?php

use Discord\Discord;
use Discord\Parts\Channel\Message;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory;
use React\Http\Browser;

require __DIR__ . '/vendor/autoload.php';

$loop = Factory::create();

$browser = new Browser($loop);

$discord = new Discord([
    'token' => '',
    'loop' => $loop,
]);

$discord->on('message', function (Message $message, Discord $discord) use ($browser) {
    if (strtolower($message->content) == '!joke') {
        $browser->get('https://api.chucknorris.io/jokes/random')->then(function (ResponseInterface $response) use ($message) {
            $joke = json_decode($response->getBody())->value;

            $message->reply($joke);
        });
    }
});

$discord->run();

You will need to update 'token' => '' with the token for your bot. Go back to Discord Developer Portal and click on Bot in the main menu; there, you can find and copy your token.

And that’s it. Congratulations, you have just created a Discord bot using asynchronous PHP.

The above code listens for any new messages on the server. When a new message event is received, it checks if the content equals “!joke”, and if it does, it makes a GET request to https://api.chucknorris.io/jokes/random to get a random joke and replies it back to the server.

If you have never come in contact with asynchronous PHP and ReactPHP before, this code might seem a bit strange, and I would advise you to visit ReactPHP: (Event-driven, non-blocking I/O with PHP) to get you acquainted with it.

3. Inviting the bot to your server

Next, you will need to invite the bot to a server where you can try it out. If you don’t have a server, you will first need to create one. You invite the bot by going to Discord Developer Portal click on OAuth2, select “bot” for scopes, and add the permissions the bot requires. I have chosen “Administrator” for simplicity, but you should only give your bot the permissions it requires.

Copy and paste the link in your browser’s address bar, hit enter, and follow the instructions to add the bot to your server.

4. Running the bot

Now all that’s left is running the bot and trying it out. From the root of your project run:

php bot.php
As you can see, once you have this setup you can begin to create all kinds of bots for your discord server.

This was taken from https://levelup.gitconnected.com/4-steps-to-create-a-discord-bot-using-asynchronous-php-951082d7677a

Published inPHP

Be First to Comment

Leave a Reply

Your email address will not be published.