首页 > 解决方案 > 我在使用不和谐机器人修改 json 文件时遇到了一些问题。如何解决这个问题?

问题描述

如果用户有权禁止成员,我希望机器人使此命令可操作,但是当我运行代码时,由于下面代码中的这一行@commands.has_permissions(ban_member=True)是我的代码中的第 42行,我得到了错误

如果我删除@commands.has_permissions(ban_member=True)它正在工作,但任何人都可以修改我不想发生的 json 文件。

如何修复此错误?

def write_json(data,filename="blacklistedWords.json"):
    with open(filename,"w") as f:
        json.dump(data,f,indent=4)

@bot.command(aliases=['awb'])
@commands.has_permissions(ban_member=True)
async def addtoblacklist(ctx,*,word):
    with open("blacklistedWords.json") as json_file:
        data = json.load(json_file)
        temp = data["blacklistedWords"]
        temp.append(word)
    write_json(data)
Traceback (most recent call last):
  File "C:\Users\ACER\Desktop\Json read write\botRead.py", line 42, in <module>
    @commands.has_permissions(ban_member=True)
  File "C:\Users\ACER\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 1779, in has_permissions
    raise TypeError('Invalid permission(s): %s' % (', '.join(invalid)))
TypeError: Invalid permission(s): ban_member

标签: pythonjsondiscorddiscord.py

解决方案


权限可能不正确,有权限ban_members(注意复数)https://discordpy.readthedocs.io/en/stable/api.html#discord.Permissions.ban_members


推荐阅读