├── README.md ├── LICENSE ├── instance.py ├── .gitignore └── aio_launcher.py /README.md: -------------------------------------------------------------------------------- 1 | # AIO_Launcher 2 | A replacement to the built-in Discord.py AutoSharder, which splits shards up across multiple threads with console-input support 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 KawaiiDevs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /instance.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | from datetime import datetime 4 | 5 | import discord 6 | from discord.ext.commands import when_mentioned_or as wmo 7 | from discord.ext.commands import AutoShardedBot 8 | 9 | 10 | class Instance: 11 | def __init__(self, instance, shard_count, ids, pipe): 12 | self.pipe = pipe 13 | 14 | with open('config.json') as r: 15 | config = json.load(r) 16 | 17 | self.bot = AutoShardedBot(command_prefix=wmo(config.get('prefix')), shard_count=shard_count, shard_ids=ids, 18 | help_attrs=dict(Hidden=True), pm_help=True, fetch_offline_members=False) 19 | 20 | self.bot.prefix = config.get('prefix') 21 | self.bot.instance = instance 22 | self.bot.startup = datetime.now() 23 | 24 | self.bot.add_listener(self.on_ready) 25 | self.bot.run(config.get('token')) 26 | 27 | async def on_ready(self): 28 | for file in os.listdir("cogs"): 29 | if file.endswith(".py"): 30 | name = file[:-3] 31 | self.bot.load_extension(f"cogs.{name}") 32 | 33 | print(f'[Instance-{self.bot.instance}] Ready!') 34 | self.pipe.send(1) 35 | self.pipe.close() 36 | 37 | async def on_message(self, msg): 38 | if not self.bot.is_ready() or msg.author.bot or \ 39 | not (isinstance(msg.channel, discord.DMChannel) or msg.channel.permissions_for(msg.guild.me).send_messages): 40 | return 41 | 42 | await self.bot.process_commands(msg) 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /aio_launcher.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import math 3 | import os 4 | import shutil 5 | import subprocess 6 | import sys 7 | from multiprocessing import Process, Pipe 8 | 9 | import instance 10 | 11 | 12 | loop = asyncio.get_event_loop() 13 | executable = str(shutil.which('python3.6') or shutil.which('py')).split('/')[-1] 14 | instance_queue = [] 15 | instances = [] 16 | 17 | 18 | class PendingInstance: 19 | def __init__(self, i_id, total_shards, shard_ids): 20 | self.id = i_id 21 | self.total_shards = total_shards 22 | self.shard_ids = shard_ids 23 | 24 | 25 | def launch_next_shard(): 26 | if instance_queue: 27 | i = instance_queue.pop(0) 28 | print('Launching instance {}'.format(i.id)) 29 | listen, send = Pipe() 30 | p = Process(target=instance.Instance, args=(i.id, i.total_shards, i.shard_ids, send,)) 31 | instances.append(p) 32 | p.start() 33 | 34 | if listen.recv() == 1: 35 | launch_next_shard() 36 | listen.close() 37 | else: 38 | print('All instances launched!') 39 | 40 | 41 | def wait(delay: int): 42 | loop.run_until_complete(asyncio.sleep(delay)) 43 | 44 | 45 | if __name__ == '__main__': 46 | shards_per_instance = 1 47 | total_shards = 2 48 | 49 | sharded = '--sharded' in sys.argv 50 | 51 | if sharded: 52 | print('Sharding enabled. Validating shard count...') 53 | if total_shards >= 40 and total_shards % 16 != 0: # 40 * 2,500 = 100,000 (see: https://github.com/discordapp/discord-api-docs/issues/387) 54 | print('Bad shard count: total_shards must be a multiple of 16') 55 | sys.exit(0) 56 | 57 | total_instances = math.ceil(total_shards / shards_per_instance) 58 | print('Using {} instances'.format(total_instances)) 59 | 60 | wait(5) 61 | 62 | for i in range(0, total_instances): 63 | start = i * shards_per_instance 64 | last = min(start + shards_per_instance, total_shards) 65 | ids = list(range(start, last)) 66 | 67 | print('Appending instance {} to launch queue...'.format(i)) 68 | instance_queue.append(PendingInstance(i, total_shards, ids)) 69 | else: 70 | instance_queue.append(PendingInstance(0, 1, [0], launch_next_shard)) 71 | 72 | launch_next_shard() 73 | 74 | try: 75 | while True: # Keep the main process up 76 | wait(5) 77 | except KeyboardInterrupt: 78 | pass 79 | --------------------------------------------------------------------------------