Discord.py - 经济系统

时间:2021-02-23 10:59:37

标签: python discord discord.py

有人可以帮我吗?我试图编写一个经济系统机器人,但它不起作用。当我尝试执行命令时,它只会给我一个很长的错误。代码如下:

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")

我真的不知道我做错了什么,但我对 discord.py 模块没有太多经验,所以如果有人能提供帮助就好了。

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)

1 个答案:

答案 0 :(得分:1)

如果您没有将您的 JSON 文件放入另一个文件夹中,您可以删除您的 os.chdir 路径,它将被找到。

快速提示:路径通常具有以下格式:C:/Users/user/...

尽管您在以下行中犯了一个小错误,但代码对我来说看起来不错:

await ctx.send(em)

如果你想发送一个嵌入,你总是必须定义它,所以正确的行是:

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

我还建议您始终将函数放在命令/实际代码之上:

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()

记得将 {} 放入您的 JSON 文件中。在您运行命令之前,它应该如下所示:

{

}