Discord, originally developed as a gamer’s communication tool is seeing phenominal growth for the last few months. They have close to 1 million users right now adding more and more users every month. As per polygon they have close to 1 million users right now
(https://www.polygon.com/2017/12/7/16739644/discord-100-million-users-safety )
The peculiar aspect about discord is that they are supporting bots from day one, probably because their game focused origins. Unlike Whatsapp – the most popular instant messaging application which launched bots very recently, discord relied heavily on bots from day one. This along with tight integration with desktop environments and in game interaction has gained the tool massive popularity.
How to make a Discord Bot application in Python
Discord, originally developed as a gamer’s communication tool is seeing phenominal growth for the last few months. They have close to 1 million users right now adding more and more users every month. As per polygon they have close to 1 million users right now
(https://www.polygon.com/2017/12/7/16739644/discord-100-million-users-safety )
The peculiar aspect about discord is that they are supporting bots from day one, probably because their game focused origins. Unlike Whatsapp – the most popular instant messaging application which launched bots very recently, discord relied heavily on bots from day one. This along with tight integration with desktop environments and in game interaction has gained the tool massive popularity.
Initial Setup
Step1:
To download the discord.py library, go to your terminal and run the following command, $python3 -m pip install -U discord.py
You should now have the library installed and now you can get to the next step of setting up your bot. Discord Python documentation is here: https://github.com/Rapptz/discord.py
Step 2:
Go to this https://discordapp.com/developers/applications/me to create your first bot and then set up bot (which is your application under your discord account). To create a new application with your discord account. Click on new app and give your bot a name and profile picture, and optionally a description. Now under the app’s details panel, you should see a client id for your bot, and under the app bot user panel the token (which is hidden initially). Keep these two saved as we will use them later on. In order for your new bot to gain access to your server, we need to authorize it with your bot’s client id at this link below,
https://discordapp.com/oauth2/authorize?client_id=<CLIENT_ID>&scope=bot
We need to replace what I’ve bolded with your client id, so hypothetically if your id is 123456, your link should look like this,
https://discordapp.com/oauth2/authorize?client_id=123456&scope=bot
It should prompt you for which server to join, so pick the one you want and confirm it. That’s all for the setup portion, now we can move on to the programming for your bot!
Step 3:
Next, open your IDE and create a new script,
will need to define our bot. Do this by typing:
Put you bot’s prefix where it says “/” and your description where it says “description here”. In my case, I made “/” my prefix. Now is where the fun begins. First, to make it show when it connects to discord, type the following:
@bot.event
async def on_ready():
print(‘Logged in as’)
print(bot.user.name)
Step 4:
This simply states your bot’s name when it successfully connects to Discord and is ready to be used. But our bot isn’t ready to be run yet, so be patient. Now we will add a command! Start by typing:
@bot.command()
“””Description of command goes here”””
async def hello():
await bot.say(‘Hello”)
So far your code should look like this:
This still isn’t enough for it to run, so we will have to add the code that runs it. To do this, get out your Bot Token and add to your code:
bot.run(‘put your bot token here’)
Step 5:
So far your code should look like this:
Remember to put your Bot Token where it says to in that piece of code. Congratulations, you now have a fully functional bot! (almost) Now, to test it, we will add it to our server. To do this, add your client id to the following link where it says CLIENT_ID_GOES_HERE:
https://discordapp.com/api/oauth2/authorize?client_id=CLIENT_ID_GOES_HERE& scope=bot&permissions=8
Open the link in your browser. It should come to a page asking you which server you want to add the bot to. Open the menu and click what server you wish to add it to. Press the authorize button. Now, your bot should be in that server.
Now bot will show as offline. That’s because we haven’t ran the code yet! Go to your IDE and run your python script. Now, enter in the chat your command prefix followed by the command we added earlier, “hello”. For example, since my prefix is “/”, i would type “/hello”. Your bot should respond back to you, “Hello!”
How the fortune command work in your Bot
Fortune is yet another implementation of the Unix-style fortune program that displays a random message from a database of quotations. start by importing os and random and then type the following in your script:
@bot.command()
async def fortune(*args):
cmd = “fortune fortunes/fortunes”;
tmp = os.popen(cmd).read()
await asyncio.sleep(3)
await bot.say(tmp)
Then run your python script again and type /fortune as message in bot
You can see like this:
Extending the existing discord bot to support ELIZA
ELIZA is an early natural language processing computer program created by Joseph Weizenbaum for communication between humans and machines,In this article shows you a small version of ELIZA and how that working with discord bot application in python,
Go here : https://github.com/jezhiggins/eliza.py
and git clone or download the eliza.py into your working directory,go to eliza.py directory and run “eliza.py” script from your command line like “python3 eliza.py”, Now you can chat with eliza through command line and eliza will reply like this.
Now we need to write a script in python to support Eliza in discord bot application, Get your favorite editor and python ready and start up a new file by importing discord and then from eliza import command_interface method as elibot (in my case).
You need to define your client instance first so type client = discord.Client() What we do next is setup the on_message() function,This function called when a message is created and sent to a server,so now your code should look like this:
Then check the message author and client user are same and use await client.send_message (“To whom”, message.content) for sending messages and then add client.run (“your token here”). So your code should look like this:
Run the script by typing “python3 your_script_name.py” then the bot will go online and you can chat with your bot like this:
So the Eliza should work with discord bot like this.