NoneType object has no attribute id [closed]

ε祈祈猫儿з 提交于 2020-06-23 18:11:12

问题


It should take the role ID next to the server ID in Json and define it as a silent role, but it gives an error and I don't know what the error is. Please help me

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'id'

def test1(guild_id: str):
        with open('test.json', "r") as f:
            test = json.load(f)
        return test[guild_id]

    @client.command()
    async def test(ctx):
        with open('test.json', 'r') as f:
            test = json.load(f)
            test1 = test[str(ctx.guild.id)]

            await ctx.send(f"{test1}")

    @client.command()
    async def mute(ctx, member: discord.Member, reason: str = "None"):    
      with open('test.json', 'r') as f:
        test = json.load(f)
        test1 = test[str(ctx.guild.id)]

        muted_role = discord.utils.get(ctx.guild.roles, id=test1)
        await member.add_roles(muted_role, reason = reason)

回答1:


You can use an if statement, e.g.if muted_role is not None

Hope this helps.



来源:https://stackoverflow.com/questions/62396279/nonetype-object-has-no-attribute-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!