This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
/
roles.py
236 lines (224 loc) · 8.64 KB
/
roles.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
"""Handling the self-assignable roles and such."""
import discord
from discord.ext import commands
from .utils import checks, embeds
class Roles(commands.Cog):
"""Handle self-assignable roles."""
def __init__(self, bot):
"""Init class."""
super().__init__()
self.bot = bot
@commands.command(aliases=['prunerole'])
@commands.guild_only()
@checks.has_permissions(manage_roles=True)
async def cleanrole(self, ctx, *, role_name):
"""(Testing) Removes all members from a certain role."""
found_role = None
for role in ctx.guild.roles:
if role.name.lower() == role_name.lower():
found_role = role
if not found_role:
await ctx.send(embed=discord.Embed(
title='Couldn\'t find role',
description=f'Couldn\'t find the given role to remove',
color=0x419400
))
return
count = 0
for user in found_role.members:
try:
local_roles = user.roles.copy()
local_roles.remove(found_role)
await user.edit(roles=local_roles)
count += 1
except Exception as e:
self.bot.logger.warning(f'Issue cleaning role: {e}')
await ctx.send(embed=discord.Embed(
title='Role cleaned:',
description=f'Successfully removed {count} '
f'users from {found_role.name}',
color=0x419400
))
@commands.command()
@commands.guild_only()
async def iam(self, ctx, *, role_name):
"""Add self-assignable role to user."""
found_role = None
users_roles = ctx.message.author.roles.copy()
for role in ctx.guild.roles:
if role.name.lower() == role_name.lower():
found_role = role
if found_role:
for role in users_roles:
if role == found_role:
local_embed = embeds.RoleDuplicateUserEmbed(
ctx.message.author, found_role.name
)
await ctx.send(embed=local_embed, delete_after=5)
return
assignable = await self.bot.pg_utils.is_role_assignable(
ctx.guild.id, found_role.id)
if assignable:
users_roles.append(found_role)
try:
await ctx.author.edit(roles=users_roles)
local_embed = embeds.SelfRoleAddedEmbed(
ctx.message.author, found_role.name
)
await ctx.send(embed=local_embed, delete_after=3)
await ctx.message.delete()
return
except discord.Forbidden:
local_embed = embeds.ForbiddenEmbed(
'self-assign role'
)
else:
local_embed = embeds.SelfRoleNotAssignableEmbed(
found_role.name
)
else:
pass
await ctx.send(embed=local_embed, delete_after=5)
@commands.command()
@commands.guild_only()
async def iamnot(self, ctx, *, role_name):
"""Remove self-assignable role from user."""
found_role = None
users_roles = ctx.message.author.roles.copy()
for role in ctx.guild.roles:
if role.name.lower() == role_name.lower():
found_role1 = role
if not found_role1:
return
for role in users_roles:
if role.name.lower() == role_name.lower():
found_role = role
if not found_role:
local_embed = embeds.RoleNotRemovedEmbed(
ctx.message.author, role_name
)
await ctx.send(embed=local_embed)
return
assignable_roles = await \
self.bot.pg_utils.get_assignable_roles(
ctx.guild.id)
if found_role.id in assignable_roles:
users_roles.remove(found_role)
await ctx.message.author.edit(roles=users_roles)
local_embed = embeds.SelfRoleRemovedEmbed(
ctx.message.author, found_role.name
)
await ctx.send(embed=local_embed, delete_after=5)
await ctx.message.delete()
else:
local_embed = embeds.SelfRoleNotAssignableEmbed(
role_name
)
await ctx.send(embed=local_embed)
@commands.group(aliases=['ar'])
@commands.guild_only()
@checks.admin_or_permissions(manage_roles=True)
async def assignableroles(self, ctx):
"""Manage server's self-assignable roles."""
if ctx.invoked_subcommand is None:
message = ' \n'
assignable_roles = []
assignable_role_ids = await \
self.bot.pg_utils.get_assignable_roles(ctx.guild.id)
for role in ctx.guild.roles:
if role.id in assignable_role_ids:
assignable_roles.append(role)
for role in assignable_roles:
message += f'{role.name}\n'
local_embed = discord.Embed(
title='Current self-assignable roles:',
description=message,
color=0x419400
)
await ctx.send(embed=local_embed)
@assignableroles.command()
async def add(self, ctx, *, role_name):
"""Add a role to the server's self-assignable roles list."""
found_role = None
for role in ctx.guild.roles:
if role.name.lower() == role_name.lower():
found_role = role
if found_role:
if not ctx.message.author.\
top_role >= found_role:
local_embed = discord.Embed(
title=f'You can\'t add a role that is a higher '
'level than your highest role',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
return
success = await self.bot.pg_utils.add_assignable_role(
ctx.guild.id, found_role.id, self.bot.logger)
if success:
local_embed = discord.Embed(
title=f'Added {found_role.name} to self-assignable roles',
description=' ',
color=0x419400
)
else:
local_embed = discord.Embed(
title=f'Internal error when adding {found_role.name} to '
'self-assignable roles, contact @dashwav#7785',
description=' ',
color=0x651111
)
else:
local_embed = discord.Embed(
title=f'Couldn\'t find role {role_name}',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
@assignableroles.command()
async def remove(self, ctx, *, role_name):
"""Remove a role from the server's self-assignable roles list."""
found_role = None
for role in ctx.guild.roles:
if role.name.lower() == role_name.lower():
found_role = role
if found_role:
try:
success = await \
self.bot.pg_utils.remove_assignable_role(
ctx.guild.id, found_role.id, self.bot.logger)
except ValueError:
local_embed = discord.Embed(
title=f'{found_role.name} is already'
' not on the self-assignable list',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
return
if success:
local_embed = discord.Embed(
title=f'Removed {found_role.name} '
'from self-assignable roles',
description=' ',
color=0x419400
)
else:
local_embed = discord.Embed(
title=f'Internal error occured,'
' please contact @dashwav#7785',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
else:
local_embed = discord.Embed(
title=f'Couldn\'t find role {role_name}',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
def setup(bot):
"""General cog loading."""
bot.add_cog(Roles(bot))