我对python很新,我想知道是否有人可以帮助我... 我正在尝试编写一个脚本来检查我的TS3服务器上是否有管理员。但是一整天后我都想不出来。
我使用的api链接在这里: http://py-ts3.readthedocs.io/en/latest/index.html
import time
import ts3
def admincheck(nickname, id):
while True:
client = ts3conn.clientdbfind(pattern=nickname)
group = ts3conn.servergroupclientlist(sgid=id)
for client in client:
print("client db id:\n" + (client["cldbid"]))
for group in group:
print("client db ids with admin:\n" + (group["cldbid"]))
time.sleep(1.5)
if int(client["cldbid"]) in range(int(group["cldbid"])):
print("client has admin")
else:
print("client does not admin")
with ts3.query.TS3Connection("localhost") as ts3conn:
ts3conn.login(
client_login_name="serveradmin",
client_login_password="<ommited>"
)
ts3conn.use(sid=1)
admincheck("test", "6")
我认为我的问题出在这一部分:
if int(client["cldbid"]) in range(int(group["cldbid"])):
print("client has admin")
else:
print("client does not admin")
如果有人可以提供帮助,那就太棒了:) 提前谢谢!
修改 由于我没有说明手头的问题(noob here),这是一个示例输出:
client db id:
2
client db ids with admin:
2
client db ids with admin:
3
client db ids with admin:
4
client does not admin
正如您所看到的,输出显示客户端没有admin,尽管客户端db id对应于列表中包含具有admin的ID的条目。
答案 0 :(得分:0)
问题:
def admincheck(nickname, id):
while True:
client = ts3conn.clientdbfind(pattern=nickname)
group = ts3conn.servergroupclientlist(sgid=id)
# you are overwriting your client-list with its first element
# both have the same name
for client in client:
print("client db id:\n" + (client["cldbid"]))
# you are overwriting your group-list with its first element
# both have the same name
for group in group:
print("client db ids with admin:\n" + (group["cldbid"]))
time.sleep(1.5)
# you should check if any client-id is equal to any inside group-ids
# instead you do something with range - wich does not make much sense?
if int(client["cldbid"]) in range(int(group["cldbid"])):
print("client has admin")
else:
print("client does not admin")
如果您需要转换多个数字,请使用map:
text = [ "1","12","123","1234","12345"]
asInt = list( map(int,text)) # apply int() to each part of text, returns iterable
print(asInt) # [1, 12, 123, 1234, 12345]
ids = ["5","7","1"]
idNums = list(map(int,text)) # same
print(any(x in asInt for x in idNums)) # checks if any x from idNums is
# inside asInt (your groupIds)
输出:
[1, 12, 123, 1234, 12345]
True