简体   繁体   English

Discord.py - 经济系统

[英]Discord.py - Economy system

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.我真的不知道我做错了什么,但我对 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)

If you do not put your JSON file into another folder you can remove your os.chdir path, it will be found.如果你不把你的 JSON 文件放到另一个文件夹中,你可以删除你的os.chdir路径,它会被找到。

Quick tip: Paths normally have the following format: C:/Users/user/...快速提示:路径通常具有以下格式: 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.请记住将{}放入您的 JSON 文件中。 It should look like this before you run a command:在运行命令之前它应该是这样的:

{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM