ctx 和 guild 参数在命令中不起作用并显示错误

时间:2020-12-25 10:39:09

标签: python discord discord.py

我有命令 custom_role:

@Bot.command()
async def custom_role(ctx, colour: str, *, name: str):
    colour = discord.Color(value=int(colour, 16))
    print(colour)
    await open_account(сtx.author)

    user = ctx.author

    bal = await update_bank(user)

    if 1000>bal:
        await ctx.send("You don't have that much money")
        return

    await ctx.guild.create_role(name = name, colour=colour)

    await update_bank(user, -1000, "wallet")

    role = discord.utils.get(guild.roles, name = name)
    
    await user.add_roles(role)

    emb = discord.Embed(description = "You bought custom role for a week!", color = 0x2ecc71)
    await ctx.send(embed = emb)

    await asyncio.sleep(604800)
    await user.remove_roles(role)

但我有两个错误:

未定义变量 'сtx'

未定义变量'公会'

完整回溯:从 exc 引发 CommandInvokeError(exc) discord.ext.commands.errors.CommandInvokeError:命令引发异常:NameError:未定义名称“сtx”

这很奇怪,因为在其他命令中ctx参数效果很好

此命令中使用了辅助函数:

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

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

    return True



async def update_bank(user, change = 0, mode = "wallet"):
    users = await get_bank_data()

    users[str(user.id)][mode] += change

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

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

    return bal

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

      return users 

如果你知道如何解决这个问题,请回答

1 个答案:

答案 0 :(得分:0)

命令应该是这样的:

@Bot.command()
async def custom_role(ctx, colour: str, *, name: str):
    colour = discord.Color(value=int(colour, 16))
    print(colour)
    user = ctx.author
    await open_account(user)

    bal = await update_bank(user)

    if 1000>bal:
        await ctx.send("You don't have that much money")
        return

    await ctx.guild.create_role(name = name, colour=colour)

    await update_bank(user, -1000, "wallet")

    role = discord.utils.get(ctx.guild.roles, name = name)

    await user.add_roles(role)

    emb = discord.Embed(description = "You bought custom role for a week!", color = 0x2ecc71)
    await ctx.send(embed = emb)

    await asyncio.sleep(604800)
    await user.remove_roles(role)

因为我忘记在“guild.roles”之前加上“ctx”