-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgrab.py
More file actions
352 lines (291 loc) · 11.2 KB
/
Copy pathgrab.py
File metadata and controls
352 lines (291 loc) · 11.2 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""
Module for defining the grabs extension
"""
from __future__ import annotations
import random
from typing import TYPE_CHECKING, Self
import discord
from discord.ext import commands
import configuration
import ui
from core import auxiliary, cogs, cryptography
if TYPE_CHECKING:
import bot
async def setup(bot: bot.TechSupportBot) -> None:
"""Loading the Grab plugin into the bot
Args:
bot (bot.TechSupportBot): The bot object to register the cogs to
"""
await bot.add_cog(Grabber(bot=bot))
async def invalid_channel(ctx: commands.Context) -> bool:
"""A method to check channels against the whitelist
If the channel is not in the whitelist, the command execution is halted
This is expected to be used in a @commands.check call
Args:
ctx (commands.Context): The context in which the command was run in
Raises:
CommandError: Raised if grabs aren't allowed in the given channel
Returns:
bool: If the grabs are allowed in the channel the command was run in
"""
allowed_channels = configuration.get_config_entry(
ctx.guild.id, "grab_allowed_channels"
)
# Check if list is empty. If it is, allow all channels
if not allowed_channels:
return True
# If this list is not empty, it is a strict whitelist
if str(ctx.channel.id) in allowed_channels:
return True
raise commands.CommandError("Grabs are disabled for this channel")
class Grabber(cogs.BaseCog):
"""Class for the actual commands
Attributes:
SEARCH_LIMIT (int): The max amount of messages to search when grabbing
"""
SEARCH_LIMIT: int = 20
@auxiliary.with_typing
@commands.guild_only()
@commands.check(invalid_channel)
@commands.command(
name="grab",
brief="Grabs a user's message",
description="Grabs a message by ID and saves it",
usage="[username-or-user-ID]",
)
async def grab_user(
self: Self, ctx: commands.Context, user_to_grab: discord.Member
) -> None:
"""This is the grab by user function. Accessible by .grab
This will only search for 20 messages
Args:
ctx (commands.Context): The context in which the command was run in
user_to_grab (discord.Member): The user to search for grabs from
"""
if user_to_grab.bot:
await auxiliary.send_deny_embed(
message="Ain't gonna catch me slipping!", channel=ctx.channel
)
return
if user_to_grab == ctx.author:
await auxiliary.send_deny_embed(
message="You can't do this to yourself", channel=ctx.channel
)
return
grab_message = None
async for message in ctx.channel.history(limit=self.SEARCH_LIMIT):
if message.author == user_to_grab:
grab_message = message.content
break
if not grab_message:
await auxiliary.send_deny_embed(
message=f"Could not find a recent message from user {user_to_grab}",
channel=ctx.channel,
)
return
grab_hash = cryptography.hash_text(grab_message)
grab = (
await self.bot.models.Grab.query.where(
self.bot.models.Grab.author_id == str(user_to_grab.id),
)
.where(self.bot.models.Grab.message_hash == grab_hash)
.gino.first()
)
if grab:
await auxiliary.send_deny_embed(
message="That grab already exists!", channel=ctx.channel
)
return
grab = self.bot.models.Grab(
author_id=str(user_to_grab.id),
channel=str(ctx.channel.id),
guild=str(ctx.guild.id),
message=cryptography.encrypt(grab_message),
message_hash=grab_hash,
nsfw=ctx.channel.is_nsfw(),
)
await grab.create()
await auxiliary.send_confirm_embed(
message=f"Successfully saved: '*{grab_message}*'", channel=ctx.channel
)
@commands.group(
brief="Executes a grabs command",
description="Executes a grabs command",
)
async def grabs(self: Self, ctx: commands.Context) -> None:
"""The bare .grabs command. This does nothing but generate the help message
Args:
ctx (commands.Context): The context in which the command was run in
"""
return
@auxiliary.with_typing
@commands.guild_only()
@commands.check(invalid_channel)
@grabs.command(
name="all",
brief="Returns grabs for a user",
description="Returns all grabbed messages for a user",
usage="[user]",
)
async def all_grabs(
self: Self, ctx: commands.Context, user_to_grab: discord.Member
) -> None:
"""Discord command to get a paginated list of all grabs from a given user
Args:
ctx (commands.Context): The context in which the command was run in
user_to_grab (discord.Member): The user to get all the grabs from
"""
is_nsfw = ctx.channel.is_nsfw()
if user_to_grab.bot:
await auxiliary.send_deny_embed(
message="Ain't gonna catch me slipping!", channel=ctx.channel
)
return
query = self.bot.models.Grab.query.where(
self.bot.models.Grab.author_id == str(user_to_grab.id)
).where(self.bot.models.Grab.guild == str(ctx.guild.id))
if not is_nsfw:
# pylint: disable=C0121
query = query.where(self.bot.models.Grab.nsfw == False)
grabs = await query.gino.all()
if not grabs:
await auxiliary.send_deny_embed(
message=f"No grabs found for {user_to_grab.name}", channel=ctx.channel
)
return
grabs.sort(reverse=True, key=lambda grab: grab.time)
embeds = []
field_counter = 1
for index, grab_ in enumerate(grabs):
description = "Let's take a stroll down memory lane..."
if not is_nsfw:
description = "Note: *NSFW grabs are hidden in this channel*"
embed = (
discord.Embed(
title=f"Grabs for {user_to_grab.name}",
description=description,
)
if field_counter == 1
else embed
)
embed.add_field(
name=f'"{cryptography.decrypt(grab_.message)}"',
value=grab_.time.date(),
inline=False,
)
if (
field_counter
== configuration.get_config_entry(ctx.guild.id, "grab_per_page")
or index == len(list(grabs)) - 1
):
embed.set_thumbnail(url=user_to_grab.display_avatar.url)
embed.color = discord.Color.orange()
embeds.append(embed)
field_counter = 1
else:
field_counter += 1
await ui.PaginateView().send(ctx.channel, ctx.author, embeds)
@auxiliary.with_typing
@commands.guild_only()
@commands.check(invalid_channel)
@grabs.command(
name="random",
brief="Returns a random grab",
description="Returns a random grabbed message for a user "
+ "(note: NSFW messages are filtered by channel settings)",
usage="[user]",
)
async def random_grab(
self: Self, ctx: commands.Context, user_to_grab: discord.Member
) -> None:
"""Discord command to get a random grab from the given user
Args:
ctx (commands.Context): The context in which the command was run in
user_to_grab (discord.Member): The user to get a random grab from
"""
if user_to_grab.bot:
await auxiliary.send_deny_embed(
message="Ain't gonna catch me slipping!", channel=ctx.channel
)
return
grabs = (
await self.bot.models.Grab.query.where(
self.bot.models.Grab.author_id == str(user_to_grab.id)
)
.where(self.bot.models.Grab.guild == str(ctx.guild.id))
.gino.all()
)
query = self.bot.models.Grab.query.where(
self.bot.models.Grab.author_id == str(user_to_grab.id)
).where(self.bot.models.Grab.guild == str(ctx.guild.id))
if not ctx.channel.is_nsfw():
# pylint: disable=C0121
query = query.where(self.bot.models.Grab.nsfw == False)
grabs = await query.gino.all()
if not grabs:
await auxiliary.send_deny_embed(
message=f"No grabs found for {user_to_grab}", channel=ctx.channel
)
return
random_index = random.randint(0, len(grabs) - 1)
grab = grabs[random_index]
embed = discord.Embed(
title=f'"{cryptography.decrypt(grab.message)}"',
description=f"{user_to_grab.name}, {grab.time.date()}",
)
embed.color = discord.Color.orange()
embed.set_thumbnail(url=user_to_grab.display_avatar.url)
await ctx.send(embed=embed)
@auxiliary.with_typing
@commands.guild_only()
@commands.check(invalid_channel)
@grabs.command(
name="delete",
brief="Deleted a specific grab",
description="Deleted a specific grab from a user by the message",
usage="[user] [message]",
)
async def delete_grab(
self: Self, ctx: commands.Context, target_user: discord.Member, *, message: str
) -> None:
"""Deletes a given grab by exact string
Args:
ctx (commands.Context): The context in which the command was run in
target_user (discord.Member): The user to delete a grab from
message (str): The exact string of the grab to delete
Raises:
CommandError: Raised if the grab cannot be found for the given user
"""
# Stop execution if the invoker isn't the target or an admin
if (
not ctx.message.author.id == target_user.id
and not ctx.message.author.guild_permissions.administrator
):
await auxiliary.send_deny_embed(
message="You don't have sufficient permissions to do this!",
channel=ctx.channel,
)
return
grab_hash = cryptography.hash_text(message)
# Gets the target grab by the message
grab = (
await self.bot.models.Grab.query.where(
self.bot.models.Grab.author_id == str(target_user.id)
)
.where(self.bot.models.Grab.guild == str(ctx.guild.id))
.where(self.bot.models.Grab.message_hash == grab_hash)
.gino.all()
)
if not grab:
await auxiliary.send_deny_embed(
message=f"Grab `{message}` not found for {target_user}",
channel=ctx.channel,
)
return
try:
await grab[0].delete()
except IndexError:
raise commands.CommandError("Couldn't delete the grab!") from IndexError
await auxiliary.send_confirm_embed(
message="Grab succesfully deleted!", channel=ctx.channel
)