Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between Bot and Client?

I've been going through some examples on how to make a Discord Python Bot and I've been seeing client and bot being used almost interchangeably and I'm not able to find when you would use which one when.

For example:

client = discord.Client()
@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    if message.content.startswith('$guess'):
        await client.send_message(message.channel, 'Guess a number between 1 to 10')

    def guess_check(m):
        return m.content.isdigit()

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

client.run('token')

vs.

bot = commands.Bot(command_prefix='?', description=description)
@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.command()
async def add(left : int, right : int):
    """Adds two numbers together."""
    await bot.say(left + right)

bot.run('token')

I'm beginning to think they have very similar qualities and can do the same things but is a personal preference to go with a client vs. a bot. However they do have their differences where clients have an on_message while bots wait for a prefix command.

Can someone please clarify the difference between client and bot?

like image 237
m1771vw Avatar asked Jul 08 '18 18:07

m1771vw


1 Answers

Tl;dr

Just use commands.Bot.


Bot is an extended version of Client (it's in a subclass relationship). Ie. it's an extension of Client with commands enabled, thus the name of the subdirectory ext/commands.

The Bot class inherits all the functionalities of Client, which means that everything you can do with Client, Bot can do it too. The most noticeable addition was becoming command-driven (@bot.command()), whereas you would have to manually work with handling events when using Client. A downside of Bot is that you will have to learn the additional functionalities from looking at examples or source codes since the commands extension isn't much documented. UPD: Now it is documented here.

If you simply want your bots to accept commands and handle them, it would be a lot easier to work with the Bot since all the handling and preprocessing are done for you. But if you're eager to write your own handles and do crazy stunts with discord.py, then by all means, use the base Client.


In case you're stumped by which to choose, I recommend you to use commands.Bot since it's a lot easier to work with and it's in addition of everything Client can already do. And please remember that you do not need both.

WRONG:

client = discord.Client()
bot = commands.Bot(".")

# do stuff with bot

CORRECT:

bot = commands.Bot(".")

# do stuff with bot
like image 155
Taku Avatar answered Oct 21 '22 04:10

Taku