Jonathon
Jonathon

Reputation: 1591

DIscord command thinks bool is True

I have 3 commands. !disablepugs, !enablepugs and !join. !disablepugs set's a variable to False, and !enablepugs sets a variable to true. However, the variable is changed just fine. But, when I check if the variable is equal to True in the !join command, it's still not detecting the change. Code:

#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

Any ideas as to why? I'm stumped...

Upvotes: 2

Views: 449

Answers (1)

Niklas Mertsch
Niklas Mertsch

Reputation: 1489

pugs_enabled is a global variable. You can access global variables from any scope, but whenever you try to change their value, you instead create a local variable with the same name and only modify that local variable. You have to explicitly "hook" the global variable into your scope to modify the global value.

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

Upvotes: 2

Related Questions