-
Notifications
You must be signed in to change notification settings - Fork 1
/
tquote.py
347 lines (324 loc) · 13 KB
/
tquote.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
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
#!/usr/bin/python3
import discord
import pandas as pd
import pendulum
import sqlalchemy
from discord import Embed
from sqlalchemy import and_, func, select, MetaData, Table, Column, Integer, String
from constants import DEFAULT_DIR, ENGINE, VERBOSE, extSet
from tutil import fetch_file, is_admin, increment_usage, fetch_value, debug
def setup() -> None:
global meta, Quotes, Lore, Config
meta = MetaData()
Config = Table(
'config', meta,
Column('id', Integer, primary_key=True),
Column('guild_name', String),
Column('locale', String),
Column('schedule', String),
Column('quote_format', String),
Column('lore_format', String),
Column('url', String),
Column('qAdd_format', String),
)
Quotes = Table(
'famQuotes', meta,
Column('id', Integer, primary_key=True),
Column('name', String),
Column('text', String),
Column('date', String),
Column('guild', String),
Column('guild_name', String),
Column('embed', String),
# Column('context', String),
)
Lore = Table(
'famLore', meta,
Column('id', Integer, primary_key=True),
Column('name', String),
Column('text', String),
Column('date', String),
Column('guild', String),
Column('embed', String),
Column('guild_name', String),
)
meta.create_all(ENGINE)
if VERBOSE >= 0:
print('[+] End Quotes Setup')
@debug
async def helper(message: discord.Message):
"""
Main entry point from main.py, handles majority of argument parsing
:param message: <Discord.message object>
:return: <lambda function> Internal function dispatch
"""
text = message.content
for each in message.mentions:
text = text.replace('<@!{}>'.format(each.id), each.name)
args = text.split()
# Lore
if args[0] == '!lore':
increment_usage(message.guild, 'lore')
if len(args) > 1:
if args[1] == 'add' and await is_admin(message.author, message):
return insert_quote(message, Lore)
elif args[1] == 'delete' and await is_admin(message.author, message):
return delete_quote(message.guild.id, args[2])
elif args[1] == 'help' and await is_admin(message.author, message):
return get_help(message)
else:
return get_quote(message, Lore, ' '.join(args[1:]), True)
else:
return get_quote(message, Lore, None, True)
# Quote with options
elif len(args) > 1:
increment_usage(message.guild, 'quote')
if args[1] == 'help':
return get_help(message)
elif args[1] == 'delete' and await is_admin(message.author, message):
return delete_quote(message.guild.id, args[2])
elif args[1] == 'log' and await is_admin(message.author, message):
return get_quote_log(message.guild.id)
else:
return get_quote(message, Quotes, ' '.join(args[1:]), True)
# Any random quote
else:
increment_usage(message.guild, 'quote')
return get_quote(message, Quotes, None, True)
@debug
def get_quote_log(guild: int) -> list:
"""
Return an xlsx of all quotes in the guild to the user.
:param guild:
:return:
"""
data_frame = [None, None]
for idx, Table in enumerate({Quotes, Lore}):
select_st = select([Table]).where(
Table.c.guild == guild)
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[4] = 'g_{}'.format(each[4])
data_frame[idx] = pd.DataFrame(entries, columns=keys)
with pd.ExcelWriter('{}/log/quoteLog_{}.xlsx'.format(DEFAULT_DIR, guild), engine='xlsxwriter') as writer:
data_frame[1].to_excel(writer, sheet_name='Sheet_1')
data_frame[0].to_excel(writer, sheet_name='Sheet_2')
return ['Log of all quotes and lore for this guild:', '{}/log/quoteLog_{}.xlsx'.format(DEFAULT_DIR, guild)]
@debug
def insert_quote(message: discord.Message, Table: (None, sqlalchemy.Table), adder=None) -> discord.Embed:
"""
Insert a quote to the database
:param message: <Discord.message object>
:param Table: <SQLAlchemy.Table object>
:param adder: <String> Username of the member who added the :speech_left:
:return: <String> Notifying of message being added
"""
if Table is None:
Table = Quotes
config = load_config(message.guild.id)
if config:
server_locale = config[2]
stm = config[7].replace('\\n', '\n')
else:
server_locale = 'Asia/Tokyo'
stm = '--{} on {}'
# Suppress any user or role mentions
text = message.content
for each in message.mentions:
text = text.replace('<@!{}>'.format(each.id), each.name)
for each in message.role_mentions:
text = text.replace('<@&{}>'.format(each.id), each.name)
text = text.replace('@everyone', '@ everyone')
text = text.replace('@here', '@ here')
# jump_url = message.jump_url
args = text.split()
embed = str(message.attachments[0].url) if message.attachments else None
if not embed:
embed = ''
for each in args:
if each.find('http') != -1:
if each.split('.')[-1] in extSet['image']:
embed = '{}\n{}'.format(embed, each)
date = pendulum.now(tz=server_locale).to_day_datetime_string()
with ENGINE.connect() as conn:
if Table.name == 'famQuotes':
ins = Table.insert().values(
id=message.id,
name=message.author.name,
text=text,
date=date,
guild=str(message.guild.id),
guild_name=message.guild.name,
embed=embed,
# context=jump_url,
)
conn.execute(ins)
if not fetch_value(message.guild.id, 10):
banner = Embed(title="{} Added Quote: {}".format(adder, message.id), description=text)
else:
banner = Embed(title="Added Quote: {}".format(message.id), description=text)
if embed:
banner.set_image(url=embed)
banner.set_footer(text=stm.format(message.author.name, date))
elif Table.name == 'famLore':
ins = Table.insert().values(
id=message.id,
name=args[2],
text=' '.join(args[3:]),
date=date,
guild=str(message.guild.id),
embed=embed,
guild_name=message.guild.name,
)
conn.execute(ins)
banner = Embed(title="Added Lore: {}".format(message.id), description=' '.join(args[3:]))
if embed:
banner.set_image(url=embed)
banner.set_footer(text=stm.format(args[2], date))
return banner
@debug
def get_quote(message: discord.Message, Table: sqlalchemy.Table, username=None, raw=False) -> discord.Embed:
"""
Retrieve a quote from the database.
:param message:
:param guild: <int> message.guild.id
:param Table: (Optional) <SQLAlchemy.Table> Quotes or Lore, defaults to Quotes
:param username: (Optional) <str> Case sensitive Discord username, without discriminator
:param raw:
"""
Table = Quotes
if username:
select_user = select([Table]).where(and_(
Table.c.name == username,
Table.c.guild == message.guild.id)).order_by(func.random())
select_id = select([Table]).where(and_(
Table.c.id == username,
Table.c.guild == message.guild.id))
else:
select_rand = select([Table]).where(
Table.c.guild == message.guild.id).order_by(func.random())
with ENGINE.connect() as conn:
if username:
result = conn.execute(select_id).fetchone()
if not result:
result = conn.execute(select_user).fetchone()
else:
result = conn.execute(select_rand).fetchone()
# Result fields translate as
# [0]: message id, [1]: author, [2]: quote, [3]: date, [6]: embed url, [7]: jump_url
if result:
config = load_config(message.guild.id)
stm = '---{} on {}'
context_url = 'blank'
title = 'Quote {}'.format(result[0])
if config:
if Table.name == 'famQuotes':
stm = config[4].replace('\\n', '\n')
title = "Quote {}".format(result[0])
context_url = 'test'
# context_url = '{}'.format(result[7])
elif Table.name == 'famLore':
stm = config[5].replace('\\n', '\n')
title = "Lore {}".format(result[0])
else:
if Table.name == 'famQuotes':
stm = '---{} on {}'
title = "Quote {}".format(result[0])
context_url = 'test'
# context_url = '{}'.format(result[7])
elif Table.name == 'famLore':
stm = '---Scribed by the Lore Master {}, on the blessed day of {}'
title = "Lore {}".format(result[0])
if raw:
# Check if there is an attached img or file to send as well
if len(result) > 6 and result[6]:
stm = stm + '\n' + result[6]
result[2].replace(result[6], '')
# Result fields translate as
# [1]: author, [2]: quote, [3]: date, [6]: embed url, [7]: jump_url
# TODO this should be a dict
if len(result) > 7 and result[7]:
text = '{} \n\n{}'.format(result[2], context_url)
else:
text = result[2]
return stm.format(title, text, result[1], result[3])
else:
stm = stm.format(result[1], result[3])
if len(result) > 6 and result[6]:
result[2].replace(result[6], '')
banner = Embed(title=title, description=result[2])
banner.add_field(name='Context ', value=context_url, inline=False)
if len(result) > 6 and result[6]:
banner.set_image(url=result[6])
banner.set_footer(text=stm)
return banner
def delete_quote(guild: int, msg_id: int) -> (str, None):
"""
Remove a quote from the database
:param guild: <Int> Discord guild ID
:param msg_id: <Int> Discord message ID
:return: <String> Notify if quote has been removed
"""
with ENGINE.connect() as conn:
for Table in {Quotes, Lore}:
select_st = select([Table]).where(and_(
Table.c.id == msg_id,
Table.c.guild == guild
))
try:
result = conn.execute(select_st).fetchone()
if result:
quote = '{}\n ---{} on {}'.format(result[2], result[1], result[3])
ins = Table.delete().where(and_(
Table.c.id == msg_id,
Table.c.guild == guild
))
conn.execute(ins)
return "Deleted quote: {}".format(quote)
except Exception as e:
if VERBOSE >= 1:
print('[!] Exception in tquote: {}'.format(e))
return None
@debug
def check_if_exists(guild, msg_id) -> bool:
"""
Internal function to ensure that we do not
add the same message to the database multiple times.
:param guild: <Int> Discord guild ID
:param msg_id: <Int> Discord message ID
:return: <Bool>
"""
with ENGINE.connect() as conn:
select_st = select([Quotes]).where(and_(
Quotes.c.id == msg_id,
Quotes.c.guild == guild))
result = conn.execute(select_st).fetchall()
if result:
return True
return False
def load_config(guild: int) -> (None, list):
"""
Retrieve any formatting options from database
:param guild: <Int> Discord guild ID
:return: <List> SQLAlchemy row entry from Config Table
"""
result = None
with ENGINE.connect() as conn:
select_st = select([Config]).where(Config.c.id == guild)
result = conn.execute(select_st).fetchone()
return result
async def get_help(message: discord.Message) -> discord.Embed:
"""
Get the help file in ./docs/help
:param message: <Discord.message.author object>
:return: <String> The local help file
"""
text = fetch_file('help', 'quotes')
if not await is_admin(message.author, message):
text = text.split('For Admins:')[0]
return Embed(title='General Help', description=text)
setup()