54 lines
1.1 KiB
Python
54 lines
1.1 KiB
Python
def shard(shard_id: int, shard_count: int) -> None:
|
|
"""Starts a new shard.
|
|
|
|
Args:
|
|
shard_id (int): The shard's ID.
|
|
shard_count (int): The total amount of shards.
|
|
"""
|
|
|
|
from payphone.bot import bot
|
|
from payphone.core import config
|
|
|
|
bot.shard_ids = [shard_id]
|
|
bot.shard_count = shard_count
|
|
|
|
bot.run(config.TOKEN)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import multiprocessing as mp
|
|
|
|
import time
|
|
|
|
import httpx
|
|
|
|
from payphone.core import config
|
|
|
|
response = httpx.get(
|
|
"https://discord.com/api/v10/gateway/bot",
|
|
headers={"Authorization": f"Bot {config.TOKEN}"},
|
|
)
|
|
|
|
info = response.json()
|
|
|
|
shard_count = info["shards"]
|
|
|
|
print(f"Starting {shard_count} shard{"s" if shard_count != 1 else ""}.")
|
|
|
|
ps: list[mp.Process] = [
|
|
mp.Process(target=shard, args=(i, shard_count)) for i in range(shard_count)
|
|
]
|
|
|
|
for p in ps:
|
|
p.start()
|
|
|
|
time.sleep(5)
|
|
|
|
while True:
|
|
for i in range(shard_count):
|
|
if not ps[i].is_alive():
|
|
ps[i] = mp.Process(target=shard, args=(i, shard_count))
|
|
ps[i].start()
|
|
|
|
time.sleep(5)
|