Python 限制命令

Python 限制命令,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我得到了这个代码,它阻止每个人访问?hello命令,除了id列表中的用户id之外,所以我修改了它,将ctx.message.author.id替换为ctx.message.server.id,现在它也绕过了服务器。所以我添加了这两个代码,但现在它不适用于用户和服务器,只适用于任何人。如何使其同时适用于服务器和用户 LIST_OF_SERVER_IDS = ['3557657657647676', '36567565756766767'] LIST_OF_USER_IDS = ['35576576

我得到了这个代码,它阻止每个人访问
?hello
命令,除了
id列表中的用户id
之外,所以我修改了它,将
ctx.message.author.id
替换为
ctx.message.server.id
,现在它也绕过了服务器。所以我添加了这两个代码,但现在它不适用于用户和服务器,只适用于任何人。如何使其同时适用于服务器和用户

LIST_OF_SERVER_IDS = ['3557657657647676', '36567565756766767']
LIST_OF_USER_IDS = ['3557657657647676', '36567565756766767'] 

def is_any_user(ids):
    def predicate(ctx):
        return ctx.message.author.id in ids
    return commands.check(predicate)

def is_any_server(ids):
    def predicate(ctx):
        return ctx.message.server.id in ids
    return commands.check(predicate)

@bot.command(pass_context=True)
@is_any_user(LIST_OF_USER_IDS)
@is_any_server(LIST_OF_SERVER_IDS)
async def hello(ctx):
     await bot.say("Hello {}".format(ctx.message.author.mention))

通过定义两个
is\u any\u user
decorator,您只需保留最近定义的,并丢失第一个。给服务器check一个更具代表性的名称(毕竟,它没有检查
User
s,为什么名称中有“User”)。然后我们可以保留两个ID的白名单。由于
commands.check
可以链接,我们只需用这两个检查来装饰我们的命令

LIST_OF_SERVER_IDS = [3557657657647676, 36567565756766767, 343657687786435432]
LIST_OF_USER_IDS = [1, 2, 3]  
# Replace as needed.  Keep in mind that 0.16 ids are strings and 1.0 ids are integers


def is_any_user(ids):
    def predicate(ctx):
        return ctx.message.author.id in ids
    return commands.check(predicate)

def is_any_server(ids):
    def predicate(ctx):
        return ctx.message.server.id in ids
    return commands.check(predicate)

@bot.command(pass_context=True)
@is_any_user(LIST_OF_USER_IDS)
@is_any_server(LIST_OF_SERVER_IDS)
async def hello(ctx):
     await bot.say("Hello {}".format(ctx.message.author.mention))
编辑:

使用这些版本的检查来尝试和调试正在发生的事情

def is_any_user(ids):
    def predicate(ctx):
        if ctx.message.author.id in ids:
            print("Good author")
            return True
        else:
            print("Bad author")
            return False
    return commands.check(predicate)

def is_any_server(ids):
    def predicate(ctx):
        if ctx.message.server.id in ids:
            print("Good server")
            return True
        else:
            print("Bad server")
            return False
    return commands.check(predicate)
编辑2:

您可以编写一个检查,查看某人是否在用户白名单中,或者是否从服务器白名单上的服务器访问该命令

def whitelists(users, servers):
    def predicate(ctx):
        return ctx.message.author.id in users or ctx.message.server.id in servers
    return commands.check(predicate)

@bot.command(pass_context=True)
@whitelists(LIST_OF_USER_IDS, LIST_OF_SERVER_IDS)
async def hello(ctx):
     await bot.say("Hello {}".format(ctx.message.author.mention))

@Demory我想这些不是您在程序中使用的ID。您应该在运行bot的终端中看到一些输出。这至少可以告诉您哪些检查失败了。@Demory我在上面添加了一些怀疑语句。试着运行这些以查看检查失败的地方。仔细检查列表中的ID(如果使用的是0.16版本,请确保它们是字符串;如果使用的是1.0版本,请确保它们是ints)。你在终端中看到任何错误了吗?当我在列表中尝试
is any\u user
时,我得到了
Good author Good server
,但当我尝试
is any\u server
时,我得到了一个很大的错误,我将在上面添加。我不明白。只有在服务器检查成功运行的情况下,才能获得
Good Server
消息。您遇到了什么错误?可能我误解了,但是如果您的id是用户id列表中唯一的id,那么当其他人调用该命令时,该命令应该失败。你是说其他用户即使不在用户ID列表中也可以使用该命令吗?