feat(bot): 🚧 Start migrating to k8s.
This commit is contained in:
+29
-21
@@ -28,9 +28,6 @@ from payphone.core import (
|
||||
)
|
||||
|
||||
|
||||
logging.setup()
|
||||
|
||||
|
||||
class Payphone(commands.AutoShardedBot):
|
||||
shutdown: managers.ValueProxy[bool]
|
||||
"""A value proxy that connects with all other shard processes.
|
||||
@@ -74,11 +71,34 @@ class Payphone(commands.AutoShardedBot):
|
||||
)
|
||||
|
||||
async def on_ready(self) -> None:
|
||||
"""Runs when the bot has finished initializing.
|
||||
"""
|
||||
"""Runs when the bot has finished initializing."""
|
||||
|
||||
self.log.info("Ready! Logged in as %s.", self.user.display_name)
|
||||
|
||||
async def set_presence(self, shard_id: int) -> None:
|
||||
"""Sets the presence of the bot back to online.
|
||||
|
||||
Args:
|
||||
shard_id (int): The ID of the shard to set the status for.
|
||||
"""
|
||||
|
||||
await self.change_presence(
|
||||
activity=discord.CustomActivity(
|
||||
name=f"Listening to p.help | Shard {shard_id + 1}"
|
||||
),
|
||||
status=discord.Status.online,
|
||||
shard_id=shard_id,
|
||||
)
|
||||
|
||||
async def on_shard_connect(self, shard_id: int) -> None:
|
||||
"""Runs when the shard connects.
|
||||
|
||||
Args:
|
||||
shard_id (int): The ID of the shard that connected.
|
||||
"""
|
||||
|
||||
self.log.info("Shard has connected.")
|
||||
|
||||
async def on_shard_ready(self, shard_id: int) -> None:
|
||||
"""Sets the activity for the shard.
|
||||
|
||||
@@ -86,15 +106,9 @@ class Payphone(commands.AutoShardedBot):
|
||||
shard_id (int): The ID of the initialized shard.
|
||||
"""
|
||||
|
||||
await self.change_presence(
|
||||
activity=discord.CustomActivity(
|
||||
name=f"Listening to p.help | Shard {shard_id + 1}"
|
||||
),
|
||||
status=discord.Status.online,
|
||||
shard_id=shard_id,
|
||||
)
|
||||
self.log.info("Shard is ready.")
|
||||
|
||||
self.log.info("Shard %s is ready!", shard_id)
|
||||
await self.set_presence(shard_id)
|
||||
|
||||
async def on_shard_resumed(self, shard_id: int) -> None:
|
||||
"""Sets the activity for the shard.
|
||||
@@ -103,15 +117,9 @@ class Payphone(commands.AutoShardedBot):
|
||||
shard_id (int): The ID of the resumed shard.
|
||||
"""
|
||||
|
||||
await self.change_presence(
|
||||
activity=discord.CustomActivity(
|
||||
name=f"Listening to p.help | Shard {shard_id + 1}"
|
||||
),
|
||||
status=discord.Status.online,
|
||||
shard_id=shard_id,
|
||||
)
|
||||
self.log.info("Shard has resumed.")
|
||||
|
||||
self.log.info("Shard %s was resumed!", shard_id)
|
||||
await self.set_presence(shard_id)
|
||||
|
||||
async def get_or_fetch_channel(
|
||||
self, channel_id: int
|
||||
|
||||
+29
-10
@@ -3,17 +3,36 @@ import logging
|
||||
from rich import logging as rlogging
|
||||
|
||||
|
||||
FORMAT = "%(message)s"
|
||||
|
||||
handler = rlogging.RichHandler(level=logging.INFO, rich_tracebacks=True)
|
||||
|
||||
formatter = logging.Formatter(FORMAT)
|
||||
handler.setFormatter(formatter)
|
||||
|
||||
logger = logging.getLogger("payphone")
|
||||
|
||||
|
||||
def setup():
|
||||
"""Sets up pycord logging."""
|
||||
def _get_handler(process_name: str, module: str | None = None) -> None:
|
||||
"""Generates a handler for the given process name and module.
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG, handlers=[handler])
|
||||
Args:
|
||||
process_name (str): The process name.
|
||||
module (str | None, optional): The module inside the process. Defaults to None.
|
||||
"""
|
||||
|
||||
fmt = f"[blue][{process_name}{f": [green]{module}[/green]" if module is not None else ""}][/blue] %(message)s"
|
||||
formatter = logging.Formatter(fmt)
|
||||
|
||||
handler = rlogging.RichHandler(
|
||||
level=logging.INFO, markup=True, rich_tracebacks=True
|
||||
)
|
||||
handler.setFormatter(formatter)
|
||||
|
||||
return handler
|
||||
|
||||
|
||||
def setup(process_name: str):
|
||||
"""Sets up pycord logging.
|
||||
|
||||
Args:
|
||||
process_name (str): The name of the process.
|
||||
"""
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG, handlers=[_get_handler(process_name)])
|
||||
|
||||
logging.getLogger("asyncio").addHandler(_get_handler(process_name, "asyncio"))
|
||||
logging.getLogger("discord").addHandler(_get_handler(process_name, "discord"))
|
||||
|
||||
Reference in New Issue
Block a user