This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
62 lines (58 loc) · 2.26 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
import os
from config import data_dir
async def add_channel_roles(channel, member):
"""Different cases:
1. The channel has no roles in the json file:
- Create a new role
- Add the role to the json file
- Add to the member the role
- Return the role
2. The channel has a role in the json file:
- Add to the member the role
- Return the role
"""
role = None
if not os.path.exists(data_dir):
os.mkdir(data_dir)
if not os.path.exists(os.path.join(data_dir, f"{channel.guild.id}.json")):
with open(os.path.join(data_dir, f"{channel.guild.id}.json"), "w") as f:
json.dump({}, f)
with open(os.path.join(data_dir, f"{channel.guild.id}.json"), "r") as f:
data = json.load(f)
if str(channel.id) not in data:
"Case 1"
role = await channel.guild.create_role(name=channel.name)
data[str(channel.id)] = role.id
with open(os.path.join(data_dir, f"{channel.guild.id}.json"), "w") as f:
json.dump(data, f)
f.close()
else:
"Case 2"
role = channel.guild.get_role(data[str(channel.id)])
await member.add_roles(role)
async def remove_channel_roles(channel, member):
"""Different cases:
1. The channel has no roles in the json file:
- Do nothing
2. The channel has a role in the json file:
- Remove the role from the member
- If the role is not used by any other member, delete the role from the server and the json file
"""
if not os.path.exists(data_dir):
os.mkdir(data_dir)
if not os.path.exists(os.path.join(data_dir, f"{channel.guild.id}.json")):
with open(os.path.join(data_dir, f"{channel.guild.id}.json"), "w") as f:
json.dump({}, f)
with open(os.path.join(data_dir, f"{channel.guild.id}.json"), "r") as f:
data = json.load(f)
if str(channel.id) in data:
"Case 2"
role = channel.guild.get_role(data[str(channel.id)])
await member.remove_roles(role)
if role.members == []:
await role.delete()
del data[str(channel.id)]
with open(os.path.join(data_dir, f"{channel.guild.id}.json"), "w") as f:
json.dump(data, f)
f.close()