discord.py 使用自定义检查黑名单

时间:2021-02-18 08:46:37

标签: python discord.py

我想做什么:我遇到一些人滥用我的机器人的命令,所以我想阻止这种情况。因此,在查看了多个代码源后,我将成员 ID 插入到 .txt 文档中。

我的问题:我的“自定义检查”出现错误。我发现的所有自定义检查示例都在 cogs 中,或者与我尝试做的事情无关,因此我还没有按预期工作。请注意,我也没有使用齿轮。

代码

def blacklist(ctx): # this is the def used for the check, it reads the .txt file for user
    file = open("blacklist.txt", "r")
    members_banned = file.readlines()
    if str(ctx.author.id) not in members_banned:
        return ctx.author.id not in members_banned

@client.command() # this command is used for adding people to the blacklist. This is most likely not the problem..
@commands.is_owner()
async def blacklist(ctx, member: discord.Member):
    file = open("blacklist.txt", "r")
    anything = False
    while anything == False:
        content = file.read()
        things = content.split("\n")
        for line in things:
            if str(line) == str(member.id):
                await ctx.send(f"{member.name} is already in blacklist, please remove manually!")
                anything = True
            else:
                pass
        if anything == False:
            thing = open("blacklist.txt", "a")
            thing.write(f"\n{member.id}")
            thing.close()
            anything = True
            await ctx.send(f"{ctx.author.mention}: {member.name} has been blacklisted, hopefully")
        file.close()
        break

@client.command()
@commands.check(blacklist) # This is the code I used to test the custom check for the blacklist
async def bltest(ctx):
    await ctx.send("Cool, you're not blacklisted")

错误:(通过此代码)

    return await self.callback(*args, **kwargs)
TypeError: blacklist() missing 1 required positional argument: 'member'

上述错误对我来说没有任何意义。

其他

.txt 文件

My text file with the ids

我看过的其他代码:

2 个答案:

答案 0 :(得分:1)

您的两个函数具有相同的名称。命令名为 blacklist,带有 ctxmember 参数,而函数的名称是 blacklist,只有 ctx 作为参数。这意味着如果选择了错误的一个,您将调用一个需要 2 个参数且只给它 1 个参数的函数。

给第一个函数(读取文件的函数)一个不同的名字,比如“check_blacklist”或“blacklisted”或任何最适合你的名字。

答案 1 :(得分:-2)

正如它在第一行代码的错误中所说的那样,将成员作为参数添加到函数黑名单中。您需要它,因为在此函数中您正在处理不和谐服务器的成员。