这是我的警告系统代码:
@commands.has_permissions(manage_messages=True)
async def warn(ctx,user:discord.User,*reason:str):
if not reason:
await ctx.send("Du skal lige give mig en grund til din Advarsel!")
return
reason = ' '.join(reason)
for current_user in report['users']:
if current_user['name'] == user.name:
current_user['reasons'].append(reason)
break
else:
await ctx.send(f"{user.name} er blevet advaret med grunden: '{reason}'")
report['users'].append({
'name':user.name,
'reasons': [reason,]
})
with open('reports.json','w+') as f:
json.dump(report,f)
@client.command()
@commands.has_permissions(manage_messages=True)
async def warnings(ctx,user:discord.User):
for current_user in report['users']:
if user.name == current_user['name']:
await ctx.send(f"{user.name} er blevet advaret {len(current_user['reasons'])} gange : {','.join(current_user['reasons'])}")
break
else:
await ctx.send(f"{user.name} er aldrig blevet advaret!")
我要问的是提供帮助的方法,因此当它们在我的report.json文件中有3条警告时,会被踢出..
还有一点免责声明..我自己没有编写所有代码,而是从堆栈中取出并进行了修改
答案 0 :(得分:1)
每当警告某人时,都要检查3条警告,并使用discord.Member
代替discord.User
。
@commands.has_permissions(manage_messages=True)
async def warn(ctx,user:discord.Member,*reason:str):
if not reason:
await ctx.send("Du skal lige give mig en grund til din Advarsel!")
return
reason = ' '.join(reason)
for current_user in report['users']:
if current_user['name'] == user.name:
current_user['reasons'].append(reason)
break
else:
await ctx.send(f"{user.name} er blevet advaret med grunden: '{reason}'")
report['users'].append({
'name':user.name,
'reasons': [reason,]
})
with open('reports.json','w+') as f:
json.dump(report,f)
if len(report['users']) >= 3:
await user.kick(reason='You reached 3 warnings')
@client.command()
@commands.has_permissions(manage_messages=True)
async def warnings(ctx,user:discord.Member):
for current_user in report['users']:
if user.name == current_user['name']:
await ctx.send(f"{user.name} er blevet advaret {len(current_user['reasons'])} gange : {','.join(current_user['reasons'])}")
break
else:
await ctx.send(f"{user.name} er aldrig blevet advaret!")