-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
cog.py
273 lines (245 loc) · 11.8 KB
/
cog.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""
Cog implementing channels and roles management. Copying/creating channels with permissions.
"""
import io
import disnake
from disnake.ext import commands
import utils
from cogs.base import Base
from database.review import SubjectDetailsDB
from rubbergod import Rubbergod
from utils.checks import PermissionsCheck
from .messages_cz import MessagesCZ
class Roles(Base, commands.Cog):
def __init__(self, bot: Rubbergod):
super().__init__()
self.bot = bot
@PermissionsCheck.is_mod_plus()
@commands.slash_command(name="move_category", description=MessagesCZ.move_category_brief)
async def move_category(
self,
inter: disnake.ApplicationCommandInteraction,
move_category: disnake.CategoryChannel,
under_category: disnake.CategoryChannel,
):
await inter.response.defer()
await move_category.edit(position=under_category.position + 1)
await inter.edit_original_response(
MessagesCZ.move_category_done(move_category=move_category, under_category=under_category)
)
@PermissionsCheck.is_mod_plus()
@commands.slash_command(name="channel", guild_ids=[Base.config.guild_id])
async def channel(self, inter: disnake.ApplicationCommandInteraction):
pass
@channel.sub_command(name="copy", description=MessagesCZ.role_channel_copy_brief)
async def copy(
self,
inter: disnake.ApplicationCommandInteraction,
src: disnake.abc.GuildChannel,
dst: disnake.abc.GuildChannel,
):
"""
Copy permissions from src channel to dst.
Both channels are expected as tags or IDs
"""
await inter.send(MessagesCZ.channel_copy_start)
for key in src.overwrites:
await dst.set_permissions(key, overwrite=src.overwrites[key])
await inter.edit_original_response(MessagesCZ.channel_copy_done)
@channel.sub_command(name="clone", description=MessagesCZ.role_channel_clone_brief)
async def clone(
self, inter: disnake.ApplicationCommandInteraction, src: disnake.abc.GuildChannel, name: str
):
"""Clone channel with same permissions as src."""
await inter.send(MessagesCZ.channel_clone_start)
new = await src.clone(name=name)
await inter.edit_original_response(MessagesCZ.channel_clone_done(id=new.id))
@channel.sub_command(name="create", description=MessagesCZ.role_channel_create_brief)
async def create(
self,
inter: disnake.ApplicationCommandInteraction,
channel_name: str,
role: disnake.Role,
rate: int = commands.Param(ge=1, default=10, description=MessagesCZ.channel_rate_param),
category: disnake.CategoryChannel = None,
):
await inter.send(MessagesCZ.channel_create_start)
guild = inter.guild
overwrites = {guild.default_role: disnake.PermissionOverwrite(view_channel=False)}
channel = await guild.create_text_channel(channel_name, category=category, overwrites=overwrites)
for index, member in enumerate(role.members):
await channel.set_permissions(member, view_channel=True)
if index % rate == 0:
await inter.edit_original_response(
f"• uživatelů: {len(role.members)}\n"
+ utils.general.create_bar(index + 1, len(role.members))
)
await inter.edit_original_response(
MessagesCZ.channel_create_done(channel=channel.mention, role=role.name, perms=len(role.members))
)
@PermissionsCheck.is_mod_plus()
@channel.sub_command(
name="add_channels_description",
description=MessagesCZ.channel_add_description_brief,
guild_ids=[Base.config.guild_id],
)
async def add_channels_description(
self,
inter: disnake.ApplicationCommandInteraction,
rate: int = commands.Param(ge=1, default=10, description=MessagesCZ.channel_rate_param),
):
await inter.send(MessagesCZ.channel_add_topic_start)
message = await inter.original_message()
for index, channel in enumerate(inter.guild.channels):
if channel.type != disnake.ChannelType.text:
continue
if index % rate == 0:
progress_bar = utils.general.create_bar(index + 1, len(inter.guild.channels))
await message.edit(
MessagesCZ.channel_add_topic_progress(
index=index + 1,
total=len(inter.guild.channels),
progress_bar=progress_bar,
channel=channel.mention,
)
)
is_private = "-" in channel.name
name = channel.name.split("-")[0] if is_private else channel.name
subject_name = SubjectDetailsDB.get(name)
if not subject_name:
continue
private_name = f"{subject_name}, but " + "-".join(channel.name.split("-")[1:])
if channel.topic:
if channel.topic != subject_name and channel.topic != private_name:
await self.log_channel.send(
MessagesCZ.channel_different_topic(channel=channel, topic=subject_name)
)
else:
await channel.edit(topic=subject_name if not is_private else private_name)
await message.edit(MessagesCZ.channel_add_topic_done)
@channel.sub_command(name="get_overwrites", description=MessagesCZ.channel_get_overwrites_brief)
async def get_overwrites(
self,
inter: disnake.ApplicationCommandInteraction,
rate: int = commands.Param(ge=1, default=10, description=MessagesCZ.channel_rate_param),
):
"""Get channels and count of user overwrites with view_channel=True."""
await inter.send(MessagesCZ.channel_get_overwrites_start)
total_overwrites = 0
user_overwrites = 0
channels_output = {}
for index, channel in enumerate(inter.guild.channels):
channel_user_overwrites = {
member: permission
for member, permission in channel.overwrites.items()
if not isinstance(member, disnake.Role) and permission.view_channel
}
channel_overwrites = {
role_or_member: permission for role_or_member, permission in channel.overwrites.items()
}
channels_output[channel] = f"user:{len(channel_user_overwrites)}, all: {len(channel_overwrites)}"
if index % rate == 0:
await inter.edit_original_response(
f"• kanálů: {index+1}/{len(inter.guild.channels)}\n"
f"{utils.general.create_bar(index+1, len(inter.guild.channels))}"
)
user_overwrites += len(channel_user_overwrites)
total_overwrites += len(channel_overwrites)
channels_output = dict(sorted(channels_output.items(), key=lambda item: item[1], reverse=True))
with io.StringIO() as output:
output.write(f"User overwrites total:{user_overwrites}\n")
output.write(f"Total overwrites:{total_overwrites}\n\n")
for channel, overwrites in channels_output.items():
if isinstance(channel, disnake.CategoryChannel):
output.write(f"#{channel.name}(kategorie) - {overwrites}\n")
else:
output.write(f"#{channel.name} - {overwrites}\n")
output.seek(0)
await inter.channel.send(file=disnake.File(output, filename="overwrites.txt"))
await inter.edit_original_response(MessagesCZ.channel_get_overwrites_done)
@channel.sub_command(name="overwrites_to_role", description=MessagesCZ.channel_overwrites_to_role_brief)
async def overwrites_to_role(
self,
inter: disnake.ApplicationCommandInteraction,
channel: disnake.TextChannel,
rate: int = commands.Param(ge=1, default=10, description=MessagesCZ.channel_rate_param),
):
"""
Create a new role with the same name as channel name.
Give users with view_channel=True this role.
Remove overwrites.
"""
await inter.send(MessagesCZ.channel_overwrites_to_role_start)
new_role = await inter.guild.create_role(name=channel.name)
await channel.set_permissions(
target=new_role, overwrite=disnake.PermissionOverwrite(view_channel=True)
)
channel_user_overwrites = {
member: permission
for member, permission in channel.overwrites.items()
if not isinstance(member, disnake.Role) and permission.view_channel
}
for index, member in enumerate(channel_user_overwrites):
await channel.set_permissions(member, overwrite=None)
await member.add_roles(new_role)
if index % rate == 0:
await inter.edit_original_response(
f"• overwrites: {index+1}/{len(channel_user_overwrites)}\n"
f"{utils.general.create_bar(index+1, len(channel_user_overwrites))}"
)
await inter.edit_original_response(MessagesCZ.channel_overwrites_to_role_done)
@channel.sub_command(name="role_to_overwrites", description=MessagesCZ.channel_role_to_overwrites_brief)
async def role_to_overwrites(
self,
inter: disnake.ApplicationCommandInteraction,
role: disnake.Role,
channel: disnake.TextChannel,
rate: int = commands.Param(ge=1, default=10, description=MessagesCZ.channel_rate_param),
):
"""
Create user overwrites from role for specific channel
Give users view_channel=True who had this role.
Remove role.
"""
await inter.send(MessagesCZ.channel_role_to_overwrites_start)
members = role.members
await role.delete()
for index, member in enumerate(members):
await channel.set_permissions(member, view_channel=True)
if index % rate == 0:
await inter.edit_original_response(
f"• overwrites: {index+1}/{len(members)}\n"
f"{utils.general.create_bar(index+1, len(members))}"
)
await inter.edit_original_response(MessagesCZ.channel_role_to_overwrites_done)
@PermissionsCheck.is_mod_plus()
@commands.slash_command(name="remove_exclusive_roles", description=MessagesCZ.remove_exclusive_roles)
async def remove_exclusive_roles(
self,
inter: disnake.ApplicationCommandInteraction,
keep_role: disnake.Role,
remove_role: disnake.Role = commands.Param(description=MessagesCZ.role_to_remove_param),
rate: int = commands.Param(ge=1, default=10, description=MessagesCZ.channel_rate_param),
):
"""Find people who have both roles and remove their remove_role."""
await inter.send(
MessagesCZ.remove_exclusive_roles_start(role1=keep_role.name, role2=remove_role.name)
)
role1_members = keep_role.members
role2_members = remove_role.members
members = set(role1_members).intersection(role2_members)
if not members:
await inter.send(MessagesCZ.role_no_exlusives)
return
for index, member in enumerate(members):
await member.remove_roles(remove_role)
if index % rate == 0:
await inter.edit_original_response(
f"• members: {index+1}/{len(members)}\n"
f"{utils.general.create_bar(index+1, len(members))}"
)
await inter.edit_original_response(MessagesCZ.remove_exclusive_roles_done)
@commands.Cog.listener()
async def on_member_join(self, member: disnake.Member):
if member.guild.id == Base.config.guild_id: # We're on VUT FIT guild
await member.add_roles(member.guild.get_role(Base.config.newbie_role))