gpt4 book ai didi

python - DIScord 命令认为 bool 为 True

转载 作者:行者123 更新时间:2023-12-04 08:57:41 24 4
gpt4 key购买 nike

我有 3 个命令。 !disablepugs、!enablepugs 和 !join。 !disablepugs 将变量设置为 False,而 !enablepugs 将变量设置为 true。但是,变量更改得很好。但是,当我在 !join 命令中检查变量是否等于 True 时,它​​仍然没有检测到变化。代码:

#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是一个全局变量。您可以从任何范围访问全局变量,但是每当您尝试更改它们的值时,您都会创建一个具有相同名称的局部变量,并且只修改该局部变量。您必须显式地将全局变量“ Hook ”到您的作用域中以修改全局值。

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

关于python - DIScord 命令认为 bool 为 True,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63731372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com