不和谐指挥部认为布尔是真的

2024-06-16 08:27:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三个命令!哈巴狗!使能帕格斯和!参加disablepugs将变量设置为False,并且!enablepugs将变量设置为true。但是,变量的变化很好。但是,当我检查变量是否等于中的True时!join命令,它仍然没有检测到更改。代码:

#Set default to True, shouldn't matter too much
pugs_enabled = True
@client.command()
async def disablepugs(ctx):
    user = ctx.message.author.name

    if user == "*My Name Here*":
        pugs_enabled = False
        await ctx.send("``Pugs are temporarily disabled.``")
        print(f"Set to {pugs_enabled}")
@client.command()
async def enablepugs(ctx):
    user = ctx.message.author.name

    if user == "*My Name Here*":
        pugs_enabled = True
        await ctx.send("``Pugs are now enabled.``")
        print(f"Set to {pugs_enabled}")
@client.command()
async def join(ctx):
    if helper.is_setup(ctx):
        print(f"The pug variable is set to {pugs_enabled}")
        if pugs_enabled is True:
            #Not detecting bool change. Still thinks it's false

你知道为什么吗?我被难住了


Tags: to命令clienttrueasyncifisdef
1条回答
网友
1楼 · 发布于 2024-06-16 08:27:45

pugs_enabled是一个全局变量。您可以从任何范围访问全局变量,但无论何时尝试更改它们的值,您都会创建一个具有相同名称的局部变量,并且只修改该局部变量。您必须显式地将全局变量“挂钩”到您的作用域中,以修改全局值

pugs_enabled = True

@client.command()
async def disablepugs(ctx):
    user = ctx.message.author.name

    if user == "*My Name Here*":
        global pugs_enabled
        pugs_enabled = False
        await ctx.send("``Pugs are temporarily disabled.``")
        print(f"Set to {pugs_enabled}")

@client.command()
async def enablepugs(ctx):
    user = ctx.message.author.name

    if user == "*My Name Here*":
        global pugs_enabled
        pugs_enabled = True
        await ctx.send("``Pugs are now enabled.``")
        print(f"Set to {pugs_enabled}")

@client.command()
async def join(ctx):
    if helper.is_setup(ctx):
        print(f"The pug variable is set to {pugs_enabled}")
        if pugs_enabled is True:
            #Not detecting bool change. Still thinks it's false

相关问题 更多 >