Skip to content

Instantly share code, notes, and snippets.

@BaseChip
Last active February 27, 2019 15:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BaseChip/e5d4583ad5392cd9638410c25d24547e to your computer and use it in GitHub Desktop.
Save BaseChip/e5d4583ad5392cd9638410c25d24547e to your computer and use it in GitHub Desktop.
Tutorial for DiscordBots.org | By BaseChip#2390

In this tutorial I'm going to answer the question, how can I create a Discord Bot? So I'll show you how to do this with Python3.6 and the rewrite API.

Install

First of all you should install the rewrite Api with the following command:

  • For Windows:
    • Start the Command Prompt as administrator
    • Type in py -3 -m pip install -U discord.py[voice]
  • For Linux:
    • Start the Terminal
    • Type in python3 -m pip install -U git+https://github.com/Rapptz/discord.py@rewrite#egg=discord.py[voice]

for more information you can have a look at this page

So now we have successfully installed discord. py! Next you have to create a new project, where we have to import Discord.py first.

import discord

Log in to the Bot / create a Bot

Now we have successfully imported Discord into our project, but we don't have a bot with which we can execute the code. To create a bot please go to https://discordapp.com/developers first and click on the tab "my apps" and then on "create app". After you have done this click on "create app" in the app and then "create a bot user""create a bot user"

Token

We still need the token from our newly created bot, but what is a token? A token is a string that is used to allows your code to log in into your bot, so you should never publish your token! To get the token, please click on "click to reveal" in your app under "Bot" and copy this token.

Get started

Now please insert the following code into your project below the import and I will explain the meaning of each part

class MyClient(discord.Client):
    async def on_ready(self):
        print("succesfully logged in")

    async def on_message(self, message):
        print("receive a message")

client = MyClient()
client.run("INSERT YOUR TOKEN HERE")

IMPORTANT Paste your token here client.run("INSERT YOUR TOKEN HERE")

For explanation: on_ready is executed when the bot has successfully logged in and is ready. The part with on_message is executed as soon as the bot receives a message, regardless of whether this message was sent on a server or a user has written it to the bot via DM.

The first command

So our bot can't do anything at the moment and that's kind of boring, so we add a ping command to our bot. We do this with the following code:

if message.content.startswith("!"):
	invoke = message.content[inleng:].split(" ")[0]

this code must be inserted into the on_message loop. The line if message. content. startswith ("!"): asks if the message starts with an exclamation mark and if not, the bot simply does nothing. The next line is only there to make our work easier, because the first word, which is written after the exclamation mark is stored under the variable invoke, which we can query directly in the next step.

if invoke=="ping":
	print("Pong")

So now our bot will print "Pong" into the console when someone writes "!ping".

How to invite the bot?

My last point is how to invite the bot to a server. For this you have to go back to your created app with the bot and from there you copy the client ID. Then go to this page and insert your client id and click on bot under the point scopes and select the required permissions for the bot. After that discord give you an Invite URL with which you can invite you bot to any server where you have the manage server permisssion.

The Code

import discord

class MyClient(discord.Client):
    async def on_ready(self):
        print("succesfully logged in")

    async def on_message(self, message):
     	if message.content.startswith("!"):
		invoke = message.content[inleng:].split(" ")[0]
		#Ping Command
		if invoke=="ping":
			print("Pong")

client = MyClient()
client.run("INSERT YOUR TOKEN HERE")
@JackSPN
Copy link

JackSPN commented Jan 24, 2018

Thank you for your help! I`ll try it later!

@BaseChip
Copy link
Author

and did it work for you? @JackSPN

@JackSPN
Copy link

JackSPN commented Jan 26, 2018

Yes it did ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment