问题
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