Can someone please help me? I was trying to code an Economy System Bot, but it doesn't work. It just gives me a very long Error when i try to execute a command. Here's the code:
import discord
from discord.ext import commands
import json
import os
import random
os.chdir("C:\\Users\\user\\Desktop\\PythonEconomyBot")
client = commands.Bot(command_prefix = "eco ")
@client.event
async def on_ready():
print("Bot ist online!")
@client.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"]
bank_amt = users[str(user.id)]["bank"]
em = discord.Embed(title = f"{ctx.author.name}'s Kontostand",color = discord.Color.red())
em.add_field(name = "Wallet", value = wallet_amt)
em.add_field(name = "Bank",value = bank_amt)
await ctx.send(em)
@client.command()
async def beg(ctx):
await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()
earnings = random.randrange(101)
await ctx.send(f"Jemand gab dir {earnings} Moneten!")
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)] = {}
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)
return users
client.run("My Token")
I really don't know what i did wrong, but i don't have really much experience with the discord.py module, so would be nice if someone could help.
Here's the Error: File
"C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-
packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-
packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-
packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
If you do not put your JSON file into another folder you can remove your os.chdir
path, it will be found.
Quick tip: Paths normally have the following format: C:/Users/user/...
The code looks fine to me although you did a little mistake in the following line:
await ctx.send(em)
If you want to send an embed you always have to define it, so the right line would be:
em = discord.Embed(title = f"{ctx.author.name}'s Kontostand",color = discord.Color.red())
em.add_field(name = "Wallet", value = wallet_amt)
em.add_field(name = "Bank",value = bank_amt)
await ctx.send(embed=em) # Added embed
I would also suggest you always put the functions on top of the commands/actual code:
import discord
from discord.ext import commands
import json
import random
async def open_account(user):
users = await get_bank_data()
if str(user.id) in users:
return False
else:
users[str(user.id)] = {}
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)
return users
@client.command()
Remember to put {}
into your JSON file. It should look like this before you run a command:
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.