|
| 1 | +# Creating your first bot |
| 2 | + |
| 3 | +Exited to create your first bot? Once you [install Pycord](installation.md), you can start right away! |
| 4 | + |
| 5 | +## Creating the bot application |
| 6 | + |
| 7 | +Just like how you needed to sign up to Discord to get started, we need to get your bot signed up too. To do this, |
| 8 | + |
| 9 | +1. Go to the [Discord Developer Portal](https://discord.com/developers/applications) and click on <button class="blurplebutton">New Application</button>. |
| 10 | +2. Give your bot a name, and click <button class="blurplebutton">Create</button>. |
| 11 | +3. Now, you should see a page like this. |
| 12 | + |
| 13 | +4. Click on <button class="greybutton">Bot</button> tab on the left side of the screen. |
| 14 | +5. Click on <button class="blurplebutton">Add bot</button>. |
| 15 | +6. You can give it a name, change the Avatar, etc. |
| 16 | + |
| 17 | +## Inviting the bot |
| 18 | +Now, lets get the bot added to some servers. Go to the <button class="greybutton">OAuth2</button> tab in the left pane, and select `bot` and `applications.commands` as the scope. |
| 19 | + |
| 20 | +The `applications.command`s scope allows the bot to use Slash Commands, which you may want to have. |
| 21 | + |
| 22 | +Next, we choose what permissions the bot will have. You can select them. For now, lets give your bot the Administrator permission, meaning the bot will have all the permissions. |
| 23 | +Once you select the permissions, click on copy to get the bot invite link. |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +You can use this link to invite the bot. |
| 28 | + |
| 29 | +## Tokens |
| 30 | +Now that we have an account for our bot, we need to login. In order to login, we need the bot's password. |
| 31 | +All users and bots have a "token". You may think of a token as a unique password, since this is what we use to log into the bot and connect it to Discord. |
| 32 | + |
| 33 | +Tokens are "snowflakes". Not actual snowflakes, though. Just like how no two snowflakes in real life have the same pattern, a snowflake in computers is a unique thing - no two bots have the same token - so a token is a snowflake. An ID is a snowflake. |
| 34 | + |
| 35 | +Now, lets get our bot's token. To do this, |
| 36 | + |
| 37 | +1. Go back to the <button class="greybutton">Bot</button> tab. |
| 38 | +2. Click on the <button class="blurplebutton">Copy</button> button in the "Token" section. |
| 39 | + |
| 40 | + |
| 41 | +Now, you have your bot's token copied to your clipboard. |
| 42 | + |
| 43 | +!!! danger |
| 44 | + |
| 45 | +Never leak your bot's token, and never share it with anyone. Even if you get any DMs and someone tells you to do so, perhaps claiming to be Discord Staff, do not do so. They are probably lying and are scamming you. Anyone with your token will be able to access your bot fully. They will be able to do anything they want with your bot. |
| 46 | + |
| 47 | +Never push it to GitHub, or send it with the code. One way to prevent your token from getting leaked is to store it in `.env` files. |
| 48 | + |
| 49 | +### Protecting Tokens |
| 50 | + |
| 51 | +#### Using dotenv |
| 52 | + |
| 53 | +You can store your tokens in `.env` files. This is a simple way to store sensitive information. |
| 54 | + |
| 55 | +1. Create a file with the name `.env`. Just `.env`, with the dot/period at the start. |
| 56 | +2. Define the token in the file, like, |
| 57 | +```env |
| 58 | +TOKEN = [PASTE YOUR TOKEN HERE] |
| 59 | +``` |
| 60 | +for example, |
| 61 | +```env |
| 62 | +TOKEN = NzkyNzE1NDU0MTk2MDg4ODQy.X-hvzA.Ovy4MCQywSkoMRRclStW4xAYK7I |
| 63 | +``` |
| 64 | +3. Install [`python-dotenv`](https://pypi.org/project/python-dotenv/) |
| 65 | + ```bash |
| 66 | + python -m pip install python-dotenv |
| 67 | + ``` |
| 68 | +If your code is a git repository, you should create a file called `.gitignore` and add `.env` to it. This means that whenever you use `git push`, your .env file will not be pushed. It will stay secure in your local system. |
| 69 | + |
| 70 | +## Coding the Basics |
| 71 | + |
| 72 | +```py |
| 73 | +import discord |
| 74 | +import os # default module |
| 75 | +from dotenv import load_dotenv |
| 76 | +
|
| 77 | +load_dotenv() # we load all the variables from the env file |
| 78 | +
|
| 79 | +bot = discord.Bot(debug_guilds=[881207955029110855]) |
| 80 | +
|
| 81 | +@bot.event |
| 82 | +async def on_ready(): |
| 83 | + print(f"{bot.user} is ready and online!") |
| 84 | +
|
| 85 | +@bot.slash_command(name = "hello", description = "Say hello to the bot") |
| 86 | +async def hello(ctx): |
| 87 | + await ctx.send("Hey!") |
| 88 | +
|
| 89 | +bot.run(os.getenv('TOKEN')) # run the bot with the token |
| 90 | +``` |
| 91 | + |
| 92 | +Let's go through the code. First, the imports. |
| 93 | +
|
| 94 | +```py |
| 95 | +import discord |
| 96 | +import os |
| 97 | +from dotenv import load_dotenv |
| 98 | +``` |
| 99 | +
|
| 100 | +In the first line, `import discord`, we import Pycord. Although you install Pycord with `pip install py-cord`, you import it with `import discord`. |
| 101 | +
|
| 102 | +We then import `os` and `dotenv`. `os` is a default module that we will use to get the token from the env file. `dotenv` is a module that we will use to load the env file. You installed this with `pip install python-dotenv`. |
| 103 | +
|
| 104 | +Next, we load the env file with `load_dotenv()`. |
| 105 | +
|
| 106 | +```py |
| 107 | +bot = discord.Bot(debug_guilds=[881207955029110855]) |
| 108 | +``` |
| 109 | +
|
| 110 | +We create a new instance of [`discord.Bot`](https://docs.pycord.dev/en/master/api.html#discord.Bot |
| 111 | +). In the `debug_guilds` argument, we pass a list of guild IDs where we want to test the bot. Remember to change the list and add a server where both you and the bot are, and you (preferably) have admin permissions. Normally, you do not pass this kwarg. However, if you do not specify this, the slash commands will take up to an hour to show up in all the guilds the bot is in. When you restrict the commands to a few guilds, you can instantly see the commands. You can also choose to make lone commands restricted to a few guilds, but we will learn that later. |
| 112 | +
|
| 113 | +```py |
| 114 | +@bot.event |
| 115 | +async def on_ready(): |
| 116 | + print(f"{bot.user} is ready and online!") |
| 117 | +``` |
| 118 | +
|
| 119 | +We use the [`event`](https://docs.pycord.dev/en/master/api.html#discord.Bot.event) decorator and [`on_ready`](https://docs.pycord.dev/en/master/api.html#discord.on_ready) to define a function that will be called when the bot is ready. This is automatically called when the bot is ready to be used. |
| 120 | +
|
| 121 | +```py |
| 122 | +@bot.slash_command(name = "hello", description = "Say hello to the bot") |
| 123 | +async def say_hello(ctx): |
| 124 | + await ctx.send("Hey!") |
| 125 | +``` |
| 126 | +
|
| 127 | +Here, we use the [`slash_command`](https://docs.pycord.dev/en/master/api.html#discord.Bot.slash_command) decorator to define a slash command. We specify the `name` and `description` arguments. If not specified, the name of the Slash Command becomes the name of the function. The description will remain empty. |
| 128 | +
|
| 129 | +Finally, we run the bot using the token we specified in our `.env` file. |
| 130 | +
|
| 131 | +Now, you are done with creating your first bot. This is the basic structure of a bot. What else can you do? The sky is the limit! Pycord will not limit your imagination. The better you know Python, the more you can do. |
| 132 | +
|
| 133 | +## FAQ |
| 134 | +
|
| 135 | +### How do I make prefixed commands? |
| 136 | +
|
| 137 | +You can read [this amazing page that doesn't exist](ext/commands.md) and idk how it is gonna be linked but it should (hopefully) be there by the time you read this page. |
| 138 | + |
| 139 | +<style> |
| 140 | + .blurplebutton{ |
| 141 | +display:inline-block; |
| 142 | +padding:0.46em 1.6em; |
| 143 | +margin:0 0.2em 0.2em 0; |
| 144 | +border-radius: 3px; |
| 145 | +box-sizing: border-box; |
| 146 | +text-decoration:none; |
| 147 | +color:#ffffff; |
| 148 | +text-shadow: 0 0.04em 0.04em rgba(0,0,0,0.35); |
| 149 | +background-color:#5865F2; |
| 150 | +text-align:center; |
| 151 | +transition: all 0.15s; |
| 152 | +} |
| 153 | +.blurplebutton:hover{ |
| 154 | +text-shadow: 0 0 2em rgba(255,255,255,1); |
| 155 | +color:#FFFFFF; |
| 156 | +border-color:#FFFFFF; |
| 157 | +} |
| 158 | +@media all and (max-width:30em){ |
| 159 | + .blurplebutton{ |
| 160 | +display:block; |
| 161 | +margin:0.4em auto; |
| 162 | +} |
| 163 | +} |
| 164 | + |
| 165 | +.greybutton{ |
| 166 | +display:inline-block; |
| 167 | +padding:0.46em 1.6em; |
| 168 | +margin:0 0.2em 0.2em 0; |
| 169 | +border-radius: 3%; |
| 170 | +box-sizing: border-box; |
| 171 | +text-decoration:none; |
| 172 | +color:#ffffff; |
| 173 | +text-shadow: 0 0.04em 0.04em rgba(0,0,0,0.35); |
| 174 | +background-color:grey; |
| 175 | +text-align:center; |
| 176 | +transition: all 0.15s; |
| 177 | +} |
| 178 | +.blurplebutton:hover{ |
| 179 | +text-shadow: 0 0 2em rgba(255,255,255,1); |
| 180 | +color:#FFFFFF; |
| 181 | +border-color:#FFFFFF; |
| 182 | +} |
| 183 | +@media all and (max-width:30em){ |
| 184 | + .blurplebutton{ |
| 185 | +display:block; |
| 186 | +margin:0.4em auto; |
| 187 | +} |
| 188 | +} |
| 189 | +</style> |
0 commit comments