This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
forked from kkrypt0nn/Python-Discord-Bot-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoderation-slash.py
318 lines (304 loc) · 11.8 KB
/
moderation-slash.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
""""
Copyright © Krypton 2022 - https://github.com/kkrypt0nn (https://krypton.ninja)
Description:
This is a template to create your own discord bot in python.
Version: 4.1
"""
import disnake
from disnake import ApplicationCommandInteraction, Option, OptionType
from disnake.ext import commands
from helpers import checks
class Moderation(commands.Cog, name="moderation-slash"):
def __init__(self, bot):
self.bot = bot
@commands.slash_command(
name="kick",
description="Kick a user out of the server.",
options=[
Option(
name="user",
description="The user you want to kick.",
type=OptionType.user,
required=True
),
Option(
name="reason",
description="The reason you kicked the user.",
type=OptionType.string,
required=False
)
]
)
@commands.has_permissions(kick_members=True)
@checks.not_blacklisted()
async def kick(self, interaction: ApplicationCommandInteraction, user: disnake.User,
reason: str = "Not specified") -> None:
"""
Kick a user out of the server.
:param interaction: The application command interaction.
:param user: The user that should be kicked from the server.
:param reason: The reason for the kick. Default is "Not specified".
"""
member = await interaction.guild.get_or_fetch_member(user.id)
if member.guild_permissions.administrator:
embed = disnake.Embed(
title="Error!",
description="User has Admin permissions.",
color=0xE02B2B
)
await interaction.send(embed=embed)
else:
try:
embed = disnake.Embed(
title="User Kicked!",
description=f"**{member}** was kicked by **{interaction.author}**!",
color=0x9C84EF
)
embed.add_field(
name="Reason:",
value=reason
)
await interaction.send(embed=embed)
try:
await member.send(
f"You were kicked by **{interaction.author}**!\nReason: {reason}"
)
except disnake.Forbidden:
# Couldn't send a message in the private messages of the user
pass
await member.kick(reason=reason)
except:
embed = disnake.Embed(
title="Error!",
description="An error occurred while trying to kick the user. Make sure my role is above the role of the user you want to kick.",
color=0xE02B2B
)
await interaction.send(embed=embed)
@commands.slash_command(
name="nick",
description="Change the nickname of a user on a server.",
options=[
Option(
name="user",
description="The user you want to change the nickname.",
type=OptionType.user,
required=True
),
Option(
name="nickname",
description="The new nickname of the user.",
type=OptionType.string,
required=False
)
],
)
@commands.has_permissions(manage_nicknames=True)
@checks.not_blacklisted()
async def nick(self, interaction: ApplicationCommandInteraction, user: disnake.User, nickname: str = None) -> None:
"""
Change the nickname of a user on a server.
:param interaction: The application command interaction.
:param user: The user that should have its nickname changed.
:param nickname: The new nickname of the user. Default is None, which will reset the nickname.
"""
member = await interaction.guild.get_or_fetch_member(user.id)
try:
await member.edit(nick=nickname)
embed = disnake.Embed(
title="Changed Nickname!",
description=f"**{member}'s** new nickname is **{nickname}**!",
color=0x9C84EF
)
await interaction.send(embed=embed)
except:
embed = disnake.Embed(
title="Error!",
description="An error occurred while trying to change the nickname of the user. Make sure my role is above the role of the user you want to change the nickname.",
color=0xE02B2B
)
await interaction.send(embed=embed)
@commands.slash_command(
name="ban",
description="Bans a user from the server.",
options=[
Option(
name="user",
description="The user you want to ban.",
type=OptionType.user,
required=True
),
Option(
name="reason",
description="The reason you banned the user.",
type=OptionType.string,
required=False
)
],
)
@commands.has_permissions(ban_members=True)
@checks.not_blacklisted()
async def ban(self, interaction: ApplicationCommandInteraction, user: disnake.User,
reason: str = "Not specified") -> None:
"""
Bans a user from the server.
:param interaction: The application command interaction.
:param user: The user that should be banned from the server.
:param reason: The reason for the ban. Default is "Not specified".
"""
member = await interaction.guild.get_or_fetch_member(user.id)
try:
if member.guild_permissions.administrator:
embed = disnake.Embed(
title="Error!",
description="User has Admin permissions.",
color=0xE02B2B
)
await interaction.send(embed=embed)
else:
embed = disnake.Embed(
title="User Banned!",
description=f"**{member}** was banned by **{interaction.author}**!",
color=0x9C84EF
)
embed.add_field(
name="Reason:",
value=reason
)
await interaction.send(embed=embed)
try:
await member.send(f"You were banned by **{interaction.author}**!\nReason: {reason}")
except disnake.Forbidden:
# Couldn't send a message in the private messages of the user
pass
await member.ban(reason=reason)
except:
embed = disnake.Embed(
title="Error!",
description="An error occurred while trying to ban the user. Make sure my role is above the role of the user you want to ban.",
color=0xE02B2B
)
await interaction.send(embed=embed)
@commands.slash_command(
name="warn",
description="Warns a user in the server.",
options=[
Option(
name="user",
description="The user you want to warn.",
type=OptionType.user,
required=True
),
Option(
name="reason",
description="The reason you warned the user.",
type=OptionType.string,
required=False
)
],
)
@commands.has_permissions(manage_messages=True)
@checks.not_blacklisted()
async def warn(self, interaction: ApplicationCommandInteraction, user: disnake.User,
reason: str = "Not specified") -> None:
"""
Warns a user in his private messages.
:param interaction: The application command interaction.
:param user: The user that should be warned.
:param reason: The reason for the warn. Default is "Not specified".
"""
member = await interaction.guild.get_or_fetch_member(user.id)
embed = disnake.Embed(
title="User Warned!",
description=f"**{member}** was warned by **{interaction.author}**!",
color=0x9C84EF
)
embed.add_field(
name="Reason:",
value=reason
)
await interaction.send(embed=embed)
try:
await member.send(f"You were warned by **{interaction.author}**!\nReason: {reason}")
except disnake.Forbidden:
# Couldn't send a message in the private messages of the user
await interaction.send(f"{member.mention}, you were warned by **{interaction.author}**!\nReason: {reason}")
@commands.slash_command(
name="purge",
description="Delete a number of messages.",
options=[
Option(
name="amount",
description="The amount of messages you want to delete. (Must be between 1 and 100.)",
type=OptionType.integer,
required=True,
min_value=1,
max_value=100
)
],
)
@commands.has_guild_permissions(manage_messages=True)
@checks.not_blacklisted()
async def purge(self, interaction: ApplicationCommandInteraction, amount: int) -> None:
"""
Delete a number of messages.
:param interaction: The application command interaction.
:param amount: The number of messages that should be deleted.
"""
purged_messages = await interaction.channel.purge(limit=amount)
embed = disnake.Embed(
title="Chat Cleared!",
description=f"**{interaction.author}** cleared **{len(purged_messages)}** messages!",
color=0x9C84EF
)
await interaction.send(embed=embed)
@commands.slash_command(
name="hackban",
description="Bans a user without the user having to be in the server.",
options=[
Option(
name="user_id",
description="The ID of the user that should be banned.",
type=OptionType.string,
required=True
),
Option(
name="reason",
description="The reason you banned the user.",
type=OptionType.string,
required=False
)
]
)
@commands.has_permissions(ban_members=True)
@checks.not_blacklisted()
async def hackban(self, interaction: ApplicationCommandInteraction, user_id: str,
reason: str = "Not specified") -> None:
"""
Bans a user without the user having to be in the server.
:param interaction: The application command interaction.
:param user_id: The ID of the user that should be banned.
:param reason: The reason for the ban. Default is "Not specified".
"""
try:
await self.bot.http.ban(user_id, interaction.guild.id, reason=reason)
user = await self.bot.get_or_fetch_user(int(user_id))
embed = disnake.Embed(
title="User Banned!",
description=f"**{user} (ID: {user_id}) ** was banned by **{interaction.author}**!",
color=0x9C84EF
)
embed.add_field(
name="Reason:",
value=reason
)
await interaction.send(embed=embed)
except Exception as e:
embed = disnake.Embed(
title="Error!",
description="An error occurred while trying to ban the user. Make sure ID is an existing ID that belongs to a user.",
color=0xE02B2B
)
await interaction.send(embed=embed)
print(e)
def setup(bot):
bot.add_cog(Moderation(bot))