forked from Rapptz/RoboDanny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.py
333 lines (277 loc) · 10.6 KB
/
launcher.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
import sys
import click
import logging
import asyncio
import asyncpg
import discord
import importlib
import contextlib
from bot import RoboDanny, initial_extensions
from cogs.utils.db import Table
from pathlib import Path
from logging.handlers import RotatingFileHandler
import config
import traceback
try:
import uvloop
except ImportError:
pass
else:
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
class RemoveNoise(logging.Filter):
def __init__(self):
super().__init__(name='discord.state')
def filter(self, record):
if record.levelname == 'WARNING' and 'referencing an unknown' in record.msg:
return False
return True
@contextlib.contextmanager
def setup_logging():
try:
# __enter__
max_bytes = 32 * 1024 * 1024 # 32 MiB
logging.getLogger('discord').setLevel(logging.INFO)
logging.getLogger('discord.http').setLevel(logging.WARNING)
logging.getLogger('discord.state').addFilter(RemoveNoise())
log = logging.getLogger()
log.setLevel(logging.INFO)
handler = RotatingFileHandler(filename='rdanny.log', encoding='utf-8', mode='w', maxBytes=max_bytes, backupCount=5)
dt_fmt = '%Y-%m-%d %H:%M:%S'
fmt = logging.Formatter('[{asctime}] [{levelname:<7}] {name}: {message}', dt_fmt, style='{')
handler.setFormatter(fmt)
log.addHandler(handler)
yield
finally:
# __exit__
handlers = log.handlers[:]
for hdlr in handlers:
hdlr.close()
log.removeHandler(hdlr)
def run_bot():
loop = asyncio.get_event_loop()
log = logging.getLogger()
kwargs = {
'command_timeout': 60,
'max_size': 20,
'min_size': 20,
}
try:
pool = loop.run_until_complete(Table.create_pool(config.postgresql, **kwargs))
except Exception as e:
click.echo('Could not set up PostgreSQL. Exiting.', file=sys.stderr)
log.exception('Could not set up PostgreSQL. Exiting.')
return
bot = RoboDanny()
bot.pool = pool
bot.run()
@click.group(invoke_without_command=True, options_metavar='[options]')
@click.pass_context
def main(ctx):
"""Launches the bot."""
if ctx.invoked_subcommand is None:
loop = asyncio.get_event_loop()
with setup_logging():
run_bot()
@main.group(short_help='database stuff', options_metavar='[options]')
def db():
pass
@db.command(short_help='initialises the databases for the bot', options_metavar='[options]')
@click.argument('cogs', nargs=-1, metavar='[cogs]')
@click.option('-q', '--quiet', help='less verbose output', is_flag=True)
def init(cogs, quiet):
"""This manages the migrations and database creation system for you."""
run = asyncio.get_event_loop().run_until_complete
try:
run(Table.create_pool(config.postgresql))
except Exception:
click.echo(f'Could not create PostgreSQL connection pool.\n{traceback.format_exc()}', err=True)
return
if not cogs:
cogs = initial_extensions
else:
cogs = [f'cogs.{e}' if not e.startswith('cogs.') else e for e in cogs]
for ext in cogs:
try:
importlib.import_module(ext)
except Exception:
click.echo(f'Could not load {ext}.\n{traceback.format_exc()}', err=True)
return
for table in Table.all_tables():
try:
created = run(table.create(verbose=not quiet, run_migrations=False))
except Exception:
click.echo(f'Could not create {table.__tablename__}.\n{traceback.format_exc()}', err=True)
else:
if created:
click.echo(f'[{table.__module__}] Created {table.__tablename__}.')
else:
click.echo(f'[{table.__module__}] No work needed for {table.__tablename__}.')
@db.command(short_help='migrates the databases')
@click.argument('cog', nargs=1, metavar='[cog]')
@click.option('-q', '--quiet', help='less verbose output', is_flag=True)
@click.pass_context
def migrate(ctx, cog, quiet):
"""Update the migration file with the newest schema."""
if not cog.startswith('cogs.'):
cog = f'cogs.{cog}'
try:
importlib.import_module(cog)
except Exception:
click.echo(f'Could not load {ext}.\n{traceback.format_exc()}', err=True)
return
def work(table, *, invoked=False):
try:
actually_migrated = table.write_migration()
except RuntimeError as e:
click.echo(f'Could not migrate {table.__tablename__}: {e}', err=True)
if not invoked:
click.confirm('do you want to create the table?', abort=True)
ctx.invoke(init, cogs=[cog], quiet=quiet)
work(table, invoked=True)
sys.exit(-1)
else:
if actually_migrated:
click.echo(f'Successfully updated migrations for {table.__tablename__}.')
else:
click.echo(f'Found no changes for {table.__tablename__}.')
for table in Table.all_tables():
work(table)
click.echo(f'Done migrating {cog}.')
async def apply_migration(cog, quiet, index, *, downgrade=False):
try:
pool = await Table.create_pool(config.postgresql)
except Exception:
click.echo(f'Could not create PostgreSQL connection pool.\n{traceback.format_exc()}', err=True)
return
if not cog.startswith('cogs.'):
cog = f'cogs.{cog}'
try:
importlib.import_module(cog)
except Exception:
click.echo(f'Could not load {cog}.\n{traceback.format_exc()}', err=True)
return
async with pool.acquire() as con:
tr = con.transaction()
await tr.start()
for table in Table.all_tables():
try:
await table.migrate(index=index, downgrade=downgrade, verbose=not quiet, connection=con)
except RuntimeError as e:
click.echo(f'Could not migrate {table.__tablename__}: {e}', err=True)
await tr.rollback()
break
else:
await tr.commit()
@db.command(short_help='upgrades from a migration')
@click.argument('cog', nargs=1, metavar='[cog]')
@click.option('-q', '--quiet', help='less verbose output', is_flag=True)
@click.option('--index', help='the index to use', default=-1)
def upgrade(cog, quiet, index):
"""Runs an upgrade from a migration"""
run = asyncio.get_event_loop().run_until_complete
run(apply_migration(cog, quiet, index))
@db.command(short_help='downgrades from a migration')
@click.argument('cog', nargs=1, metavar='[cog]')
@click.option('-q', '--quiet', help='less verbose output', is_flag=True)
@click.option('--index', help='the index to use', default=-1)
def downgrade(cog, quiet, index):
"""Runs an downgrade from a migration"""
run = asyncio.get_event_loop().run_until_complete
run(apply_migration(cog, quiet, index, downgrade=True))
async def remove_databases(pool, cog, quiet):
async with pool.acquire() as con:
tr = con.transaction()
await tr.start()
for table in Table.all_tables():
try:
await table.drop(verbose=not quiet, connection=con)
except RuntimeError as e:
click.echo(f'Could not drop {table.__tablename__}: {e}', err=True)
await tr.rollback()
break
else:
click.echo(f'Dropped {table.__tablename__}.')
else:
await tr.commit()
click.echo(f'successfully removed {cog} tables.')
@db.command(short_help="removes a cog's table", options_metavar='[options]')
@click.argument('cog', metavar='<cog>')
@click.option('-q', '--quiet', help='less verbose output', is_flag=True)
def drop(cog, quiet):
"""This removes a database and all its migrations.
You must be pretty sure about this before you do it,
as once you do it there's no coming back.
Also note that the name must be the database name, not
the cog name.
"""
run = asyncio.get_event_loop().run_until_complete
click.confirm('do you really want to do this?', abort=True)
try:
pool = run(Table.create_pool(config.postgresql))
except Exception:
click.echo(f'Could not create PostgreSQL connection pool.\n{traceback.format_exc()}', err=True)
return
if not cog.startswith('cogs.'):
cog = f'cogs.{cog}'
try:
importlib.import_module(cog)
except Exception:
click.echo(f'Could not load {cog}.\n{traceback.format_exc()}', err=True)
return
run(remove_databases(pool, cog, quiet))
@main.command(short_help='migrates from JSON files')
@click.argument('cogs', nargs=-1)
@click.pass_context
def convertjson(ctx, cogs):
"""This migrates our older JSON files to PostgreSQL
Note, this deletes all previous entries in the table
so you can consider this to be a destructive decision.
Do not pass in cog names with "cogs." as a prefix.
This also connects us to Discord itself so we can
use the cache for our migrations.
The point of this is just to do some migration of the
data from v3 -> v4 once and call it a day.
"""
import data_migrators
run = asyncio.get_event_loop().run_until_complete
if not cogs:
to_run = [(getattr(data_migrators, attr), attr.replace('migrate_', ''))
for attr in dir(data_migrators) if attr.startswith('migrate_')]
else:
to_run = []
for cog in cogs:
try:
elem = getattr(data_migrators, 'migrate_' + cog)
except AttributeError:
click.echo(f'invalid cog name given, {cog}.', err=True)
return
to_run.append((elem, cog))
async def make_pool():
return await asyncpg.create_pool(config.postgresql)
try:
pool = run(make_pool())
except Exception:
click.echo(f'Could not create PostgreSQL connection pool.\n{traceback.format_exc()}', err=True)
return
client = discord.AutoShardedClient()
@client.event
async def on_ready():
click.echo(f'successfully booted up bot {client.user} (ID: {client.user.id})')
await client.logout()
try:
run(client.login(config.token))
run(client.connect(reconnect=False))
except:
pass
extensions = ['cogs.' + name for _, name in to_run]
ctx.invoke(init, cogs=extensions)
for migrator, _ in to_run:
try:
run(migrator(pool, client))
except Exception:
click.echo(f'[error] {migrator.__name__} has failed, terminating\n{traceback.format_exc()}', err=True)
return
else:
click.echo(f'[{migrator.__name__}] completed successfully')
if __name__ == '__main__':
main()