简体   繁体   中英

Discord.py adding a command

So I am trying to make a command, my code:

global_reps = {}
@client.command()
async def global(ctx, top):

        
    co2 = dict(sorted(global_reps.items(), key=operator.itemgetter(1), reverse=True)[:10])
    print(co2)
    embed = discord.Embed(
        title = f'Global',
        description = f'Reputation leaderboard',
        color = random.choice(cols),
        timestamp = ctx.message.created_at
    )
    ccc = [1]
    to = list(co2.values())
    top = sum(to)
    top = int(top)
        
    for k, v in co2.items():
        percs = '{:.0%}'.format(v/top)

        k = int(k)
        m = client.get_user(k)
        if len(ccc) == 1:
            e = '🥇'

        elif len(ccc) == 2:
            e = '🥈'

        elif len(ccc) == 3:
            e = '🥉'

        else:
            e = ''
        embed.add_field(name=f'{len(ccc)}.{e}{m.name}#{m.discriminator}', value=f"> Rep points: **{v}**\n> Top: **{percs}**")
        ccc.append(1)
    embed.set_footer(text=f'Active at: ')
    msg = await ctx.send(embed=embed)

But in this line:

async def global(ctx, top):

Python 'thinks' that I try to use global keyword. But I just want it to become a command. How can I do this?

There actually is a way to do this:

@client.command(name="global")
async def THIS_COULD_HAVE_ANY_NAME_NOW(ctx, top):
    # now define your command here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM