-2

我对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")

如果有人可以提供帮助,那就太好了 :) 在此先感谢!

编辑 由于我没有说明手头的问题(这里是菜鸟),这是一个示例输出:

    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

如您所见,输出表明客户端没有管理员,尽管客户端 db id 对应于列表中具有管理员 id 的条目。

4

1 回答 1

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

独库:any()map()

于 2018-03-22T20:55:40.397 回答