Discord.py: How To Extract A Boolean Value For If Someone Is Muted Or Not
Solution 1:
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.
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":[]}
Then create the command, pass in the required arguments and load the JSON file.
@bot.command()asyncdeftempmute(ctx, member : discord.Member, time : int):
withopen('mute.json') as f:
data = json.load(f)
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.idin all_users:
else:
data["users"].append({"id":member.id, "muted":True, "banned":False, "mute_time":time, "ban_time":0, "last_message": ""})
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 IDawait member.add_roles(mute_role_id)
And now that we have made the changes to the JSON file and given the role to the user we must save the changes.
withopen('mute.json', 'w') as f:
json.dump(data, f, indent=2)
So the final complete command looks like this:
@bot.command()asyncdeftempmute(ctx, member : discord.Member, time : int):
withopen('mute.json') as f:
data = json.load(f)
all_users = []
for user in data['users']:
all_users.append(user['id'])
if member.idin 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": ""})
withopen('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!
Post a Comment for "Discord.py: How To Extract A Boolean Value For If Someone Is Muted Or Not"