0

I'm using pickledb and discord.ext, and I'm trying to store separate data for each user. How would I do that?

import discord
import random
import pickledb
from datetime import datetime
from discord.ext import commands


import math


crun = True
db = pickledb.load('discord.db', True)


@bot.command()
async def test(ctx):
    global token
    crun += 1
    await ctx.reply(f'you have run this command {crun} times.')



bot.run(TOKEN)
 

EDIT: how would i do it with something like this?

@bot.command(aliases=["classpick mage"])
async def classpick(ctx):
        classpicked = str(ctx.author)
        embed = discord.Embed(title="class = \"mage\"", description="here are your stats.", color=discord.Color.random())
        embed.add_field(name="magic_attack = ", value="15")
        embed.add_field(name="melee_attack = ", value="5", inline=True)
        embed.add_field(name="magic_defense = ", value="1", inline=True)
        embed.add_field(name="melee_defense = ", value="0", inline=True)
        embed.add_field(name="max_health = ", value="90", inline=True)
        embed.add_field(name="starting_skill = fireball", value="fireball", inline=True)
        embed.add_field(name="You are now a mage! do \"q adventure\" to start!", value="w", inline=True)
        print(db.get(classpicked))

1 Answer 1

0

This should work.

import discord
import random
import pickledb
from datetime import datetime
from discord.ext import commands
import math

db = pickledb.load('discord.db', True)

@bot.command()
async def test(ctx):
    author = str(ctx.author) # We get the username (RobertK#6151)
    if not db.exists(author): # If username is not already in the database
        db.set(author, 0) # Make profile for username in database or it will error
    db.append(author, 1) # Add 1 to the users count
    await ctx.reply(f'you have run this command {db.get(author)} times.')

bot.run(TOKEN)
3
  • Can you explain how this works? Commented Oct 9, 2022 at 16:23
  • I edited my answer and added comments on many lines Commented Oct 9, 2022 at 16:37
  • One more question. If I wanted multiple data sets per user, how would I do that? Commented Oct 9, 2022 at 21:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.