elf
elf

Reputation: 46

How to add a + 1 to the number every time someone uses a command

I'm making a bakery bot in discord.py where people are able to bake items, I'm making a command called "Cake", where users can run it every 2/1 minutes and their cake count will go up.

For example, if someone runs the command b!bake cake it would return "You've baked 1 more cake, you now have 2 cakes!" How would I do this?

Upvotes: 1

Views: 430

Answers (1)

ChrisDewa
ChrisDewa

Reputation: 642

You need to store a the information somewhere.

If you want the data to be persistent across restart then write it to a file or database.

If you don't you can always save it to a variable accessible from anywhere. An option would be to use a global variable but it could be unaccessible from a cog, you can set an attribute in your bot's instance.

bot = commands.Bot(...)
bot.cakes = {}

@bot.command()
async def bake (ctx, dish):
    if dish == 'cake':
        if ctx.author.id not in bot.cakes.keys():
            bot.cakes[ctx.author.id] =  1
        else bot.cakes[ctx.author.id] += 1
        await ctx.send(f"you have baked {bot.cakes[ctx.author.id]} cakes")
bot.run(TOKEN)

Upvotes: 1

Related Questions