DIscord命令认为bool为True



我有3个命令!残疾人!enablepugs和!参加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

有什么原因吗?我被难住了。。。

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

最新更新