我该怎么做,以便当有人在discord.py中发出3条警告时,我的机器人自动踢人

时间:2020-05-30 19:08:18

标签: discord.py

这是我的警告系统代码:

@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条警告时,会被踢出..

还有一点免责声明..我自己没有编写所有代码,而是从堆栈中取出并进行了修改

1 个答案:

答案 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!")