所以我正在制作一个警告系统,我快完成了,但有一个问题我必须添加一个明确的警告命令,该命令可以清除特定用户的所有警告......但不知道如何。这是我的代码-
@client.command(pass_context = True)
@commands.has_permissions(manage_roles=True, ban_members=True)
async def warn(ctx,user:discord.User,*reason:str):
if not reason:
await ctx.send("Please provide a reason")
return
reason = ' '.join(reason)
for current_user in report['users']:
if current_user['name'] == user.name:
current_user['reasons'].append(reason)
await ctx.send("reported sir!")
await user.send(f"u have been reported for :{reason}")
break
else:
report['users'].append({
'name':user.name,
'reasons': [reason,]
})
with open('reports.json','w+') as f:
json.dump(report,f)
@client.command(pass_context = True, aliases=["warns"])
async def warnings(ctx,user:discord.User):
for current_user in report['users']:
if user.name == current_user['name']:
embed = discord.Embed(title="Reports:", description=(f"{user.name} has been reported {len(current_user['reasons'])} times : {','.join(current_user['reasons'])}"))
await ctx.send(embed= embed)
break
else:
await ctx.send(f"{user.name} has never been reported")
答案 0 :(得分:0)
首先,让我指出您代码中的错误。
async def warn(ctx,user:discord.User,*reason:str):
if not reason:
await ctx.send("Please provide a reason")
return
if
块不会被执行,因为原因没有默认值,请使用 reason:str = None
设置默认值,否则机器人将在没有原因的情况下引发错误。< /p>
report['users'].append({
'name':user.name,
'reasons': [reason,]
})
您在此处发布的代码中未定义报告,请确保您阅读了 json 文件并获取了数据,否则您可能会丢失一些数据。
终于解决了
@client.command()
async def clear_warn(ctx, member: discord.Member):
with open('reports.json', 'r') as f:
records = json.load(f)
if member.name in records['users']:
index = [record['name'] for record in records['users']].index(member.name)
records.pop(index)
await ctx.send('removed all warns')
else:
await ctx.send('member has no warns')
我会在你的代码中指出的一些事情是