discord.py – How to Store User IDS Once their key is claimed

Question:

So im setting up a discord bot , I don’t know what to use or what a good resource is for my "storing" something , I don’t really understand it so im asking for some help. Here is the code I have so far

import discord
import rstr
import random
from discord.ext import commands
intents = discord.Intents.all()
bot = commands.Bot(command_prefix=".", intents=intents, help_command=None)


list_keys = [rstr.xeger(r'[a-zA-Zd]{25}') for x in range(10)]
@bot.listen()
async def on_ready():
    print('ready')

@bot.command()
async def redeem(ctx, key):
    if key in list_keys:
        embed=discord.Embed(title="Demon V2", description=f"Claimed the key !  /n   Happy Bloxfliping , If commands arent working dm scooby#0001 (no caps)")
        embed.set_thumbnail(url="https://media.discordapp.net/attachments/860177535010603028/1010881302678999140/PP4Y_756.gif")
        embed.set_footer(text="#8k.HITTAS | scooby#0001")
        await ctx.send(embed=embed)
    else:
        embed=discord.Embed(title="Demon V2", description="Invaild Key.", color=0x980de3)
        embed.set_thumbnail(url="https://media.discordapp.net/attachments/860177535010603028/1010881302678999140/PP4Y_756.gif")
        embed.set_footer(text="#8k.HITTAS | buy keys from scooby#0001")
        await ctx.send(embed=embed)


@bot.command()
async def printkeys(ctx):
    for key in list_keys:
        print(key)

bot.run('my token')

But I’m trying to set it up where said person runs a code like
".claim (key)"
And then it stores there ID into somewhere if its a valid key
and eventually it can make them where they can use certain commands, that others cannot.

Asked By: Scooby

||

Answers:

I’d like to recomend to use SQLite to store all keys, to keep records even when the bot is off.

You have to do the next to create the database with the table:

import sqlite3

DB=sqlite3.connect("Bot_DB")

query=DB.cursor()

query.execute("""CREATE TABLE IF NOT EXISTS KEYS (
        UNQ_ID PRIMARY KEY AUTOINCREMENT,
        USER_ID NUMBER,
        CLAIMED_KEY VARCHAR2
);""")

then, create the command:

@bot.command()
async def claim(ctx):
    new_key=rstr.xeger(r'[a-zA-Zd]{25}')
    query.execute("""INSERT INTO KEYS (USER_ID, CLAIMED_KEY) VALUES (?, ?)""",(ctx.author.id, new_key,))
    embed=discord.Embed(title="Key generated!", description=f"Your new personal key is **{new key}**", color=0x980de3)
    await ctx.send(embed=embed)

Note that previous way to generate key (with list comprehension) has been replaced.

This way allows you to fetch key and user with single query. In this sense, to redeem a key, command be like this:

@bot.command()
async def redeem(ctx, key):
    query.execute("""SELECT CLAIMED_KEY FROM KEYS WHERE USER_ID = ?""",(ctx.author.id,))
    list_keys=query.fecthall()
    if key in list_keys:
        embed=discord.Embed(title="Demon V2", description=f"Claimed the key !  /n   Happy Bloxfliping , If commands arent working dm scooby#0001 (no caps)")
        embed.set_thumbnail(url="https://media.discordapp.net/attachments/860177535010603028/1010881302678999140/PP4Y_756.gif")
        embed.set_footer(text="#8k.HITTAS | scooby#0001")
        await ctx.send(embed=embed)
    else:
        embed=discord.Embed(title="Demon V2", description="Invaild Key.", color=0x980de3)
        embed.set_thumbnail(url="https://media.discordapp.net/attachments/860177535010603028/1010881302678999140/PP4Y_756.gif")
        embed.set_footer(text="#8k.HITTAS | buy keys from scooby#0001")
        await ctx.send(embed=embed)

Hope this helped you.

Answered By: Andrés Ricaurte
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.