How can i make a default command in discord py?

Viewed 184

I want to have two commands in the same command, I want a default one that will be "presencas" and another with the parameters that will be "presencascanal".

Here is the code I have:

# Command to know how much people are in general
@bot.command(name='presencas')
@commands.has_role(admin_id)
async def on_message(ctx):
    await ctx.send('Presenças no servidor no dia: ' + str(datetime.now().date()) + ' às ' + time.strftime(r"%H:%M") + 'H')
    with open('presencas_' + str(datetime.now().date()) + '.csv', 'w', newline='') as file:

        writer = csv.writer(file, delimiter=':', quoting=csv.QUOTE_NONE)
        writer.writerow(['Nome, tag, timestamp'])

        for user in ctx.guild.members:
            writer = csv.writer(file, delimiter='"', quoting=csv.QUOTE_NONE)
            if user.status != discord.Status.offline:
                writer.writerow([user.name + ', ' + '#'+user.discriminator + ', ' +str(datetime.now().date()) + ' ' + time.strftime(r"%H:%M") + 'H'])

    await ctx.send(file=discord.File(r'presencas_' + str(datetime.now().date()) + '.csv'))


#Command to know how much people are a specific channel
@bot.command()
@commands.has_role(admin_id)
async def presencascanal(ctx, canal):

    channel = bot.get_channel(int(canal))  # Canal de onde a lista vem

    members = channel.members  # Encontra os membros que estão no canal

    await ctx.send('Presenças no canal ' + channel.name + ' no dia ' + str(datetime.now().date()) + ' às ' + time.strftime(r"%H:%M") + 'H')
    with open('presencas_canal_' + channel.name + '_' + str(datetime.now().date()) + '.csv', 'w', newline='') as file:

        writer = csv.writer(file, delimiter=':', quoting=csv.QUOTE_NONE)
        writer.writerow(['Nome, tag, timestamp'])

        for user in members:
            writer = csv.writer(file, delimiter='"', quoting=csv.QUOTE_NONE)
            if user.status != discord.Status.offline:
                writer.writerow([user.name + ', ' + '#'+user.discriminator + ', ' +str(datetime.now().date()) + ' ' + time.strftime(r"%H:%M") + 'H'])

    await ctx.send(file=discord.File(r'presencas_canal_' + channel.name + '_' + str(datetime.now().date()) + '.csv'))
2 Answers

Based on comments under the Vajra Budiono answer I think that what you mean is this:

@bot.command(aliases=['cmd'])
async def command(ctx, parameter = None):
    if arg == None:
        # the default command
    else:
        # command when you pass parameter

the parameter = None means that if you invoke a command without parameter by default this parameter will have None value.

i think u want a command function to be executed with a different command, I hope I'm getting this right, well there is a parameter u can pass in the command decorator, it's called aliases, here's how u pass it in:

@bot.command(aliases=['cmd'])
async def command(ctx):
    ctx.channel.send('test')

now u have a default called "command" and u can also activate the function with "cmd", u can also add more aliases in the aliases list

here's an example output, the prefix will be "!":

user: !command
bot: test
user: !cmd
bot: test

hope i help, if u wanna ask something just give me a comment under this answer

Related