【问题标题】:How can i make a default command in discord py?如何在 discord py 中创建默认命令?
【发布时间】:2021-08-12 19:32:44
【问题描述】:

我想在同一个命令中有两个命令,我想要一个默认的“presencas”,另一个参数是“presencascanal”。

这是我的代码:

# 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'))

【问题讨论】:

  • 您的问题到底是什么?你有任何错误吗?有什么问题?
  • 我想把两个命令放在一起
  • 基本上,它们做同样的事情,那么如果我可以将它们放在一起,为什么我需要两个呢?我只是不知道该怎么做。我搜索了所有内容以找到此信息,但一无所获

标签: python discord discord.py


【解决方案1】:

基于 Vajra Budiono 答案下的 cmets,我认为您的意思是:

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

parameter = None 表示如果你调用一个没有参数的命令,默认情况下这个参数会有None 的值。

【讨论】:

    【解决方案2】:

    我想你想要一个命令函数用不同的命令来执行,我希望我做对了,你可以在命令装饰器中传递一个参数,它被称为别名,这是你如何传递它的:

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

    现在你有一个名为“command”的默认值,你也可以用“cmd”激活该功能,你还可以在别名列表中添加更多别名

    这是一个示例输出,前缀是“!”:

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

    希望我能帮上忙,如果你想问什么,请在这个答案下给我评论

    【讨论】:

    • ctx.send 是协程,应该等待
    • 有点像,但我希望同一个命令做两件不同的事情。
    • 我想在您键入该命令时为该命令创建一个默认函数,并创建一个更复杂的函数。例如 !presencas 应该执行默认功能,但是当您键入 !presencas 时,它应该执行另一项操作。我不知道我是否足够清楚,但基本上就是这样
    • hmmm,你可以使用辅助输入,比如在命令中添加额外的命令,比如 !command extra_data
    • 是的,我认为是这样的
    猜你喜欢
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 2021-09-04
    • 2020-10-09
    • 2021-05-22
    • 1970-01-01
    相关资源
    最近更新 更多