How to make a Discord bot in PHP?

How to make a Discord bot in PHP?

So how to make a Discord bot in PHP?

In order to create a Discord bot in php, you need some common knowledge of PHP, a local server to run your code (like Wamp, Laragon, Xamp etc) and basic knowledge of “Composer”.

Also, “Composer” needs to be installed on your pc.

1. Create a folder in your wamp www folder.
Run the cmd command from Windows and type something like this to reach the discord folder that you just created.

Cd c:\
Cd wamp64
Cd www
Cd discord

Then run:
composer require team-reflex/discord-php

2. Create a run.php page and put it in the root folder of your discord folder, from wamp.

Write some code, something basic like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
 
include __DIR__.'/vendor/autoload.php';
 
use Discord\DiscordCommandClient;
 
$discord = new DiscordCommandClient([
  'token' => 'add your bot token here',
]);
 
$discord->on('ready', function ($discord) {
    echo "Bot is ready.", PHP_EOL;
 
 
 
    // Listen for events here
    $discord->on('message', function ($message) {
 
        echo "Recieved a message from {$message->author->username}: {$message->content}", PHP_EOL;
 
 
 }); //end small function with content
}); //end main function ready
 
$discord->run();
?>

2. Go to this page and register your application (bot).
https://discordapp.com/developers/applications/

Give your bot a name, go to the bot tab and take the token, paste it in your run.php page.

3. Invite the bot into your channel.
Paste this url in the browser: https://discordapp.com/oauth2/authorize?client_id=CLIENTID&scope=bot

You will get the client id from https://discordapp.com/developers/applications/.
You will see the client id on the main page. Replace it in the url and paste it.

You can invite the bot only on the channels that you are admin.

4. Run the bot from your cmd console.
Pull cmd, go to the correct path where the discord folder is and type “php run.php”.

You will see the bot starting up and going online on discord, on your channel.

That’s it. After that, you can start to add some more code in the run.php file.


Here is an example with some basic stuff. Replying to the user and also storing all the chat messages in a html file.

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
 
include __DIR__.'/vendor/autoload.php';
 
use Discord\DiscordCommandClient;
 
$discord = new DiscordCommandClient([
  'token' => 'your token goes here',
]);
 
$discord->on('ready', function ($discord) {
    echo "Bot is ready.", PHP_EOL;
 
 
 
    // Listen for events here
    $discord->on('message', function ($message) {
 
        echo "Recieved a message from {$message->author->username}: {$message->content}", PHP_EOL;
 
 
 
//I have named my bot Hall 9000, name yours as you like
 if ($message->author->username !== "Hall 9000"){
	//$message->reply("HI!");
 }
if (preg_match('/ping/', $message->content)){
	$message->reply("pong");
}
if ($message->content == '!info'){
	$message->reply("Here is some info!");
}
if ($message->content == '!age'){
	$message->reply("I am one day old!");
}
//this will close the bot
if ($message->content == '!exit'){
	$discord->close();
}
 
//gets the weather in my city
$url = 'http://rogeek.com/arhiva-vremea-meteo/locatie-galati.php';
$get_content = file_get_contents($url);
preg_match_all('/Temperatura: [0-9] grade celsius/', $get_content, $matches);
if ($message->content == '!weather'){
	$message->reply($matches[0][0]);
}
 
//if I write anything on the channel
if ($message->author->username == 'rgCristi'){
	$message->reply("chin up my dude!");
}
 
//writes data in a html file, the chat content, acts as a log
date_default_timezone_set('Europe/Bucharest');
$current_date = date('m.d.Y G:i:s');
$file = 'chat-log.html';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= $current_date. ' ' .$message->author->username.': '.$message->content.PHP_EOL;
// Write the contents back to the file
file_put_contents($file, $current);
 
 
}); //end small function with content
}); //end main function ready
 
$discord->run();
?>

Last idea, you can add some php code to upload the log file on a server and have it all the time on a website.

If you have any questions or you don’t understand something, please leave in the comments.

Add a comment: