Learn to Code via Tutorials on Repl.it!

← Back to all posts
Discord.py - Rewrite Tutorial using commands extension
h
TheDrone7

Discord.py Rewrite

Note : Before you start this tutorial, You should already know the language python, how to create applications in discord and you should've already invited the bot to your server.


Step 1 : Setting up

  1. Create a new folder for your bot, it can be anything. For reference, I will name it Sample Bot.

  2. Open up command prompt or terminal and type the following commands in it.

  3. Now create a new file named main.py in your bot's folder. This file will hold the main bot.

  4. Also create a new directory named cogs in your bot's directory, this will hold our command groups (aka cogs).

    Now you're ready to code


Step 2 : The main bot

  1. Open up your main.py file. Given below is some basic code with everything explained using comments.

  2. Now run this file, commands extension will prepare a default help command for you, you should see something similar to this when you use it: -

    Note that it doesn't show help when =help is used as this was done in DMs, and we only allowed ==help in DMs.

  3. Next we add the commands from the cogs directory.

  4. Inside the cogs directory, create a new file named basic.py.

  5. Add the following code above the

    part in the main.py file

    This basically points to the basic.py file in the cogs folder.

  6. Next, change

    to

    This will load the all the files specified in the cogs variable.

  7. Next, open up the basic.py file and write the following code in it.

    This will add a new ping command to your bot!

  8. Save the file


Step 3 : Checking the new command

  1. Run the main.py again.

  2. Use the help command. It should look something like this: -

    Observe the new Basic category and ping command listed under it. This means that our command was successfully loaded.

  3. Now use the help ping command, should look something like this: -

    Note that in the example, it shows the prefix used by you!

  4. Finally, let's use the ping command.

    This is an example run (my internet is a bit slow)


Step 4 : Commands with arguments

  1. Open up the basic.py file and add the following command to it by typing it just below the return statement of the ping command. This is a say command that repeats the words said by the user (in bold): -

  1. Next, let's use this command!

    As you can see, works perfectly!


Step 5 : Embeds

  1. Since, we've already done this, let's do one final command. The embed command.

    What this command will do?

    • Ask for the title of the embed.
    • Ask the user to specify the text for the embed description.
    • Set a random color to the embed.
    • Send the embed in the channel

    What this will help you learn?

    • How to create and send embeds.
    • How to wait for user's response.
  2. In your cogs directory, create a new file named embed.py

  3. In your main.py file, to the cogs list which formerly was cogs = ['cogs.basic'] add a new cog i.e. 'cogs.embed', making it - cogs = ['cogs.basic', 'cogs.embed'] . This will tell it to also treat the embed.py file as another cog.

  4. Open up the embed.py file and write the following code in it: -

  5. Now, let's run this command!

  6. And that's it!


Step 6 : Changing the default help command

  1. As we've already seen, commands extension already comes with a built-in help command. Let's use it again!

    Pretty clearly, it's very ugly. So, let's change it!

  2. So, what we'll do is, remove the pre-defined help command and then create a new help command in the embed.py file.

  3. Open up the main.py file and make the following changes: -

  4. Open the embed.py file and make a new help command is given below: -

    NOTE : We're making this in the embed.py file because this help command will be using embeds too. ( though it's not necessary to do even if it uses embeds, I just don't want to create another file with all those colors).

    What this help command will do?

    • Get all the cogs registered.

    • If the user has passed a cog argument,
      then list all commands under that cog.

    • Else, list all cogs and commands under them.

  1. This is just an example and gives you an basic idea of what it is like. Below is a sample run of this new help command.

    No cog specified, commands listed

    Embed cog specified, show the details of commands under Embed category.

    Invalid cog specified, send error message.


Quick Tip

Use .env files to store your token. I've used .env file to store my token in the final code. You can learn more about how to use .env files here.


The End

Those are all the basics you'll need to know, refer to API Reference for reference on the discord.py commands extension. You can also refer to the main library docs. All of the above code can be obtained at here. Also you can visit this link to learn how to host your discord bots on repl.it!

Voters
Tokushir
PishiCudar
studentSamarthS
CoolMasterB
Yemzero
BolehNgopi
Melinzz321
MemeManHimself
McJoe21
AbsoluteZero111
Comments
hotnewtop
BolehNgopi

Uhh I still confused 😅

How to make cogs?

NichoJ23

Everything works for me except for when I try to use the help command on a specific class (i.e. "=help basic"), and then it brings up this message:

the same thing happens when I try to fork your finished code

TheDrone7

@NichoJ23 looks like discord.py rewrite has had a new big update. I'll update this ASAP. Thanks for letting me know!

NichoJ23

Thanks! @TheDrone7

TheDrone7

@NichoJ23 I'm sorry it took so long but it's been fixed now! Thanks again for letting me know about this.

NichoJ23

Thanks for your help! @TheDrone7

raoulthegeek

maybe you could update the libs, as Discord.py Rewrite has been officially released, and replaced the async version

TheDrone7

@raoulthegeek thanks, done.

AidanJimenez1

Can someone help me im around step 3 and it says this
Ignoring exception in on_ready
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 30, in on_ready
for cog in cogs:
NameError: name 'cogs' is not defined

Jobbynaitor

nvm

Jobbynaitor

it doesn't recognise the ping cmd, when i exactly copy the code and run it's just like the cog doesn't exist

McJoe21

Indent error on line 20 (cogs.basic)

sunnylubby

Hello is anyone still available here? I'm getting this error for the custom help:

2020-05-03T14:05:40.834559+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
2020-05-03T14:05:40.834560+00:00 app[worker.1]: In embed.fields.10.name: This field is required
2020-05-03T14:05:40.834561+00:00 app[worker.1]: In embed.fields.10.value: This field is required
2020-05-03T14:05:40.834561+00:00 app[worker.1]: In embed.fields.12.name: This field is required
2020-05-03T14:05:40.834561+00:00 app[worker.1]: In embed.fields.12.value: This field is required
2020-05-03T14:05:40.834562+00:00 app[worker.1]: In embed.fields.4.name: This field is required
2020-05-03T14:05:40.834562+00:00 app[worker.1]: In embed.fields.4.value: This field is required
2020-05-03T14:05:40.834563+00:00 app[worker.1]: In embed.fields.6.name: This field is required
2020-05-03T14:05:40.834563+00:00 app[worker.1]: In embed.fields.6.value: This field is required
2020-05-03T14:05:40.834563+00:00 app[worker.1]: In embed.fields.8.name: This field is required
2020-05-03T14:05:40.834564+00:00 app[worker.1]: In embed.fields.8.value: This field is required

Thank you!

ripu

Nice job good tutorial

colezx5014

I'm very confused. Whenever I try to install discord.py using the "pip" thing, it does not work and it says "'pip' is not recognized as an internal or external command,
operable program or batch file". What do I do?

TheDrone7

@colezx5014 while installing python on your device, it gives you an option to add the directory to the path environment variable. You might have not checked it. You can either add the directory to the environment variable manually or reinstall python and make sure the option is checked.

[deleted]

@TheDrone7 What are cogs and do I have to use terminal or command prompt for this? Can you please share the link of your code so I can fork it and just do it?

P.s I am new to coding

TheDrone7

@haniel2007 I really have no clue what cogs means but I simply assume it's short for command groups cuz that basically defines them perfectly. As for the next part, command prompt or powers hell is what you'd use on windows, on Linux, you'd use terminal, on repl.it you'd use the repl package manager or just the requirements.txt solution. It's just different names for the same thing depending on the operating system. Nextly, the final code is linked at the end of the tutorial.
Lastly, I recommend you to learn about coding and python first and then return to this tutorial. This one is not for beginners or people who are not well versed with the language since I only explain what is discord.py doing and not what python is doing. Hope that'll be all.

AkimotoRyou

It gives me this error
TypeError: cogs must derive from Cog

AkimotoRyou

@TheDrone7 I've tried to fork it to my repl.it to get exact code, but it throws this error :

TheDrone7

@AkimotoRyou Can I see the repl?

AkimotoRyou
TheDrone7

@AkimotoRyou I saw it and observed that there have been a few changes in how discord.py-rewrite works. Please be patient while I update my tutorial and example.

TheDrone7

@AkimotoRyou Thanks for the patience, I have updated both my tutorial and the final example to match the latest requirements. Thanks for informing me.

pooopycaca68

@TheDrone7 the help command is not working it does not respond and it says not found somebody please help me i need it

anasir514

@pooopycaca68 did u call the help by using your specified prefix?

hillsam001

When I use the ping command, it says I need to use context, and same as with help ping...

TheDrone7

@hillsam001 it might be due to you using discord.py async. Although I'm not sure, I think I'll need more info like link to a repl and what error exactly you're getting.