-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheleus.py
607 lines (544 loc) · 20.9 KB
/
heleus.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#!/usr/bin/env python3
import argparse
import asyncio
import bz2
import datetime
import logging
import os
import platform
import sys
import threading
import time
import uuid
import warnings
from concurrent.futures import TimeoutError, ThreadPoolExecutor
from hashlib import sha256
import coredis
import dill
import disnake as discord
from disnake import utils as dutils
from disnake.ext import commands
from utils.storage import RedisCollection
class NoResponse:
def __repr__(self):
return '<NoResponse>'
def __eq__(self, other):
if isinstance(other, NoResponse):
return True
else:
return False
# We'll be disabling the help command later, so best to remove the ending note
# mentioning it for when `send_cmd_help` is called
class CustomHelp(commands.DefaultHelpCommand):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def get_ending_note(self):
return None
def create_bot(auto_shard: bool):
cls = commands.AutoShardedBot if auto_shard else commands.Bot
class Heleus(cls):
def __init__(self, *args, **kwargs):
self.redis = kwargs.pop('redis', None)
self.name = kwargs.pop('name', 'Heleus')
if self.redis is None:
raise AssertionError('No redis instance specified')
self.test = kwargs.pop('test', False)
self.args = kwargs.pop('cargs', None)
self.boot_time = (
time.time()
) # for uptime tracking, we'll use this later
# used for keeping track of *this* instance over reboots
self.instance_id = sha256(
f'{platform.node()}_{os.getcwd()}_{self.args.shard_id}_{self.args.shard_count}'.encode()
).hexdigest()
self.logger = logging.getLogger('heleus')
self.logger.info('Heleus is booting, please wait...')
self.settings = RedisCollection(self.redis, 'settings')
self.invite_url = None # this too
self.team = None # and this
self.send_cmd_help = send_cmd_help
self.send_command_help = send_cmd_help
self.pm_help = kwargs.pop('pm_help', None)
db = str(self.redis.connection_pool.connection_kwargs['db'])
self.pubsub_id = f'heleus.{db}.pubsub.code'
self._pubsub_futures = {} # futures temporarily stored here
self._pubsub_broadcast_cache = {}
self._pubsub_pool = ThreadPoolExecutor(max_workers=1)
self.t1 = threading.Thread(
name='pubsub cache',
target=self._pubsub_cache_loop,
daemon=True,
)
load_cogs = kwargs.pop('load_cogs', None)
if load_cogs is not None:
self.autoload = load_cogs.split(',')
else:
self.autoload = None
self.loader = kwargs.pop('loader', 'cogs.core')
super().__init__(*args, **kwargs)
self.ready = False # we expect the loader to set this once ready
def init(self):
"""Initializes the bot."""
# pubsub
self.t1.start()
self.loop.create_task(self._pubsub_loop())
# load the core cog
default = 'cogs.core'
self.load_extension(self.loader)
if loader != default:
self.logger.warning(
f'Using third-party loader and core cog, {loader}. No support will be provided if anything goes wrong!'
)
def _process_pubsub_event(self, event):
_id = self.pubsub_id
if event['type'] != 'message':
return
try:
_data = dill.loads(event['data'])
target = _data.get('target')
broadcast = target == 'all'
if not isinstance(_data, dict):
return
# get type, if this is a broken dict just ignore it
if _data.get('type') is None:
return
# ping response
if target == self.shard_id or broadcast:
if _data['type'] == 'ping':
self.redis.publish(
_id,
dill.dumps(
{
'type': 'response',
'id': _data.get('id'),
'response': 'Pong.',
}
),
)
if _data['type'] == 'coderequest':
func = _data.get(
'function'
) # get the function, discard if None
if func is None:
return
resp = {
'type': 'response',
'id': _data.get('id'),
'response': None,
}
if broadcast:
resp['from'] = self.shard_id
args = _data.get('args', ())
kwargs = _data.get('kwargs', {})
try:
# noinspection PyCallingNonCallable
resp['response'] = func(
self, *args, **kwargs
) # this gets run in a thread so whatever
except Exception as e:
resp['response'] = e
try:
self.redis.publish(_id, dill.dumps(resp))
except dill.PicklingError: # if the response fails to dill, return None instead
resp = {'type': 'response', 'id': _data.get('id')}
if broadcast:
resp['from'] = self.shard_id
self.redis.publish(_id, dill.dumps(resp))
if _data['type'] == 'response':
__id = _data.get('id')
_from = _data.get('from')
if __id is None:
return
if __id not in self._pubsub_futures:
return
if (
__id not in self._pubsub_broadcast_cache
and _from is not None
):
return
if _from is None:
self._pubsub_futures[__id].set_result(
_data.get('response')
)
del self._pubsub_futures[__id]
else:
self._pubsub_broadcast_cache[__id][_from] = _data.get(
'response'
)
except dill.UnpicklingError:
return
async def _pubsub_loop(self):
pubsub = self.redis.pubsub()
_id = self.pubsub_id
await pubsub.subscribe(_id)
for event in await pubsub.listen():
self._pubsub_pool.submit(self._process_pubsub_event, event)
def _pubsub_cache_loop(self):
while True:
for k, v in dict(self._pubsub_broadcast_cache).items():
contents = [v[x] for x in v if x != 'expires']
if (
v['expires'] < time.monotonic()
or NoResponse() not in contents
):
del v['expires']
self._pubsub_futures[k].set_result(v)
del self._pubsub_futures[k]
del self._pubsub_broadcast_cache[k]
time.sleep(0.01) # be nice to the host
def request(self, target, broadcast_timeout=1, **kwargs):
_id = str(uuid.uuid4())
self._pubsub_futures[_id] = fut = asyncio.Future()
request = {'id': _id, 'target': target}
request.update(kwargs)
if target == 'all':
cache = {
k: NoResponse() for k in range(0, self.shard_count)
} # prepare the cache
cache['expires'] = time.monotonic() + broadcast_timeout
self._pubsub_broadcast_cache[_id] = cache
self.redis.publish(self.pubsub_id, dill.dumps(request))
return fut
async def run_on_shard(self, shard, func, *args, **kwargs):
return await self.request(
shard,
type='coderequest',
function=func,
args=args,
kwargs=kwargs,
)
async def ping_shard(self, shard, timeout=1):
try:
await asyncio.wait_for(
self.request(shard, type='ping'), timeout=timeout
)
return True
except TimeoutError:
return False
async def on_ready(self):
await self.redis.set(
'__info__',
f'This database is used by the Heleus Discord bot, logged in as user {self.user}.',
)
self.logger.info('Heleus is connected!')
self.logger.info(f'Logged in as {self.user}.')
if self.shard_id is not None:
self.logger.info(
f'Shard {self.shard_id + 1} of {self.shard_count}.'
)
app_info = await self.application_info()
self.invite_url = dutils.oauth_url(app_info.id)
self.logger.info(f'Invite URL: {self.invite_url}')
self.team = app_info.team
if self.test:
self.logger.info('Test complete, logging out...')
await self.close()
exit(0) # jenkins' little helper
async def on_message(self, message):
pass
def __repr__(self):
return '<Heleus username={} shard_id={} shard_count={}>'.format(
*[
repr(x)
for x in [self.user.name, self.shard_id, self.shard_count]
]
)
return Heleus
async def send_cmd_help(ctx):
ctx.invoked_with = 'help'
if ctx.invoked_subcommand:
await ctx.send_help(ctx.invoked_subcommand)
else:
await ctx.send_help(ctx.command)
if __name__ == '__main__':
# Get defaults for argparse
help_description = os.environ.get(
'HELEUS_HELP',
'Heleus, an open-source Discord bot base maintained by DerpyChap, '
'forked from Liara by Cassandra and contributors\n'
'https://github.com/nerdcubed/Heleus',
)
runtime_name = os.environ.get('HELEUS_NAME', 'Heleus')
token = os.environ.get('HELEUS_TOKEN', None)
redis_host = os.environ.get('HELEUS_REDIS_HOST', 'localhost')
redis_pass = os.environ.get('HELEUS_REDIS_PASSWORD', None)
try:
redis_port = int(os.environ.get('HELEUS_REDIS_PORT', 6379))
redis_db = int(os.environ.get('HELEUS_REDIS_DB', 0))
except ValueError:
print(
'Error parsing environment variables HELEUS_REDIS_PORT or HELEUS_REDIS_DB\n'
'Please check that these can be converted to integers'
)
exit(4)
shard_id = os.environ.get('HELEUS_SHARD_ID', None)
shard_count = os.environ.get('HELEUS_SHARD_COUNT', None)
try:
if shard_id is not None:
shard_id = int(shard_id)
if shard_count is not None:
shard_count = int(shard_count)
except ValueError:
print(
'Error parsing environment variables HELEUS_SHARD_ID or HELEUS_SHARD_COUNT\n'
'Please check that these can be converted to integers'
)
exit(4)
message_cache = os.environ.get('HELEUS_MESSAGE_CACHE_COUNT', 5000)
try:
if message_cache is not None:
message_cache = int(message_cache)
except ValueError:
print(
'Error parsing environment variable HELEUS_MESSAGE_CACHE_COUNT\n'
'Please check that this can be converted to an integer'
)
exit(4)
load_cogs = os.environ.get('HELEUS_LOAD_COGS', None)
intents = os.environ.get('HELEUS_INTENTS', 'all')
test_guilds = os.environ.get('HELEUS_TEST_GUILDS', None)
loader = os.environ.get('HELEUS_LOADER', 'cogs.core')
# Parse command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument(
'--description',
type=str,
help='modify the bot description shown in the help command',
default=help_description,
)
parser.add_argument(
'--name',
type=str,
help='allows for white labeling Heleus',
default=runtime_name,
)
parser.add_argument('--debug', help=argparse.SUPPRESS, action='store_true')
parser.add_argument('--test', help=argparse.SUPPRESS, action='store_true')
parser.add_argument(
'--message_cache_count',
help='sets the maximum amount of messages to cache in Heleus.messages',
default=message_cache,
type=int,
)
parser.add_argument(
'--test_guilds',
help='a comma separated list of guild IDs to configure as test servers',
default=test_guilds,
)
parser.add_argument(
'--uvloop', help='enables uvloop mode', action='store_true'
)
parser.add_argument(
'--stateless', help='disables file storage', action='store_true'
)
parser.add_argument(
'--cogs',
help='a comma separated list of cogs to automatically load on startup',
default=load_cogs,
)
parser.add_argument(
'--intents',
help='a comma separated list of Gateway Intents to enable',
default=intents,
)
parser.add_argument(
'token', type=str, help='sets the token', default=token, nargs='?'
)
shard_grp = parser.add_argument_group('sharding')
# noinspection PyUnboundLocalVariable
shard_grp.add_argument(
'--shard_id',
type=int,
help='the shard ID the bot should run on',
default=shard_id,
)
# noinspection PyUnboundLocalVariable
shard_grp.add_argument(
'--shard_count',
type=int,
help='the total number of shards you are planning to run',
default=shard_count,
)
redis_grp = parser.add_argument_group('redis')
redis_grp.add_argument(
'--host', type=str, help='the Redis host', default=redis_host
)
# noinspection PyUnboundLocalVariable
redis_grp.add_argument(
'--port', type=int, help='the Redis port', default=redis_port
)
# noinspection PyUnboundLocalVariable
redis_grp.add_argument(
'--db', type=int, help='the Redis database', default=redis_db
)
redis_grp.add_argument(
'--password', type=str, help='the Redis password', default=redis_pass
)
cargs = parser.parse_args()
if cargs.token is None:
exit(parser.print_usage())
if cargs.uvloop:
try:
# noinspection PyUnresolvedReferences
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
print('uvloop is not installed!')
exit(1)
if not cargs.stateless:
# Logging starts here
# Create directory for logs if it doesn't exist
if not os.path.exists('logs'):
os.mkdir('logs')
# Compress logfiles that were left over from the last run
os.chdir('logs')
if not os.path.exists('old'):
os.mkdir('old')
for item in os.listdir('.'):
if item.endswith('.log'):
with bz2.open(item + '.bz2', 'w') as f:
f.write(open(item, 'rb').read())
os.remove(item)
for item in os.listdir('.'):
if item.endswith('.gz') or item.endswith('.bz2'):
os.rename(item, 'old/' + item)
os.chdir('..')
# Define a format
now = (
str(datetime.datetime.now())
.replace(' ', '_')
.replace(':', '-')
.split('.')[0]
)
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
# Setting up loggers
logger = logging.getLogger('heleus')
if cargs.debug:
logger.setLevel(logging.DEBUG)
sync_commands_debug = True
else:
logger.setLevel(logging.INFO)
sync_commands_debug = False
if not cargs.stateless:
handler = logging.FileHandler(f'logs/heleus_{now}.log')
handler.setFormatter(formatter)
logger.addHandler(handler)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
logger.addHandler(handler)
discord_logger = logging.getLogger('discord')
if cargs.debug:
discord_logger.setLevel(logging.DEBUG)
else:
discord_logger.setLevel(logging.INFO)
if not cargs.stateless:
handler = logging.FileHandler(f'logs/discord_{now}.log')
handler.setFormatter(formatter)
discord_logger.addHandler(handler)
if cargs.intents:
intents_list = cargs.intents.split(',')
if 'all' in intents_list:
intents = discord.Intents.all()
else:
if 'default' in intents_list:
intents = discord.Intents.default()
intents_list.remove('default')
else:
try:
intents_list.remove('none')
except ValueError:
pass
intents = discord.Intents.none()
for i in intents_list:
if not hasattr(intents, i):
logger.warning(f'{i} is not a valid Gateway Intent.')
else:
setattr(intents, i, True)
else:
intents = discord.Intents.none()
if not intents.guilds:
logger.warning(
'Running without the guilds intent is not recommend and is not officially supported. '
'You have been warned!'
)
if cargs.test_guilds:
test_guilds = [int(id) for id in test_guilds.split(',')]
def is_docker():
path = '/proc/self/cgroup'
return (
os.path.exists('/.dockerenv')
or os.path.isfile(path)
and any('docker' in line for line in open(path))
)
if not is_docker():
logger.warning(
'Running outside of a Docker container, while should work with the right setup, is not officially '
"supported. DO NOT expect any support if things go wrong. You've been warned!"
)
if cargs.shard_id is not None: # usability
cargs.shard_id -= 1
# Redis connection attempt
redis_conn = coredis.Redis(
host=cargs.host, port=cargs.port, db=cargs.db, password=cargs.password
)
# sharding logic
unsharded = True
if cargs.shard_id is not None:
unsharded = False
heleus_cls = create_bot(unsharded)
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
loop: asyncio.AbstractEventLoop = asyncio.get_event_loop()
# noinspection PyUnboundLocalVariable
heleus = heleus_cls(
load_cogs=cargs.cogs,
intents=intents,
test_guilds=test_guilds,
command_sync_flags=commands.CommandSyncFlags(
sync_commands_debug=sync_commands_debug
),
shard_id=cargs.shard_id,
shard_count=cargs.shard_count,
description=cargs.description,
help_command=CustomHelp(),
pm_help=None,
max_messages=message_cache,
redis=redis_conn,
cargs=cargs,
test=cargs.test,
name=cargs.name,
loader=loader,
command_prefix=commands.when_mentioned,
loop=loop,
) # heleus-specific args
# Removing the help command here instead of using `help_command=None` in the bot
# definition ensures that `send_cmd_help` can still function as expected
heleus.remove_command('help')
async def run_bot():
await heleus.redis.ping()
heleus.init()
await heleus.start(cargs.token)
# noinspection PyBroadException
def run_app():
exit_code = 0
try:
loop.run_until_complete(run_bot())
except KeyboardInterrupt:
logger.info(
'Shutting down threads and quitting. Thank you for using Heleus.'
)
loop.run_until_complete(heleus.close())
except coredis.ConnectionError:
exit_code = 2
logger.critical('Unable to connect to Redis.')
except discord.LoginFailure:
exit_code = 3
logger.critical('Discord token is not valid.')
except Exception:
exit_code = 1
logger.exception('Exception while running Heleus.')
loop.run_until_complete(heleus.close())
finally:
loop.close()
return exit_code
exit(run_app())