forked from lapraswastaken/laprOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCogMod.py
71 lines (58 loc) · 2.81 KB
/
CogMod.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
from typing import Optional
import discord
from discord.ext import commands
import sources.ids as IDS
import sources.text as T
class MiniEntry:
def __init__(self, userID: int, targetUserID: int, count: int):
self.userID = userID
self.targetUserID = targetUserID
self.count = count
class CogMod(commands.Cog, **T.MOD.cog):
def __init__(self):
self.oldDeleteEntries: dict[int, MiniEntry] = {}
@staticmethod
async def getNewEntries(guild: discord.Guild) -> dict[int, MiniEntry]:
timeout = 300
newDeleteEntries: dict[int, MiniEntry] = {}
entry: discord.AuditLogEntry
async for entry in guild.audit_logs():
timeout -= 1
if timeout <= 0:
break
if not entry.action == discord.AuditLogAction.message_delete: continue
newDeleteEntries[entry.id] = MiniEntry(entry.user.id, entry.target.id, entry.extra.count)
return newDeleteEntries
async def handleOnReady(self, guild: discord.Guild):
self.oldDeleteEntries = await CogMod.getNewEntries(guild)
async def handleMessageDelete(self, message: discord.Message):
if (
isinstance(message.channel, discord.DMChannel) or
not message.guild.id in IDS.logChannelIDs or
message.author.bot
):
return
newDeleteEntries = await CogMod.getNewEntries(message.guild)
retrievedDeleterID: Optional[int] = None
for newID in newDeleteEntries:
newEntry = newDeleteEntries[newID]
if not newEntry.targetUserID == message.author.id: continue
if newID in self.oldDeleteEntries:
oldEntry = self.oldDeleteEntries[newID]
if newEntry.count == oldEntry.count + 1:
retrievedDeleterID = newEntry.userID
break
else:
retrievedDeleterID = newEntry.userID
break
if retrievedDeleterID != None and retrievedDeleterID != message.author.id:
deleter: discord.Member = await message.guild.fetch_member(retrievedDeleterID)
#if message.guild.id == 546872429621018635 and not 550518609714348034 in [role.id for role in deleter.roles]: return
channel: discord.TextChannel = message.guild.get_channel(IDS.logChannelIDs[message.guild.id])
await channel.send(T.MOD.deletedMessageText(message.author.id, deleter.id, message.jump_url))
await channel.send(
message.content,
embed=message.embeds[0] if message.embeds else None,
files=[await attachment.to_file(use_cached=True) for attachment in message.attachments]
)
self.oldDeleteEntries = newDeleteEntries