0

So I'm trying to make a warn command, but it seems like nothing will work. I've been trying for months, and I've done whatever I could think of.

First I used JSON, but the code wouldn't execute. Second, I tried making some kind of alert system, but that isn't reliable. Third, I tried using dictionaries, but I couldn't figure out how to add variables.

Here is my current code

async def warn(ctx, member: discord.Member):
    def warn(warn, user):
        warn[user] = warn[user] + 1
        
        return warn
    warn = {}
    warn = warn(warn, member)
    await ctx.send(f"Warned")

Any help would be appreciated. Thanks in advance!

Westlenando
  • 88
  • 1
  • 8

2 Answers2

1

I got working code:

async def update_data(users, user):
    if not f'{user.id}' in users:
        users[f'{user.id}'] = {}
        users[f'{user.id}']['warns'] = 0

async def add_warns(users, user, warns):
    users[f'{user.id}']['warns'] += warns

@client.command()
async def warn(ctx, user: discord.Member):
    with open('warns.json', 'r') as f:
        users = json.load(f)

    await update_data(users, user)
    await add_warns(users, user, 1)

    with open('warns.json', 'w') as f:
        json.dump(users, f, sort_keys=True, ensure_ascii=False, indent=4)

    await ctx.send(f'Warned {user}')

that code should work (Import json) and it will sorts the .json file.

Edit 1: I Made a unwarn code and it works like a charm (It deletes the user id and the warns so it saves more space than just leaving it 0)

@client.command()
async def remove_warn(ctx, user: discord.Member, amount: int=None):
    with open('warns.json', 'r') as f:
        users = json.load(f)

    amount = amount or 1

    await update_data(users, user)
    await add_warns(users, user, -amount)

    if users[f'{user.id}']['warns'] <= 0:
        with open('warns.json', 'w') as f:
            del users[f'{user.id}']['warns']
            del users[f'{user.id}']
            f.write(json.dumps(users, indent=4))
        return

    else:

        with open('warns.json', 'w') as f:
            json.dump(users, f, sort_keys=True, ensure_ascii=False, indent=4)

        await ctx.send(f'Removed {amount} warn for {user}')
        return

edit 2: Made a working warn checker.

@client.command()
async def warns(ctx, user: discord.Member=None):
    user = user or ctx.author
    try:
        with open('warns.json', 'r') as f:
            users = json.load(f)

        warns = users[f'{user.id}']['warns']

        await ctx.send(f'{user} has {warns} warnings')
    except:
        await ctx.send(f"{user} doesn't have any warnings.")
Raphiel
  • 398
  • 2
  • 17
0

Not exactly sure what you're trying to achieve, but here is an example that warns a user when they say a 'badword' in any channel in the guild. After they say it more than once, they get a message saying this is your last warning. You could easily add a kick {member} after if they surpass 3 on the counter. Hope this helps.

@client.event
async def on_message(message):
    counter = 0
    member = client.get_user(message.author.id)
    channel = client.get_channel(message.channel.id)
    async for message in channel.history():
        if message.content == 'badword' and message.author == member:
            counter = counter + 1

    if counter == 1:
        await channel.send(f'{member} you have been warned.')
    elif counter == 2:
        await channel.send(f'{member} this is your last warning.')
        return

EDIT: I was waiting to post this till I could get a working example that included kicking the user from the server but couldn't get it to work based on what's been posted and the documentation. As I don't think that was your goal any way I'm going to post this in hopes that it helps someone.

MrMetacom
  • 142
  • 3
  • 16