我需要在函数内将值从一个函数传递到下一个函数。
例如(我的 IRC 机器人被编程为响应频道中的命令):
def check_perms(nick,chan,cmd):
sql = "SELECT `"+ cmd +"` FROM permissions WHERE nick = '"+ nick +"' and chan = '"+ chan +"'" # This returns 0
#sql = "SELECT `restart` FROM permissions WHERE nick = 'Me' and chan = '#mychan'" # this works as intended
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
if (row[0] == 1): # Nick logged in and has permission
return 1
else: # nick does not have permissions
return 0
def com_restart(nick,chan):
perm = check_perms(nick,chan,"restart")
if (perm == 0): # nick did not have permission
irc.send("NOTICE "+ nick +" :Permission denied.\n")
elif (perm == 1): # nick has permission
irc.send("PRIVMSG "+ chan +" :I've been asked to restart myself by "+ nick +".\n")
nick = "Me" # This is determined by a bunch of regex splits and such
chan = "#mychan" # This is determined by regex splits as well
com_restart(nick,chan)
但是,当我尝试这个时,似乎这些值没有传递给 SQL 查询,所以它返回 0。
感谢您的任何帮助,您可以提供。
编辑 - 添加了我现在正在使用的代码。