0
votes

I've been working on a moderator discord bot. Made all the command except the warn command. Can anyone help me to make a warn command.

If the member (with manage member permission) types ?warn @user reason the bot will save the warning in a .json file.

And if the user says ?warnings @user the bot will show the warnings of the user.

1
Please take some time to read how to ask and how to create a complete and verifiable example. Maybe it's better for you to also read how to ask - Simo
What part are you struggling with? Are you asking how to write files, how to read files, how to identify a user from the command arguments...? - Patrick Haugh
I'd love to help you with this problem, but there's not enough information for me to assist. You also never asked a question, you just stated things that you want the command to do. - J0hn

1 Answers

4
votes

You could do something like this

import discord
from discord.ext.commands import commands,has_permissions, MissingPermissions
import json

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

client = discord.ext.commands.Bot(command_prefix = '?')

@client.command(pass_context = True)
@has_permissions(manage_roles=True, ban_members=True)
async def warn(ctx,user:discord.User,*reason:str):
  if not reason:
    await client.say("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)
      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)
async def warnings(ctx,user:discord.User):
  for current_user in report['users']:
    if user.name == current_user['name']:
      await client.say(f"{user.name} has been reported {len(current_user['reasons'])} times : {','.join(current_user['reasons'])}")
      break
  else:
    await client.say(f"{user.name} has never been reported")  

@warn.error
async def kick_error(error, ctx):
  if isinstance(error, MissingPermissions):
      text = "Sorry {}, you do not have permissions to do that!".format(ctx.message.author)
      await client.send_message(ctx.message.channel, text)   

client.run("BOT_TOKEN")

Where you save all of the user's reports in a file called reports.json and instead of manage_roles=True, ban_members=True inside @has_permissions you can put anything from the documentation