Discord.py Bot - What is the difference between `if` command and `async def`?

Viewed 71

I have seen many developers coding their python bots for Discord in 2 ways.

Some of them use:

if message.content.startswith("command"):
    await message.channel.send("text")

And others (majority) use this method:

@client.command()
async def command(ctx):
    response = "Text"
    await ctx.send(response)

What is the difference between them two and which method is better/ more productive to use?

1 Answers

Using @client.command() and async def means those are callable functions inside of a coroutine, whereas the if, I think, would require nesting in some kind of loop.

When you client.RUN(TOKEN) to start up your bot, the Discord module starts up an asyncio coroutine. It uses async and await to allow asynchronous task management, see the Python documentation on coroutines and the Discord.py API Reference on wait_for events

Related