警告命令 discord.py(日志记录)

时间:2021-02-24 19:33:19

标签: python discord.py

所以我想添加一个记录日志的警告命令,但我不知道如何做到这一点。我在此处查看了有关堆栈溢出的其他一些问题,但它们只是令人困惑。我不知道它是如何工作的。也许有人可以帮忙。显然,您需要某种文档来跟踪内容,所以我添加了warnlogging.txt,但是我如何运行它并在那里记录内容? 这是我当前的警告命令

@client.command()
@commands.has_permissions(view_audit_log = True)
async def warn(ctx, member : discord.Member, *, reason=None)
    await ctx.send(f"Member warned. {ctx.author} warned {member} for the following reason: "+reason)
    await ctx.message.delete()

我没有添加嵌入,因为我只是想添加这个日志记录,但我不知道从哪里开始。

1 个答案:

答案 0 :(得分:1)

存储警告的最佳方式可能是 json 文件:

|AR| beIN MOVIES 1 HD
GLORY 77 BOX
National 1
Nagarjuna
FOOTBALL
Aaryaa News

现在用户将在达到 7 个警告后被踢出。您可以使用命令警告 <@user> 通过创建另一个命令来检查用户警告:

import discord
from discord.ext import commands
import json



with open('reports.json', encoding='utf-8') as f:
  try:
    report = json.load(f)
  except ValueError:
    report = {}
    report['users'] = []



intents = discord.Intents.default()

intents.members = True
client = commands.Bot(command_prefix = "", intents = intents)



@client.command(pass_context = True)
@has_permissions(manage_roles=True, ban_members=True)
async def warn(ctx,user:discord.User,*reason:str):
  author = ctx.author
  if not reason:
    await ctx.send("Please provide a reason")
    return
  reason = ' '.join(reason)
  await ctx.send(f'**{user.mention} has been warned by {author.name}.**')
  await user.send(f'You have been warned in **{ctx.guild.name}** by **{author.name}**.')
  for current_user in report['users']:
    if current_user['name'] == user.name:
      current_user['reasons'].append(reason)
      break
  else:
    report['users'].append({
      'name':user.name,
      'reasons': [reason,]
    })
  with open('reports.json','w+') as f:
    json.dump(report,f)

  with open('reports.json','w+') as f:
    json.dump(report,f)
  if len(report['users']) >= 7:
    await user.kick(reason='You reached 7 warnings')

编辑:您必须在编码机器人的同一路径/文件夹中创建一个名为 reports.json 的文件。