首页 > 解决方案 > 检查用户 id 是否在用户 id 列表中以允许命令访问 - discord.py

问题描述

我对python相当陌生,并且一直在和我和几个朋友一起为服务器编写一个不和谐的机器人作为练习。我添加了一些命令,如果所有用户都可以随时访问它们,并且一直在尝试添加一种方法来启用和禁用对它们的访问,而不使用不和谐角色而是使用 python 列表,那么这些命令可能会非常具有破坏性。我添加了一个命令,可以用来将他们的用户 ID 添加到列表中,但是当用户尝试使用该命令时,他们会收到权限错误。

下面的代码是来自 cog 的有问题的代码

modlist = []

    def ListCheck():
        async def IsInList(ctx):
            member = ctx.message.author.id
            return member in modlist
        return commands.check(IsInList)

    @commands.command()
    async def AddUser(self, ctx, *, question):
        modlist.append(question)

    @commands.command()
    async def ViewModList(self, ctx):
        await ctx.send('User ids that have access:')
        for i in range(0, len(modlist)):
            await ctx.send(modlist[i])

    @commands.command()
    async def ClearModList(self, ctx):
        modlist.clear()
        await ctx.send('Modlist cleared')

    @commands.command()
    @ListCheck()
    async def PermTest(self, ctx):
        await ctx.send('user is in modlist')

任何帮助将不胜感激

标签: discord.py

解决方案


一种方法是使用commands.check(). 下面代码中的函数检查消息作者 ,ctx.author是否在给定列表中, modList。如果调用“test”命令时作者在此列表中,则机器人将发送“你是一个模组!”,否则将引发检查错误。

modList = [394506589350002688, 697725862128386048] # just some ids as examples

def check_Mod(ctx):
    if ctx.author.id in modList:
        return ctx.author.id in modList

@commands.command()
@commands.check(check_Mod)
async def test(ctx):
    await ctx.send("You are a mod!")

上述工作如下图所示:

工作模式检查命令


要回答is there is a way to add user ids to the list via a command?,您可以使用 json 或文本文件。在这种情况下,我将使用文本文件,因为它更简单,而且我的 json 不像其他人那样流畅。

这是您的文本文件的外观示例:

394506589350002688
697725862128386048

首先你需要读取文件并检查作者的id是否在文件中。我们将check_Mod为此更改功能。如果您想了解更多关于阅读文本文件的信息,或者觉得您可能需要一种更有效的方法,您可以看看这个:如何在文本文件中搜索字符串?

def check_Mod(ctx):
    with open('Mod.txt') as f: # do change the 'Mod.txt' to the name that suits you. Ensure that this file is in the same directory as your code!
        if str(ctx.author.id) in f.read(): # this is reading the text file and checking if there's a matching string
            return ctx.author.id        

现在将作者 ID 写入您的文本文件。

@commands.command()
@commands.check(check_Mod)
async def add_Mod(ctx, user:discord.Member=None):
    if user == None:
        await ctx.send("Please provide a user to add as a Mod!")
        return

    # First we'll make some functions for cleaner, more readable code #

    def is_Mod(user_id): 
    ## This function will check if the given id is already in the file. True if in file, False if not ##
        with open('Mod.txt', 'r') as f:
            if str(user_id) in f.read():
                return True
            else:
                return False

    def add_Mod(user_id):
    ## This function will add the given user id into the given text file, Mod.txt ##
        with open('Mod.txt', 'a') as f: # 'a' is used for appending, since we don't want to overwrite all the ids already in the file
            f.write(f"{str(user_id)}\n")
            f.close()

    # Now we put those functions to use #
    if is_Mod(user.id) == True:
        await ctx.send(f"The user {user} is already a Mod!")
    else:
        add_Mod(user.id)
        await ctx.send(f"{user} added as a Mod!")

上面的工作应该如下图所示。

工作 add_mod 命令

ps 如果您有任何其他问题,请将它们作为新问题发布,谢谢!


推荐阅读