-
Notifications
You must be signed in to change notification settings - Fork 1
/
tlog.py
239 lines (205 loc) · 8.8 KB
/
tlog.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
#!/usr/bin/python3
import aiohttp
import aiofiles
from pathlib import Path
import pendulum
import pandas as pd
from sqlalchemy import and_, MetaData, Table, Column, Integer, String, select
from tutil import fetch_file, is_admin, config_fetch_embed, debug
from constants import DEFAULT_DIR, ENGINE, VERBOSE
extSet = {}
# TODO redo the entirety of logging
def setup():
global meta, Corpus
meta = MetaData()
Corpus = Table(
'corpus', meta,
Column('id', Integer, primary_key=True),
Column('content', String),
Column('user_name', String),
Column('user_id', String),
Column('time', String),
Column('channel', String),
Column('embeds', String),
Column('attachments', String),
Column('mentions', String),
Column('channel_mentions', String),
Column('role_mentions', String),
Column('msg_id', String),
Column('reactions', String),
Column('guild', String),
Column('guild_name', String),
)
meta.create_all(ENGINE)
for file_ in {'audio', 'docs', 'images', 'video'}:
f = fetch_file('ext', file_).strip('\n')
extSet[file_] = f.split()
if VERBOSE >= 0:
print('[+] End Logger Setup')
async def log_message(message):
"""
Grab new config file if present and add each raw message to the database.
:param message: <Discord.message object>
:return: <None>
"""
timestamp = pendulum.now(tz='Asia/Tokyo').to_datetime_string()
corpus_insert(message, timestamp)
if message.attachments:
await fetch_embed(message, timestamp)
if await is_admin(message.author, message) and message.attachments[0].file_name == '{}.json'.format(message.guild.id):
await config_fetch_embed(message)
def corpus_insert(message, time_stamp):
"""
For bulk logging of all messages sent in all servers. Used for stats and admin logs.
:param message: <Discord.message object>
:param timeStamp> <String> The current server time
:return: <None>
"""
msg_mentions = [''.join(str(each)) for each in message.mentions]
msg_embeds = [''.join(str(each.to_dict())) for each in message.embeds]
msg_attachments = [''.join(str(each.file_name)) for each in message.attachments]
msg_channel_mentions = [''.join(str(each)) for each in message.channel_mentions]
msg_role_mentions = [''.join(str(each)) for each in message.role_mentions]
with ENGINE.connect() as conn:
ins = Corpus.insert().values(
content=str(message.content),
user_name=str(message.author),
user_id=str(message.author.id),
time=str(time_stamp),
channel=str(message.channel),
embeds=str(msg_embeds),
attachments=str(msg_attachments),
mentions=str(msg_mentions),
channel_mentions=str(msg_channel_mentions),
role_mentions=str(msg_role_mentions),
msg_id=str(message.id),
reactions="none",
guild=str(message.guild.id),
guild_name=str(message.guild.name),
)
conn.execute(ins)
async def fetcher(filetype, url, time, message):
"""
Internal function to download any message attachments from Discord servers
:param filetype: <String> The file extension
:param url: <String> The file location URL
:param time: <String> Current server time
:param message: <Discord.message object>
:return: <None>
"""
file_name = '{}_{}_{}'.format(message.author.name, time, url.split('/')[-1])
Path('{}/log/{}/{}/{}'.format(
DEFAULT_DIR, filetype, message.guild.name, message.channel.name)).mkdir(
parents=True, exist_ok=True)
file_path = '{}/log/{}/{}/{}/{}'.format(
DEFAULT_DIR, filetype, message.guild.name, message.channel.name, file_name)
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if resp.status == 200:
f = await aiofiles.open(file_path, mode='wb')
await f.write(await resp.read())
await f.close()
if VERBOSE >= 1:
print("[+] Saved: {}".format(file_path))
async def fetch_embed(message, time):
"""
Call fetcher() for each message.attachment
:param message: <Discord.message object>
:param time: <String> Current server time
:return: <None>
"""
url = str(message.attachments[0].url)
ext = str(url.split('.')[-1].lower())
[await fetcher(each, url, time, message) for each in extSet if ext in extSet[each]]
@debug
def get_log(message):
"""
Get an excel file log of a guild
:param message: <Discord.message object>
:return: <List> Describing output and file location
"""
args = message.content.split()
if VERBOSE >= 2:
print(args)
if len(args) > 2:
# Get log of a specific user
if args[1] in {"user"}:
try:
for each in message.mentions:
user = each.id
except:
pass
select_st = select([Corpus]).where(and_(
Corpus.c.guild == message.guild.id,
Corpus.c.user_id == user))
df = [None]
with ENGINE.connect() as conn:
result = conn.execute(select_st).fetchall()
keys = conn.execute(select_st).keys()
entries = [each.values() for each in result]
for each in entries:
each[0] = 'id_{}'.format(each[0])
each[3] = 'uid_{}'.format(each[3])
each[11] = 'mid_{}'.format(each[11])
each[13] = 'gid_{}'.format(each[13])
df[0] = pd.DataFrame(entries, columns=keys)
with pd.ExcelWriter('{}/log/Log_{}.xlsx'.format(DEFAULT_DIR, message.guild.id),
engine='xlsxwriter') as writer:
df[0].to_excel(writer, sheet_name='Messages')
return ['Log of {} for this guild:'.format(args[2]), '{}/log/Log_{}.xlsx'.format(DEFAULT_DIR, message.guild.id)]
# Get log of aa specific channell
if args[1] in {"channel"}:
for each in message.channel_mentions:
channel = each.name
if VERBOSE >= 2:
print(message.channel, channel)
select_st = select([Corpus]).where(and_(
Corpus.c.guild == message.guild.id,
Corpus.c.channel == channel))
df = [None]
with ENGINE.connect() as conn:
result = conn.execute(select_st).fetchall()
keys = conn.execute(select_st).keys()
entries = [each.values() for each in result]
for each in entries:
each[0] = 'id_{}'.format(each[0])
each[3] = 'uid_{}'.format(each[3])
each[11] = 'mid_{}'.format(each[11])
each[13] = 'gid_{}'.format(each[13])
df[0] = pd.DataFrame(entries, columns=keys)
with pd.ExcelWriter('{}/log/Log_{}.xlsx'.format(DEFAULT_DIR, message.guild.id),
engine='xlsxwriter') as writer:
df[0].to_excel(writer, sheet_name='Messages')
return ['Log of {} for this guild:'.format(args[2]), '{}/log/Log_{}.xlsx'.format(DEFAULT_DIR, message.guild.id)]
else:
# Get a log of the guild
if VERBOSE >= 2:
print('Getting full log...')
select_st = select([Corpus]).where(
Corpus.c.guild == message.guild.id)
df = [None]
with ENGINE.connect() as conn:
result = conn.execute(select_st).fetchall()
if result:
keys = conn.execute(select_st).keys()
entries = [each.values() for each in result]
for each in entries:
each[0] = 'id_{}'.format(each[0])
each[3] = 'uid_{}'.format(each[3])
each[11] = 'mid_{}'.format(each[11])
each[13] = 'gid_{}'.format(each[13])
df[0] = pd.DataFrame(entries, columns=keys)
with pd.ExcelWriter('{}/log/Log_{}.xlsx'.format(DEFAULT_DIR, message.guild.id), engine='xlsxwriter') as writer:
df[0].to_excel(writer, sheet_name='Messages')
return ['Log of all messages for this guild:', '{}/log/Log_{}.xlsx'.format(DEFAULT_DIR, message.guild.id)]
else:
return None
# Placeholder function
def get_help(message):
"""
Get the help file in ./docs/help for admin command to get a log file
:param message: <Discord.message object>
:return: <String> The local help file
"""
pass
setup()