Z
Z
zerno112021-07-16 18:28:34
Python
zerno11, 2021-07-16 18:28:34

Where is the error in writing discord.py economic bot?

I tried to make an economic bot, but when I enter the "!balance" command into the chat, no changes occur in the database (as I understand it, it has not been updated) after the "!beg" command
Here is the whole code:

import discord 
import json
import random as rand
from discord.ext import commands
import os

bot = commands.Bot(command_prefix = "!", intents = discord.Intents.all())
bot.remove_command( 'help' )
os.chdir("C:\\Users\\User\\Desktop\\test botik")

@bot.command()
async def balance(ctx):
    await open_account(ctx.author)

    user = ctx.author

    users = await get_bank_data()


    wallet_amt = users[str(user.id)]["wallet"] = 0
    bank_amt = users[str(user.id)]["bank"] = 0

    em = discord.Embed(title = f"{ctx.author.name}'s balance",color = discord.Color.red())
    em.add_field(name = "Wallet balance",value = wallet_amt)
    em.add_field(name = "Bank balance",value = bank_amt)
    await ctx.send(embed = em)


@bot.command()
async def beg(ctx):
    await open_account(ctx.author)

    users = await get_bank_data()
    user = ctx.author
    earnings = rand.randrange(101)


    await ctx.send(f"Кто-то дал тебе {earnings} монет!")



    users[str(user.id)]["wallet"] += earnings

    with open("mainbank.json","w") as f:
         json.dump(users,f)



async def open_account(user):
    
    users = await get_bank_data()

    if str(user.id) in users:
        return False
    else:
        users[str(user.id)]["wallet"] = 0
        users[str(user.id)]["bank"] = 0

    with open("mainbank.json","w") as f:
         json.dump(users,f)
    return True



async def get_bank_data():
    with open("mainbank.json","r") as f:
        users = json.load(f)


bot.run("")

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question