M
M
Mem13882021-06-14 19:19:11
Python
Mem1388, 2021-06-14 19:19:11

How to calculate the time (cooldown) for the team?

I want to make a rollback for the command, which will be stored in a common database, and when the bot is restarted, it continues to count the time. Here is what I wrote, but the loop for counting does not work, the time is written to the database and is no longer updated.

conn = sqlite3.connect('testingBot.db')
cur = conn.cursor()

@Bot.event
async def on_ready():
    cur.execute(f"""CREATE TABLE IF NOT EXISTS users (
        id BIGINT,
        rep INT,
        rep_last_user BIGINT,
        rep_time BIGINT
    )""")
    for guild in Bot.guilds:
        for member in guild.members:
            if cur.execute(f"SELECT id FROM users WHERE id = {member.id}").fetchone() is None:
                cur.execute(f"INSERT INTO users VALUES (?, ?, ?, ?)", (member.id, 0, 0, 0))
            else:
                pass
    conn.commit()
    print(f"Bot logged as -> {Bot.user}")

# Цикл для отсчёта времени, который написан некорректно.
for guild in Bot.guilds:
    for member in guild.members:
        while cur.execute(f"SELECT rep_time FROM users WHERE id = {member.id}").fetchone()[0] > 0:
            cur.execute(f"UPDATE users SET rep_time = rep_time - 5 WHERE id = {member.id}")
            conn.commit()

@Bot.command()
async def rep(ctx, member: Optional[discord.Member]):
    if cur.execute(f"SELECT rep_time FROM users WHERE id = {ctx.message.author.id}").fetchone()[0] == 0:
        if member is None:
            return
        elif member.id == ctx.message.author.id:
            emb = discord.Embed(
                description = f'{ctx.message.author.mention} Вы не можете повысить репутацию сами себе.',
                color = 0xff0000
            )
            await ctx.send(embed = emb)
        else:
            cur.execute(f"UPDATE users SET rep = rep + 1 WHERE id = {member.id}")
            conn.commit()
            emb = discord.Embed(
                description = f'{ctx.message.author.mention} Вы повысили репутацию пользователя {member.mention} \nТеперь у него **+{cur.execute(f"SELECT rep FROM users WHERE id = {member.id}").fetchone()[0]}☆** репутации. \n\nПовысить снова можно через **2 часа.**',
                color = 0x00E584
            )
            await ctx.send(embed = emb)
            cur.execute(f"UPDATE users SET rep_time = 10800 WHERE id = {ctx.message.author.id}")
            conn.commit()
    elif cur.execute(f"SELECT rep_time FROM users WHERE id = {member.id}").fetchone()[0] > 0:
        rep_cooldown = strftime('%Hh %Mm %Ss', gmtime(cur.execute(f"SELECT rep_time FROM users WHERE id = {ctx.message.author.id}").fetchone()[0]))
        emb = discord.Embed(
            description = f'{ctx.message.author.mention} Вы уже использовали очки репутации. Снова повысить репутацию вы сможете через **{rep_cooldown}.**',
            color = 0xff0000
        )
        await ctx.send(embed = emb)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-06-14
@Vindicar

Wrong architecture.
You don't need to count the time all the time - just remember when the command was sent by a given user, and the next time you run this command, check the remembered previous sending time and compare it with the current one. If the difference is greater than the cooldown, you can execute the command again.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question