-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathACL.py
More file actions
197 lines (169 loc) · 8.74 KB
/
ACL.py
File metadata and controls
197 lines (169 loc) · 8.74 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
import discord
from discord.ext import commands
import os
import datetime
import shutil
import dotenv
class AdvancedChannelListener(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.ACLver = "4.1"
self.displayname='A.C.L.'
self.maindir = os.getcwd()
#Messages
self.ACL_notfounderr = "User history not found."
self.ACL_historynotfound = "Default message history does not exist."
self.ACL_nopermission = "You don't have permission to use ACL mode. This incident will be reported."
self.ACL_rm_all_success = "Cleared all saved message history."
self.ACL_rm_all_fail = "Can't clear all message history."
self.ACL_rm_user_fail = "Can't clear message history of the selected user. Does it even exist?"
try:
dotenv.load_dotenv()
################# Tokens/IDs/etc ###################
self.admin_usr = os.getenv('admin_usr')
self.extendedErrMess = os.getenv('extendedErrMess')
####################################################
except:
print("[ACL] CAN'T LOAD .env FILE!\nCreate .env file with the variables below and reload module:\nadmin_usr = ['your Discord ID']\nextendedErrMess = True/False\nThe .env file should be located in the bot's main directory.")
self.aclcheck()
#AdvancedChannelListener
def aclcheck(self):
print(f"AdvancedChannelListener v{self.ACLver} loaded.")
if os.path.exists(f'{self.maindir}/ACL') == True:
print("ACL check OK")
else:
print("ACL not found.\nCreating...")
try:
os.makedirs(f'{self.maindir}/ACL')
except:
print('Cannot create ACL directory.')
#MessageLogging
def userLog(self, usr, usrmsg, chnl, srv, usr_id, chnl_id, srv_id):
time = datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S')
if os.path.exists(f'{self.maindir}/ACL/{usr_id}/message.txt') == True:
usrmessage = open(f'{self.maindir}/ACL/{usr_id}/message.txt', 'a', encoding='utf-8')
usrmessage.write(f'[{time}] [{srv}({srv_id}) / {chnl}({chnl_id})] {usr}: {usrmsg}\n')
usrmessage.close()
else:
print("[ACL] New user detected. Creating new entry...")
os.makedirs(f'{self.maindir}/ACL/{usr_id}')
usrmessage = open(f'{self.maindir}/ACL/{usr_id}/message.txt', 'a', encoding='utf-8')
usrmessage.write(f'{self.displayname} user message log\nUsername: {usr}\nUserID: {usr_id}\n##############################\n\n')
usrmessage.write(f'[{time}] [{srv}({srv_id}) / {chnl}({chnl_id})] {usr}: {usrmsg}\n')
usrmessage.close()
def channelLog(self, usr, usrmsg, chnl, srv, usr_id, chnl_id, srv_id):
time = datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S')
print(f"[{time}] [Message//{srv}/{chnl}] {usr}: {usrmsg}")
if os.path.exists(f'{self.maindir}/ACL/default/message.txt') == True:
usrmessage = open(f'{self.maindir}/ACL/default/message.txt', 'a', encoding='utf-8')
usrmessage.write(f"[{time}] [Message//{srv}/{chnl}] {usr}: {usrmsg}\n")
usrmessage.close()
else:
print("[ACL] Default message history not detected. Creating new entry...")
os.makedirs(f'{self.maindir}/ACL/default')
usrmessage = open(f'{self.maindir}/ACL/default/message.txt', 'a', encoding='utf-8')
usrmessage.write(f"[{time}] [Message//{srv}/{chnl}] {usr}: {usrmsg}\n")
usrmessage.close()
#LogMessage
def logMessage(self, info):
time = datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S')
logs = open(f'{self.maindir}/Logs.txt', 'a', encoding='utf-8')
logs.write(f'[{time}] {info}\n')
logs.close()
#PrintMessage
def printMessage(self, info):
time = datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S')
print(f'[{time}] {info}')
async def on_message_hook(self,message):
#Username
username = str(message.author).split('#')[0]
#UserMessage
user_message = str(message.content)
#Channel
try:
channel = str(message.channel.name)
except AttributeError:
channel = str(message.channel)
#Server
try:
server = str(message.guild.name)
except AttributeError:
server = str(message.guild)
#UserID
userid = message.author.id
#ChannelID
channelid = message.channel.id
#ServerID
try:
serverid = message.guild.id
except AttributeError:
serverid = "DM"
self.channelLog(username, user_message, channel, server, userid, channelid, serverid)
self.userLog(username, user_message, channel, server, userid, channelid, serverid)
#1
@commands.command(name='ACL', help='Manage A.C.L. users messages saved history\ngetusr - shows User history by User ID\nget history - history of all saved messages\nclear [all/user_id] - removes all saved messages or only messages of selected user')
async def ACL(self, ctx, mode, *, value):
if str(ctx.message.author.id) in self.admin_usr:
if mode == 'getusr':
try:
await ctx.send(file=discord.File(f'{self.maindir}/ACL/{value}/message.txt'))
except:
await ctx.send(self.ACL_notfounderr)
elif mode == 'get' and value == 'history':
try:
await ctx.send(file=discord.File(f'{self.maindir}/ACL/default/message.txt'))
except:
await ctx.send(self.ACL_historynotfound)
elif mode == 'clear':
if value == 'all':
try:
shutil.rmtree(f'{self.maindir}/ACL/')
await ctx.send(self.ACL_rm_all_success)
message = f"Information[ACL]: {self.ACL_rm_all_success} Command executed by: {ctx.author.id}\n"
self.printMessage(message)
self.logMessage(message)
except Exception as exc:
if self.extendedErrMess:
await ctx.send(f"{self.ACL_rm_all_fail} \nException: {exc}")
else:
await ctx.send(self.ACL_rm_all_fail)
message = f"Information[ACL]: User {ctx.message.author.id} tried to clear all message history but failed. \nException: \n{exc}\n"
self.printMessage(message)
self.logMessage(message)
else:
try:
shutil.rmtree(f'{self.maindir}/ACL/{value}')
await ctx.send(f"Cleared message history of <@{value}>.")
message = f"Information[ACL]: User {ctx.message.author.id} cleared message history of {value}.\n"
self.printMessage(message)
self.logMessage(message)
except Exception as exc:
if self.extendedErrMess:
await ctx.send(f"{self.ACL_rm_user_fail} \nException: {exc}")
else:
await ctx.send(self.ACL_rm_user_fail)
message = f"Information[ACL]: User {ctx.message.author.id} tried to clear message history of {value} but failed. \nException: \n{exc}\n"
self.printMessage(message)
self.logMessage(message)
else:
await ctx.send("Wrong mode. See '.help ACL' for more info")
else:
await ctx.send(self.ACL_nopermission)
message = f"Information[ACL]: User {ctx.message.author.id} tried to use .ACL command without permission.\nSee {self.maindir}/ACL/{ctx.message.author.id} for more information.\n"
self.printMessage(message)
self.logMessage(message)
#2
@commands.command(name='ACLinfo', help='Show info about loaded module')
async def ACLinfo(self, ctx):
await ctx.send(f"""
***AdvancedChannelListener***
Version: {self.ACLver}
Main Directory: {self.maindir}
Source: [ACL on GitHub](https://github.com/kamile320/AdvancedChannelListener)
This module saves every message sent in Discord channels. This can break user privacy; you're using this module at your own risk!
Changelog v{self.ACLver}:
- Reworked to a cog module - now you need a bot that uses cogs to run it (like ServerBot).
- Added .ACLinfo command to show info about the module.
- Added missing definitions for message logging.""")
async def setup(bot):
await bot.add_cog(AdvancedChannelListener(bot))