【问题标题】:Im making this discord.py ecnomy bot and It not giving me any errors but it dosent send the embed我正在制作这个 discord.py 经济机器人,它没有给我任何错误,但它没有发送嵌入
【发布时间】:2021-10-01 23:54:21
【问题描述】:

我正在使用的代码:

@client.command()
async def buy(ctx,item,amount = 1):
    await open_account(ctx.author)

    res = await buy_this(ctx.author,item,amount)

    if not res[0]:
        if res[1]==1:
            await ctx.send("That Object isn't there!")
            return
        if res[1]==2:
            await ctx.send(f"You don't have enough money in your wallet to buy {amount} {item}")
            return
        if item in mainshop:
            name = item["name"].lower()
            p = item["price"]
            cost = p*amount
            return
        if cost == cost:

            embed = discord.Embed(color = 0xff0000,description = f"{ctx.author.mention} bought {amount} {item} for `{cost}` coins")
            embed.set_author(icon_url = bot_pfp,name = f"Succesful {item} purchase.")
            embed.set_footer(text ="Thank-You for your purchase")
            await ctx.send(embed=embed)

详情:

它没有给我错误,它确实为我购买了该项目......但它没有发送嵌入......如果我删除价格变量但我想保留它,嵌入工作。我尝试了很多东西,但似乎没有用。我会为每种类型的名称制作不同的 if 语句,但这需要太多的工作,而且很难。
这是buy_this 函数:

async def buy_this(user,item_name,amount):
    item_name = item_name.lower()
    name_ = None
    for item in mainshop:
        name = item["name"].lower()
        if name == item_name:
            name_ = name
            price = item["price"]
            break

    if name_ == None:
        return [False,1]

    cost = price*amount

    users = await get_bank_data()

    bal = await update_bank(user)

    if bal[0]<cost:
        return [False,2]

    try:
        index = 0
        t = None
        for thing in users[str(user.id)]["bag"]:
            n = thing["item"]
            if n == item_name:
                old_amt = thing["amount"]
                new_amt = old_amt + amount
                users[str(user.id)]["bag"][index]["amount"] = new_amt
                t = 1
                break
            index+=1 
        if t == None:
            obj = {"item":item_name , "amount" : amount}
            users[str(user.id)]["bag"].append(obj)
    except:
        obj = {"item":item_name , "amount" : amount}
        users[str(user.id)]["bag"] = [obj]        

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

    await update_bank(user,cost*-1,"wallet")

    return [True,"Worked"]

【问题讨论】:

  • 每个 if 块中都有一个 return,也许这就是我猜的原因......

标签: python discord discord.py


【解决方案1】:

这么简单的缩进错误:

        if item in mainshop:
            name = item["name"].lower()
            p = item["price"]
            cost = p*amount
            return
        if cost == cost:

            embed = discord.Embed(color = 0xff0000,description = f"{ctx.author.mention} bought {amount} {item} for `{cost}` coins")
            embed.set_author(icon_url = bot_pfp,name = f"Succesful {item} purchase.")
            embed.set_footer(text ="Thank-You for your purchase")
            await ctx.send(embed=embed)

if cost == cost: 不在if item in mainshop 内部,if item in mainshop 中的最后一行是一个返回,因此缩进循环 - if cost == cost: 并删除if item in mainshop 内部的返回

应该是:

        if item in mainshop:
            name = item["name"].lower()
            p = item["price"]
            cost = p*amount
            if cost == cost:
                embed = discord.Embed(color = 0xff0000,description = f"{ctx.author.mention} bought {amount} {item} for `{cost}` coins")
                embed.set_author(icon_url = bot_pfp,name = f"Succesful {item} purchase.")
                embed.set_footer(text ="Thank-You for your purchase")
                await ctx.send(embed=embed)
            else:
                await ctx.send("You don't have enough money to buy {name}")

【讨论】:

    【解决方案2】:

    您仅通过缩进发送嵌入if not res[0]。这可能是导致问题的原因。另外,使用if code == code 是没用的:还不如不写if 语句。

    附带说明,您可能希望从 buy_this 函数本身返回商品名称和价格,以确保您不必再次遍历整个 mainshop 变量。

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 2021-12-08
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2021-07-11
      相关资源
      最近更新 更多