Discord is a chat platform, which is specially made for video gamers. This software is available for PC, MAC operating system, Linux, Android, and iOS, but it is recommended to use the app.
What is Discord Bot and Its Uses
Discord bot is the best way to increase the productivity of the server, which helps you to schedule events, sending notifications, get important data, and many more. This is one of the most promising features of Discord, which is used frequently.
Basically Discord bots are automated tasks, which performed on your discord server after specific events occurring. Like sending a welcome message to new members, server moderation, view information about your server or member, leveling system of discord room.
Now talking about how to use these bots, First thing you have to decide what functionality you want on your server. For example, you want to add a flare on the server or need a moderation bot, etc. Many readymade discord bots are available on the internet, a recommended website is top.gg. Here you can find many categorized discord bot, just invite it on your server.
How to Make a Discord Bot
Discord has an API, via this, we can create any custom bot, For this, you need some programming skills, it’s not for everyone. Let’s start with the initial process to create a discord bot.
1. First visit to the discord developer portal. Here you need to create New Application, just name it as you want to identify.
2. Now you get the Client ID and Secret key, note it down, we need it later. Make sure that do not reveal these keys to anyone, otherwise your bot will be hacked.
3. Now add a bot user to your application, via click on the ‘Add Bot’ button. This action is irrevocable so choose wisely.
4. After adding bot user, you got the token, click to reveal token and note it down. This is important, so don’t share it with anyone. Enable PUBLIC BOT option, so this bot can be added by anyone, Or you can keep this bot private.
At the same screen below, all the bot permissions are mentioned, you can select any one as per your discord bot requirement.
The above process is for creating a discord bot online via browser app, next we are showing to create custom code for the bot to add more functionality in it and install on your server. So let’s start coding for that…
Start coding to create Discord Bot in PHP:
We are using PHP programming language to create discord bot, for this, you need basic knowledge of it and a composer (https://getcomposer.org/) installed on your server.
Note: PHP version must be 5.6 or above, we recommend to use PHP 7.X.
Step a: Create a folder on your server via composer, let’s create it ‘NewBot’ folder using this command:
$ mkdir NewBot && cd NewBot
Then initialize the composer via this command:
$ composer init
Now we need DiscordPHP into composer project, for this execute this command :
composer require team-reflex/discord-php
This command will install the latest version of DiscordPHP
Step b: Now create a run.php file inside root ‘NewBot’ folder, that you just created and add below code in it.
run.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php include __DIR__.'/vendor/autoload.php'; $discord = new \Discord\Discord([ 'token' => 'your-auth-token', ]); $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; }); }); $discord->run(); ?> |
Now add your Bot Token in run.php file, which we got in above Step 4.
Step c: Now do invite this custom bot into your channel, for this use this web URL in your browser:
https://discordapp.com/oauth2/authorize?client_id=CLIENTID&scope=bot
Replace CLIENTID with your application client ID value and execute this URL. This will prompt a window to add bot to the server, Pick any server and authorize it.
Note: You can add this bot only on the channels that you are an admin.
Step d: To start this bot and make it online, go on cmd, and write the correct path of the NewBot folder and type “php run.php” command.
This will start discord bot accessible online on your channel.
Once these all steps are done, you can send this link to your friends to add this bot on their discord server too.
That’s it. We have shown a basic example of the bot, you can add more functionality via adding more code in run.php file. Such as display channel member information, auto-reply of specific message, etc.
Hope this tutorial helps you create your first Discord Bot successfully. Thanks