Warn command discord.py (logging)

Shiqi58 Playz

So I wanted to add a warn command that logs, but I have no idea on how ou can do it. I looked at some of the other question here on stack overflow but they were just confusing. I don't know how it works. Maybe someone can help. Apparently, you need some sort of doc to track stuff so I added warnlogging.txt, but how do I run it and log the stuff there? Here is my current warn 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()

I did not add embeds because I am just tryna add this logging thing but I have no idea where to start.

Matan Ka.

Best way to to store warnings is probably json file:

import discord
from discord.ext import commands
import json

with open('reports.json', encoding='utf-8') as f:
    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")
  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:
      'reasons': [reason,]
  with open('reports.json','w+') as f:

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

Now user will be kicked after reaching 7 warnings. You can use command warnings <@user> to check users warnings by creating another command:

@client.command(pass_context = 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} has been reported {len(current_user['reasons'])} times : {','.join(current_user['reasons'])}**")
    await ctx.send(f"**{user.name} has never been reported**")

EDIT: you have to create a file called reports.json in same path/folder your bot is coded in.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment