christopherson
christopherson

Reputation: 423

How can I get a counter to only increase for the message author in discord.py

I am trying to only increase the counter for the author of the message with the command. After that, I want to display it to another command. How do I assign a counter value based off of the author of the message?

client.counter = 0
@client.command(pass_context = True)
async def pick(ctx):
  author = ctx.message.author.id

 if 1 <= giftRate <= 2:
    if (1 <= lowFishRange <= 20):

        oneto5 = 5
        await client.say("<@%s>" % (author) + " recived " + str(oneto5) + " fish")
        client.counter += oneto5


    else:
        oneto5 = 1
        await client.say("<@%s>" % (author) + " recived " + str(oneto5) + " fish")
        client.counter += oneto5

else:
    await client.say("<@%s>" % (author) + " seemed to have missed the mark, the only way to gurantee fish is by "
                     "waiting for a cool pant cate to give you some.")

Upvotes: 0

Views: 502

Answers (1)

Benjin
Benjin

Reputation: 3497

Currently, client.counter is an integer, meaning it can only store one value. If you want to store a separate counter per user, you will need something that can store multiple values. One way to do this is to use a dictionary, which allows you to store values for specific keys. In this case, your keys would be your author IDs and your values would be your counters.

Using your code as a basis, below is an example of how this can be done.

client.counter = {}
@client.command(pass_context = True)
async def pick(ctx):
    author = ctx.message.author.id

    # check if author is in dictionary
    if author not in client.counter:
        client.counter[author] = 0

    if 1 <= giftRate <= 2:
        if (1 <= lowFishRange <= 20):

            oneto5 = 5
            await client.say("<@%s>" % (author) + " recived " + str(oneto5) + " fish")
            client.counter[author] += oneto5


        else:
            oneto5 = 1
            await client.say("<@%s>" % (author) + " recived " + str(oneto5) + " fish")
            client.counter[author] += oneto5

    else:
        await client.say("<@%s>" % (author) + " seemed to have missed the mark, the only way to gurantee fish is by waiting for a cool pant cate to give you some.")

Upvotes: 1

Related Questions