-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcustom.py
317 lines (282 loc) · 11.4 KB
/
tcustom.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
#!/usr/bin/python3
# TODO add embeds
# TODO add rich support such as dates
from copy import deepcopy
import discord
import pendulum
from discord import Embed
from sqlalchemy import and_, select, MetaData, Table, Column, Integer, String
import tquote
import tsched
from tutil import is_admin, guild_list, fetch_value, increment_usage, fetch_file, debug, is_admin_test
from constants import ENGINE, VERBOSE, extSet
custom_commands = dict()
def setup() -> None:
global meta, Commands
meta = MetaData()
Commands = Table(
'commands', meta,
Column('id', Integer, primary_key=True),
Column('guild_id', Integer),
Column('guild_name', String),
Column('name', String),
Column('embed', String),
Column('value', String),
)
meta.create_all(ENGINE)
guilds = guild_list()
for each in guilds:
import_custom_commands(each)
if VERBOSE >= 0:
print('[+] End Custom Commands Setup')
def import_custom_commands(guild: int) -> None:
"""
Internal function to grab any custom commands from the database
:param guild: <Int> Discord guild ID
:return: <None>
"""
name = ' '.join(fetch_value(guild, 1))
if VERBOSE >= 2:
print(name)
with ENGINE.connect() as conn:
select_st = select([Commands]).where(
Commands.c.guild_name == name
)
result = conn.execute(select_st).fetchall()
if result:
commands = dict()
for each in result:
each = list(each)
commands[each[3]] = each[5]
custom_commands[guild] = commands
@debug
def get_command(message: discord.Message) -> (None, (str, discord.Embed)):
"""
Check to see if the Message object is a custom guild-level
command, or if it is an override for a default command.
:param message: <discord.Message> Raw message object
:return: None or (<str>, <discord.Embed>)
"""
embed_image = None
links = ''
custom = '.'
args = message.content.split()
config = tsched.load_config(message.guild.id)
quotes = False
# TODO update logging
# Log message
if VERBOSE >= 2:
print("Config: ")
print(config)
try:
# TODO Replace command prefix in code at global level for quicker testing
if args[0] == '$custom' or args[0:2] == ['$help', 'custom']:
if VERBOSE >= 1:
print('[-] custom - help by {} in {} - {}'.format(
message.author.name, message.channel.name, message.guild.name))
return get_help(message)
except Exception as e:
if VERBOSE >= 2:
print("Exception: ")
print(e)
if config:
server_locale = config[2]
url = config[6]
timestamp = pendulum.now(tz=server_locale).to_datetime_string()
else:
url = ' '
timestamp = pendulum.now(tz='America/New_York').to_datetime_string()
server_locale = 'America/New_York'
try:
# all custom commands for the guild
guild_commands = custom_commands[message.guild.id]
if VERBOSE >= 2:
print("Guild Commands: ")
print(guild_commands)
# the value to be returned to the user, in raw formatting
custom = guild_commands[args[0]]
if VERBOSE >= 2:
print("Custom: ")
print(custom)
# Check for image links or general links
if VERBOSE >= 2:
print("Checking for links...")
for each in custom.split():
if each.find('http') != -1:
if each.split('.')[-1] in extSet['image']:
embed_image = each
else:
links = '{}\n{}'.format(links, each)
# Setting up for nested loops
prev = deepcopy(custom)
count = 0
# for nested commands, not the most efficient solution but it works
# if you can improve it please issue a code merge request
while True:
if VERBOSE >= 2:
print("Formatting command, loop {}...".format(count))
# Discord's character limit is 2000
# count is the recursion limit
# count is chosen arbitrarily, lower number = fewer cycles
if len(custom) + len(prev) > 1999 or count > 50:
break
else:
for key in guild_commands:
custom = custom.replace('<{}>'.format(key), guild_commands[key])
# TODO user mentions for <QUOTE>
custom = custom.replace('\\n', '\n')
custom = custom.replace('\\r', '<\n>')
custom = custom.replace('<URL>', url)
custom = custom.replace('<NOW>', '{} in {}'.format(timestamp, server_locale))
custom = custom.replace('<TIME>', '{} in {}'.format(timestamp, server_locale))
custom = custom.replace('<LOCALE>', server_locale)
custom = custom.replace('<LOCATION>', server_locale)
custom = custom.replace('<AUTHOR>', message.author.name)
custom = custom.replace('<GUILD>', message.guild.name)
while '<SCHED>' in custom:
custom = custom.replace('<SCHED>', tsched.get_schedule(message, True))
# TODO do we still need this?
#if '<QUOTE>' in custom:
# custom = custom.replace('<QUOTE>', tquote.get_quote(message, tquote.Quotes, raw=True))
# quotes = True
custom = custom.replace('<LORE>', tquote.get_quote(message, tquote.Lore, raw=True))
if custom == prev:
break
else:
count += 1
prev = deepcopy(custom)
except Exception as e:
if VERBOSE >= 2:
print('[!] Exception in tcustom {}'.format(e))
pass
finally:
# Cleanup from quote with user
if quotes:
custom = custom.replace('>', '')
if custom == '.':
return None
else:
increment_usage(message.guild, 'custom')
if VERBOSE >= 1:
print('[-] custom - {} by {} in {} - {}'.format(args[0], message.author.name, message.channel.name,
message.guild.name))
banner = Embed(title=args[0], description=custom)
if embed_image:
banner.set_image(url=embed_image)
if links == '':
links = None
return links, banner
def insert_command(message: discord.Message) -> discord.Embed:
"""
Add a new custom command to the database.
:param message: <Discord.message> Raw message object
:return: <str> Banner notifying if new command has been created or exisisting has been updated.
"""
# if await is_admin(message.author):
args = message.content.split()
links = str(message.attachments[0].url) if message.attachments else ''
# Check for web links in message's text
for each in args:
if each.find('http') != -1:
if each.split('.')[-1] in extSet['image']:
links = '{}\n{}'.format(links, each)
with ENGINE.connect() as conn:
select_st = select([Commands]).where(and_(
Commands.c.guild_id == message.guild.id,
Commands.c.name == args[0]
))
result = conn.execute(select_st).fetchone()
# Guild already has at least one custom command
if result:
ins = Commands.update().where(and_(
Commands.c.guild_name == message.guild.name,
Commands.c.name == args[0])).values(
value='{} {}'.format(' '.join(args[1:]), links),
links=links,
)
# TODO DRY the below
conn.execute(ins)
import_custom_commands(message.guild.id)
banner = Embed(title='Updated `{}`'.format(args[0], description=' '.join(args[1:])))
if VERBOSE >= 1:
print('[+] Updated {}'.format((args[0])))
return banner
# Guild has zero existing custom commands
else:
ins = Commands.insert().values(
id=message.id,
guild_id=message.guild.id,
guild_name=message.guild.name,
name=args[0],
value='{} {}'.format(' '.join(args[1:]), links),
embed=links,
)
conn.execute(ins)
import_custom_commands(message.guild.id)
banner = Embed(title='Added custom command `{}`'.format(args[0]), description=' '.join(args[1:]))
if VERBOSE >= 1:
print('[+] Updated {}'.format((args[0])))
return banner
@debug
async def delete_command(message: discord.Message) -> str:
"""
Permanently remove a custom command if it exists
:param message: <Discord.message object>
:return: <String> Notifying command has been removed
"""
try:
admin = await is_admin_test(message.author, message)
except:
return "Error checking roles status."
if not admin:
return "Only admins may perform this action."
else:
args = message.content.split()
if VERBOSE >= 2:
print("Args:")
print(args)
with ENGINE.connect() as conn:
select_st = select([Commands]).where(and_(
Commands.c.guild_id == message.guild.id,
Commands.c.name == args[0]
))
result = conn.execute(select_st).fetchone()
# TODO logging
if VERBOSE >= 2:
print("Result:")
print(result)
if result:
if VERBOSE >= 2:
print("Result:")
print(result)
del_st = Commands.delete().where(and_(
Commands.c.guild_id == message.guild.id,
Commands.c.name == args[0]
))
conn.execute(del_st)
import_custom_commands(message.guild.id)
return 'Deleted {}'.format(args[0])
else:
return 'Command `{}` does not exist.'.format(args[0])
def get_help(message: discord.Message) -> (None, list):
"""
Get the command help file from ./docs/help
:param message: <Discord.message object>
:return: <String> Containing help for the user's available options or list of locations
"""
increment_usage(message.guild, 'help')
custom = ''
try:
guild_commands = custom_commands[message.guild.id]
banner = Embed(title='Custom Command Help', description=fetch_file('help', 'custom'))
for name, value in guild_commands.items():
custom = '{}`{}`: {}\n'.format(custom, name, value)
available = Embed(title='Custom commands available in this server are:', description=custom)
# banner.add_field(name='Custom commands available in this server are:', value=custom)
return None, [banner, available]
except Exception as e:
if VERBOSE >= 0:
print('[!] Exception in get help custom {}'.format(e))
pass
# Register with SQLAlchemy on import
setup()