Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

python - In discord.py - How can I display the individual warnings for my warning system?

Now sure if the title conveys my intention here, but basically what I have is a warning command which stores all the values I want into my json file and I'm struggling to write a command that will display the warnings a user has.

Here's my code so far:

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

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

@bot.command()
@commands.has_permissions(administrator=True)
async def warn(ctx, user:discord.Member, *, args):

    reason = ''.join(args) #to get the full reason
    with open('warns.json', 'r') as f:
        users = json.load(f)

    await update_data(users, user)
    await add_warns(users, user, 1)
    random_id = random.randint(10000, 99999)
    warndate = time.strftime("%A, %B %d %Y @ %H:%M:%S %p")
    users[f'{user.id}']['mod'].append(ctx.author.id)
    users[f'{user.id}']['reason'].append(reason)
    users[f'{user.id}']['time'].append(warndate)
    users[f'{user.id}']['warn_id'].append(random_id)


    await ctx.send(f"{user.mention} has been warned!")

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

And here's what my json file looks like:

{
    "7656440092XXXXXXXX": {
        "mod": [
            8473926669XXXXXXXX,
            8473926669XXXXXXXX,
            8473926669XXXXXXXX
        ],
        "reason": [
            "reason 1",
            "reason 2",
            "reason 3"
        ],
        "time": [
            "Wednesday, January 27 2021 @ 20:32:55 PM",
            "Wednesday, January 27 2021 @ 20:33:03 PM",
            "Wednesday, January 27 2021 @ 20:36:58 PM"
        ],
        "warn_id": [
            49926,
            84348,
            55828
        ],
        "warns": 3
    }
}

I started to write a command that displays the warnings seperately, but all I managed was to dump out all of the reasons, times, etc. at once.

@bot.command()
@commands.has_permissions(administrator=True)
async def warndetails(ctx, user:discord.Member):
    try:
        with open('warns.json', 'r') as f:
            users = json.load(f)

        warns = users[f'{user.id}']['warns']
        moderator = users[f'{user.id}']['mod']
        warn_reason = users[f'{user.id}']['reason']
        warn_time = users[f'{user.id}']['time']
        warn_id = users[f'{user.id}']['warn_id']


        await ctx.send(f"Warning count: {warns}
Given by:{moderator}
For:{warn_reason}
At:{warn_time}
IDs:{warn_id}")

    



    except:
        await ctx.send(f"{user.mention} doesn't have any warnings (yet).")

I tried using a for loop to loop through the reasons and then display them to the user, but i don't know how to read the individual values for the warnings, as I'm quite new to json in general and I couldn't find anything through googleing. So ideally if you use that warndetails command, it would throw out 1 embed with the reason and everything for each warning given. The next goal I'm working towards is to delete the individual warnings by the ID assigned to them, if you have any pointers for that I would also be grateful. Also I'm willing to completely start over, so don't feel inclined to fix my code if it's better to start over in your opinion.

Thanks in advance, sorry for the very long code.

question from:https://stackoverflow.com/questions/65944019/in-discord-py-how-can-i-display-the-individual-warnings-for-my-warning-system

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You stored the warnings in a really bad way, it's not difficult to do that, but it's chunky and it's not the best way

# Harcoding so it's easier to visualize
warn_ids = [49926, 84348, 55828]
mods = [81730981793807198, 81730981793807198, 81730981793807198]
reasons = ["reason1", "reason2", "reason3"]
warn_times = ["Wednesday, January 27 2021 @ 20:32:55 PM", "Wednesday, January 27 2021 @ 20:32:55 PM", "Wednesday, January 27 2021 @ 20:32:55 PM"]
warn_ids = [12345, 54321, 123654]
warns = 3

print(f"Total warnings: {warns}")

for warn_id, mod, reason, warn_time, warn_id in zip(warn_ids, mods, reasons, warn_times, warn_ids):
    print(f"ID: {warn_id}, mod: {mod}, reason: {reason}, time: {warn_time}")

Output:

Total warnings: 3
ID: 49926, mod: 81730981793807198, reason: reason1, time: Wednesday, January 27 2021 @ 20:32:55 PM
ID: 54321, mod: 81730981793807198, reason: reason2, time: Wednesday, January 27 2021 @ 20:32:55 PM
ID: 123654, mod: 81730981793807198, reason: reason3, time: Wednesday, January 27 2021 @ 20:32:55 PM

I personally would use the following JSON format

{
  "1929291823719827398": [
    {"id": 123, "mod": 91823091823012339, "reason": "reason1", "timestamp": "12:34:12 01:01:2021"},
    {"id": 124, "mod": 19182309182309183, "reason": "reason2", "timestamp": "12:45:45 10:01:2021"},
    {"id": 125, "mod": 76187326198632893, "reason": "reason3", "timestamp": "20:12:54 13:01:2021"}
  ]
}

It's a lot easier now to read each indivitual warning, let me show an example

with open("path.json", "r") as f:
    data = json.load(f)

user_data = data[str(some_id)]

print(f"Total warnings: {len(user_data)}")

for warn in user_data:
    warn_id, mod, reason, time = warn.values()
    print(f"ID: {warn_id}, mod: {mod}, reason: {reason}, time: {time}")

Output:

Total warnings: 3
ID: 123, mod: 91823091823012339, reason: reason1, time: 12:34:12 01:01:2021
ID: 124, mod: 19182309182309183, reason: reason2, time: 12:45:45 10:01:2021
ID: 125, mod: 76187326198632893, reason: reason3, time: 20:12:54 13:01:2021

PS: Also sorry for a lot of code :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...