Make a Discord Bot with Python - Part 2

If you don't already have a Discord server and a bot, you might want to check out Making a Discord Bot in Python - Part 1 which covers how to create a server, create a Discord app, create a bot user, authorize the bot for your server, and test it all with a Python script. This assumes you already have a bot account ready to go.

If you are interested in learning how to make a Discord bot in JavaScript, check out the JavaScript Discord Bot Tutorial and check out all of the other Discord related tutorials on DevDungeon.

If you want to check out some bots that are already made, check out two DevDungeon projects: Chatty Cathy AI chat bot and Help Desk Bot a fun utility bot, both written in Python.

Run pip install from your system terminal/shell/command prompt. Make sure to install the correct version of the package. If you are using PyCharm you can open the terminal from View -> Tool Windows -> Terminal

python -m pip install discord.py==0.16.12

Bot Code

# Work with Python 3.6
import random
import asyncio
import aiohttp
import json
from discord import Game
from discord.ext.commands import Bot

BOT_PREFIX = ("?", "!")
TOKEN = "XXXXSECRET_TOKENXXXXXXX"  # Get at discordapp.com/developers/applications/me

client = Bot(command_prefix=BOT_PREFIX)

@client.command(name='8ball',
                description="Answers a yes/no question.",
                brief="Answers from the beyond.",
                aliases=['eight_ball', 'eightball', '8-ball'],
                pass_context=True)
async def eight_ball(context):
    possible_responses = [
        'That is a resounding no',
        'It is not looking likely',
        'Too hard to tell',
        'It is quite possible',
        'Definitely',
    ]
    await client.say(random.choice(possible_responses) + ", " + context.message.author.mention)


@client.command()
async def square(number):
    squared_value = int(number) * int(number)
    await client.say(str(number) + " squared is " + str(squared_value))


@client.event
async def on_ready():
    await client.change_presence(game=Game(name="with humans"))
    print("Logged in as " + client.user.name)


@client.command()
async def bitcoin():
    url = 'https://api.coindesk.com/v1/bpi/currentprice/BTC.json'
    async with aiohttp.ClientSession() as session:  # Async HTTP request
        raw_response = await session.get(url)
        response = await raw_response.text()
        response = json.loads(response)
        await client.say("Bitcoin price is: $" + response['bpi']['USD']['rate'])


async def list_servers():
    await client.wait_until_ready()
    while not client.is_closed:
        print("Current servers:")
        for server in client.servers:
            print(server.name)
        await asyncio.sleep(600)


client.loop.create_task(list_servers())
client.run(TOKEN)

Links