Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的例外被忽略了-有人能帮我吗?discord.py重写_Python_Json_Discord_Discord.py - Fatal编程技术网

Python 我的例外被忽略了-有人能帮我吗?discord.py重写

Python 我的例外被忽略了-有人能帮我吗?discord.py重写,python,json,discord,discord.py,Python,Json,Discord,Discord.py,我有个问题。当我尝试使用try/except语句时,失败了。例外情况正在被忽略。我可以像我希望的那样阻止a,我可以阻止我自己,以及Ownerid中的用户。例外情况只是被忽略了。我不知道我做错了什么。使用if语句也失败了,我在那里遇到了一个错误。所以我试着用这个。我也会分享我的json文件。我希望看到任何建议或代码,可以帮助我 from discord.ext import has_permissions @commands.command(name='blockuser') @has_permi

我有个问题。当我尝试使用try/except语句时,失败了。例外情况正在被忽略。我可以像我希望的那样阻止a,我可以阻止我自己,以及Ownerid中的用户。例外情况只是被忽略了。我不知道我做错了什么。使用if语句也失败了,我在那里遇到了一个错误。所以我试着用这个。我也会分享我的json文件。我希望看到任何建议或代码,可以帮助我

from discord.ext import has_permissions
@commands.command(name='blockuser')
@has_permissions(administrator=True)
@commands.guild_only()
async def blockuser(self, ctx, user: discord.User):
    with open('./bot_config/blocked_users.json', 'r+') as json_file:
        json_dict = json.load(json_file)
        blockedUser = json_dict["blockedUser"]
    try:
        self.bot.blocked_users.append(user.id)
        json_dict["blockedUser"].append(user.id)
        Cogs._json.write_json(json_dict, "blocked_users")
        return
    except:
        if ctx.message.author.id in ctx.message.content:
            await ctx.send(f'You cannot block yourself, {ctx.author.mention}.')
            return
    try:
        self.bot.blocked_users.append(user.id)
        json_dict["blockedUser"].append(user.id)
        Cogs._json.write_json(json_dict, "blocked_users")
        return
    except:
        if user.id in blockedUser:
            await ctx.send(f'The user {user.mention} is already blocked.')
            return
    try:
        self.bot.blocked_users.append(user.id)
        json_dict["blockedUser"].append(user.id)
        Cogs._json.write_json(json_dict, "blocked_users")
        return
    except:
        if ownerid in ctx.message.content:
            await ctx.send(f"The Botowner {botowner} can't be blocked.")
            return
    try:
        if user.id not in json_dict:
            self.bot.blocked_users.append(user.id)
            json_dict["blockedUser"].append(user.id)
            Cogs._json.write_json(json_dict, "blocked_users")
            await ctx.send(f'User was blocked: {user.mention}')
            return
    except:
        await ctx.send("Blocking the User failed. Type either in the id, name or mention the user.")
        return
这是我的json文件。如你所见,ID在那里有两次

{
    "blockedUser": [
        450949112230314014,
        450949112230314014
    ]
}

除外:
仅适用于运行时错误。例如,你可以

name=“鲍勃”
尝试:
newName=int(名称)
除值错误外:
打印(“值错误!”)

由于
name
是一个字符串,因此
try
无法将其转换为整数。这将触发ValueError异常,导致print语句正常工作。我不完全确定您的代码试图做什么,但看起来嵌套条件应该可以正常工作。

您的异常被忽略,因为您正在使用纯try-except子句忽略它们try/except子句需要处理异常。您没有指定要捕获的任何类型的错误