Hi everyone, last week I started back in school for the fall semester and wanted to take some more time to post more projects. Recently I’ve been playing some games with friends and we’ve been using discord. I was interested in making a discord bot with python, so after doing some googling, I have put together this guide that will hopefully help you create a cool bot for your own discord server.

  1. First off, you will need to make sure you have python 3.7 installed, If you need help with this click here. After that, you will need to download the Discord app which you can find here Discord download
  2. Ok, now that we’ve got that out of the way, we can actually begin. In order to create an app that will work with discord, we need to register the app and obtain the secret key that will allow us to link our program to discord. To do this first you need to visit the discord developer portal and click “New Application”.
  3. You will need to name your app, I called mine “MyApp”. Click “next” and find the secret key and client ID for your app, it will look something like this, just without the ID and secret key colored over. key
  4. IMPORTANT: Never share your secret key with anyone. Consider this a password to your app. You will want to copy both the client ID and secret key and store them somewhere safe.

  5. Next, click on the “Bot” button on the left side of the page then click on “Add a Bot”. You will see a page that looks like this bot Copy the bot token and save it with your client ID and secret key. We will use this later. Next, go to the bottom of the “Bot” page and select “Send Messages, “Read Message History”, “Add Reactions”, and anything else you would like your bot to be able to do.
  6. Now, click on the OAuth2 link on the left side of the page. You need to check the “Bot” toggle button
  7. After selecting the permissions for your bot, you need to link your bot to discord. To do this go to this link https://discordapp.com/oauth2/authorize?client_id=XXXXXXXXXXXX&scope=bot and replace “XXXXXXXXXXXX” with your bot token.

  8. OK, just one more step before we actually start to write code. We need to open up discord and create a new guild. So open the discord app, scroll down to the bottom of the list of guild you are a member of and click the “+” button.
    +
    Give your new guild a name and select the appropriate country. Once inside the guild, create a new text channel and then click the settings wheel next to the new channels name and go to “permissions”. You want to check the boxes “Read Messages”, “Send Messages”, and “Read Message History”.
  9. Ok, finally, we can begin to write the program to make this thing work. So open up your favorite IDE and create a new python file.

    You will need to install the discord python package so open up a terminal and type

    pip install discord

    Then go to discord python github page and download and unzip the file in the folder you created the project file in.

    At the top of the page we need to add the import for discord, so put

    import discord

    at the top. Below that we need to initialize a variable to be the discord client. So just below the import add

    client = discord.Client()
  10. Now we will create a function that will tell us when are bot is connected to the discord application. Type:
    
    @client.event
    async def on_connected():
        print(f"Connected as {client.user}")
    
  11. Ok, almost ready. Lets add some functionality to the bot so people can interact with it. So, below the on_connected function type:
    
    @client.event
    async def on_message(message):
        print(f"{message.author.name}: {message.content}")
        
        ###Important###
        disc_bot = client.get_guild(XXXXXXXXXXXXXXXXXXXXXXXXXXX) #add your bot id here
        
        if message.content.lower() == "hi there":
            await message.channel.sent("Hey!")
    
  12. And thats pretty much it. To actually run it, open discord, then open a terminal window and navigate to your file. Run it with python3 filename.py Then open
Categories: Uncategorized