警告命令不执行任何操作 [英] Warn command not executing whatsoever

查看:24
本文介绍了警告命令不执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试发出警告命令,但似乎没有任何效果.我已经尝试了几个月,我已经做了我能想到的任何事情.

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.

首先我使用 JSON,但代码不会执行.其次,我尝试制作某种警报系统,但这并不可靠.第三,我尝试使用字典,但我不知道如何添加变量.

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.

这是我当前的代码

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!

推荐答案

我得到了工作代码:

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}')

该代码应该可以工作(导入 json),它会对 .json 文件进行排序.

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

编辑 1:我做了一个未警告的代码,它的作用就像一个魅力(它删除了用户 ID 和警告,因此它比仅仅保留 0 节省更多空间)

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

编辑 2:制作了一个有效的警告检查器.

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.")

这篇关于警告命令不执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆