【发布时间】:2020-10-09 18:37:50
【问题描述】:
所以我有这段代码选择列 userID 中的每一行,将返回的值放入字典然后打印出来。
whitelist = []
with db.cursor() as cursor:
whitelist_get = "SELECT userID FROM whitelist;"
cursor.execute(whitelist_get)
whitelist = str(cursor.fetchall())
print(whitelist)
这段代码会检查 discord 中的作者 ID 是否在该白名单中。
async def _function(ctx):
global whitelist
if ctx.message.author.id in whitelist:
print("access granted")
else:
print("access not granted")
但是,在运行脚本时,代码会超过 global whitelist,然后它不会在 if 语句或 else 语句中执行。它什么也不做。它也不打印任何东西。我做错什么了吗?
编辑:我的作者 ID 在 MySQL 表中,它应该打印 access granted 但它根本不打印任何内容。
EDIT2:通过将if ctx.message.author.id in whitelist: 更改为if str(ctx.message.author.id) in whitelist: 解决
【问题讨论】:
-
你能分享一个截图显示你是如何调用这个函数的吗?
-
@GPhilo 嗨,这是一个不和谐的机器人命令,所以它在
async def之前只有@bot.command(name="function"),并且每当我键入命令“simi function”时都会调用该函数。
标签: python mysql discord.py-rewrite