Discord.py:如何为某人是否静音而提取布尔值 [英] Discord.py: How to extract a Boolean value for if someone is muted or not

查看:44
本文介绍了Discord.py:如何为某人是否静音而提取布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在为特定的不和谐服务器开发机器人.在服务器中,我观察到,如果我关闭正在运行的脚本,然后再次运行它,则先前插入的数据将被删除,这是我不想发生的事情.这会导致周围发生各种错误.

Recently, I have been working on my bot for a particular discord server. In the server I observed that if I closed the running script and then ran it again the previously inserted data was deleted and this is what I don't want to happen. It would lead to various bugs happening around.

我的机器人使某人静音的方法是分配一个角色名称:静音.该角色具有发布消息和加入VC的权限.因此,当主持人询问时,我的机器人会扮演这个角色.

The way my bot mutes someone is by assigning a role name: Muted. This role has the permissions of posting messages and joining VCs disabled. Thus, my bot gives this role when asked by a moderator.

我有一个 tempmute 命令,该命令可以使某人静音特定时间,但是如果机器人关闭,则计时器也会重置,并且在许多其他情况下也会发生同样的情况.就像有一个列表,我使用命令在其中添加成员.但是,一旦重新启动该漫游器,它也会被重置.

I have a tempmute command which would mute someone for a particular time but if the bot shuts down then the timer is reset as well and the same would happen with many other things. Like there is a list into which I add members using a command. But as soon as the bot is restarted it is reset as well.

我希望您能帮助我如何将数据存储在JSON文件中,在该文件中我将为每个成员插入数据.以下是我要保存的内容:

I want you to help me how to store data in a JSON file where I would insert the data for every single member. It following would be the things that I want to save:

  • 名称:
  • 静音:对/错(我不知道!)
  • 被禁止:对/错(我不知道!)
  • 静音时间:(我不确定!)
  • 禁令时间:(我不确定!)
  • 最后一条消息:

我添加的笔记是我不知道的东西.请让我知道我该怎么做.任何帮助将不胜感激.

The things which I have added notes are the ones I don't know. Please let me know how I can do what I want to do here. Any help would be appreciated.

我的代码的链接为: https://paste.pythondiscord.com/pogapiyolu.py

谢谢!:)

推荐答案

要进行命令,我们需要一些参数.我们需要的信息是:要静音的用户和将被静音的时间.根据您的要求,我将完成一个命令,将上述信息存储在JSON文件中,并为用户提供静音"的命令.角色.我将开始倒数,并删除静音"标签.角色由您决定.过程非常相似.

To make your command we need to have some arguments. The info we need is: the user to be muted and the time the user will be muted for. As you requested I am going to go through the process of making a command to store the above info in a JSON file and give the user the "Muted" role. I am going to leave counting down and removing the "Muted" role up to you. The process is very similar.

因此,首先在与脚本相同的目录中创建一个JSON文件.我将其命名为"mute.json".该文件必须包含以下内容:

So firstly create a JSON file in the same directory as your script. I named mine "mute.json". The file must contain the following:

{
  "users": [
    
  ]
}

然后创建命令,传递必需的参数并加载JSON文件.

Then create the command, pass in the required arguments and load the JSON file.

@bot.command()
async def tempmute(ctx, member : discord.Member, time : int):
    with open('mute.json') as f:
        data = json.load(f)

现在,我们需要检查用户是否已经在我们的JSON文件中.为此,我们遍历文件中已有的所有用户,并将其ID附加到列表中.然后,我们检查用户是否在该列表中.如果他不在我们的文件中,则将带有他的信息的对象添加到用户"列表中.数组.

Now we need to check if the user is already in our JSON file. To do that we iterate through all the users already in the file and append their IDs to a list. Then we check if the user is in that list. If he is not in our file we add an object with his information to the "users" array.

all_users = []
for user in data['users']:
        all_users.append(user['id'])
    
    if member.id in all_users:
else:
        data["users"].append({"id":member.id, "muted":True, "banned":False, "mute_time":time, "ban_time":0, "last_message": ""})

注意:我正在使用用户的ID来引用他.这种方式比使用他的名字更好,因为与id不同,它可以更改.

Note: I am using the user's ID to reference him. This way is better than using his name because, unlike the id, it can be changed.

下一步,我们需要检查文件中的用户是否匹配我们指定的用户.

Next, we need to check if the user in the file matches our specified user.

 if member.id == user['id']:

现在,我们检查用户是否已被静音.如果不是,我们将其静音并定义静音时间.这是我们要为用户提供静音"功能的部分.角色.

Now we check if the user is already muted. If he isn't we mute him and define the time of the mute. This is the part where we are going to give the user the "Muted" role.

if user['muted'] == False:
                    user['muted'] = True
                    user['mute_time'] = time
                    mute_role_id = member.guild.get_role(9999999999999999) # Change this to the "Muted" role ID
                    await member.add_roles(mute_role_id)

现在我们已经对JSON文件进行了更改,并将角色赋予用户,我们必须保存更改.

And now that we have made the changes to the JSON file and given the role to the user we must save the changes.

with open('mute.json', 'w') as f:
    json.dump(data, f, indent=2)

因此,最终的完整命令如下所示:

So the final complete command looks like this:

@bot.command()
async def tempmute(ctx, member : discord.Member, time : int):
    with open('mute.json') as f:
        data = json.load(f)
            
    all_users = []
    for user in data['users']:
        all_users.append(user['id'])
    
    if member.id in all_users:
        for user in data['users']:
            if member.id == user['id']:
                if user['muted'] == False:
                    user['muted'] = True
                    user['mute_time'] = time
                    mute_role_id = member.guild.get_role(9999999999999999)
                    await member.add_roles(mute_role_id)
    else:
        mute_role_id = member.guild.get_role(9999999999999999)
        await member.add_roles(mute_role_id)
        data["users"].append({"id":member.id, "muted":True, "banned":False, "mute_time":time, "ban_time":0, "last_message": ""})

    with open('mute.json', 'w') as f:
        json.dump(data, f, indent=2)

您可以修改和调整值,以将其设置为禁止".命令.希望我能帮上忙!

You can modify and tweak values to make this a "ban" command as well. I hope I could help!

这篇关于Discord.py:如何为某人是否静音而提取布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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