Compare commits
6 commits
850ddf15a6
...
a563a42d2e
Author | SHA1 | Date | |
---|---|---|---|
a563a42d2e | |||
2859f93501 | |||
7d1a9cc01a | |||
5adc7a2c7b | |||
5384809780 | |||
78f036da48 |
8 changed files with 150 additions and 33 deletions
|
@ -30,6 +30,6 @@
|
|||
"PROJECT_DIR": "/workspaces/SeaCogs"
|
||||
},
|
||||
"mounts": ["source=seacogs-persistent-data,target=/workspaces/SeaCogs/.data,type=volume"],
|
||||
"postCreateCommand": "uv sync --frozen && sudo chown -R vscode:vscode /workspaces/SeaCogs/.data",
|
||||
"postCreateCommand": "uv sync --frozen && sudo chown -R vscode:vscode /workspaces/SeaCogs/.data && uv run redbot-setup --no-prompt --instance-name=local --data-path=/workspaces/SeaCogs/.data --backend=json",
|
||||
"remoteUser": "vscode"
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
|
||||
|
||||
import random
|
||||
from asyncio import create_task
|
||||
from io import BytesIO
|
||||
|
||||
import aiohttp
|
||||
|
@ -26,20 +27,21 @@ class Bible(commands.Cog):
|
|||
|
||||
__author__ = ["[cswimr](https://www.coastalcommits.com/cswimr)"]
|
||||
__git__ = "https://www.coastalcommits.com/cswimr/SeaCogs"
|
||||
__version__ = "1.1.1"
|
||||
__version__ = "1.1.2"
|
||||
__documentation__ = "https://seacogs.coastalcommits.com/pterodactyl/"
|
||||
|
||||
def __init__(self, bot: Red):
|
||||
super().__init__()
|
||||
self.bot = bot
|
||||
self.session = aiohttp.ClientSession()
|
||||
self.config = Config.get_conf(
|
||||
self, identifier=481923957134912, force_registration=True
|
||||
)
|
||||
self.config = Config.get_conf(self, identifier=481923957134912, force_registration=True)
|
||||
self.logger = getLogger("red.SeaCogs.Bible")
|
||||
self.config.register_global(bible="de4e12af7f28f599-02")
|
||||
self.config.register_user(bible=None)
|
||||
|
||||
def cog_unload(self):
|
||||
create_task(self.session.close())
|
||||
|
||||
def format_help_for_context(self, ctx: commands.Context) -> str:
|
||||
pre_processed = super().format_help_for_context(ctx) or ""
|
||||
n = "\n" if "\n\n" not in pre_processed else ""
|
||||
|
@ -51,7 +53,6 @@ class Bible(commands.Cog):
|
|||
]
|
||||
return "\n".join(text)
|
||||
|
||||
|
||||
def get_icon(self, color: Colour) -> File:
|
||||
"""Get the docs.api.bible favicon with a given color."""
|
||||
image_path = data_manager.bundled_data_path(self) / "api.bible-logo.png"
|
||||
|
@ -70,9 +71,7 @@ class Bible(commands.Cog):
|
|||
|
||||
async def translate_book_name(self, bible_id: str, book_name: str) -> str:
|
||||
"""Translate a book name to a book ID."""
|
||||
book_name_list = [
|
||||
w.lower() if w.lower() == "of" else w.title() for w in book_name.split()
|
||||
]
|
||||
book_name_list = [w.lower() if w.lower() == "of" else w.title() for w in book_name.split()]
|
||||
book_name = " ".join(book_name_list)
|
||||
books = await self._get_books(bible_id)
|
||||
for book in books:
|
||||
|
@ -247,13 +246,9 @@ class Bible(commands.Cog):
|
|||
from_verse, to_verse = passage.replace(":", ".").split("-")
|
||||
if "." not in to_verse:
|
||||
to_verse = f"{from_verse.split('.')[0]}.{to_verse}"
|
||||
passage = await self._get_passage(
|
||||
ctx, bible_id, f"{book_id}.{from_verse}-{book_id}.{to_verse}", True
|
||||
)
|
||||
passage = await self._get_passage(ctx, bible_id, f"{book_id}.{from_verse}-{book_id}.{to_verse}", True)
|
||||
else:
|
||||
passage = await self._get_passage(
|
||||
ctx, bible_id, f"{book_id}.{passage.replace(':', '.')}", False
|
||||
)
|
||||
passage = await self._get_passage(ctx, bible_id, f"{book_id}.{passage.replace(':', '.')}", False)
|
||||
except (
|
||||
bible.errors.BibleAccessError,
|
||||
bible.errors.NotFound,
|
||||
|
@ -275,10 +270,7 @@ class Bible(commands.Cog):
|
|||
description=passage["content"].replace("¶ ", ""),
|
||||
color=await ctx.embed_color(),
|
||||
)
|
||||
embed.set_footer(
|
||||
text=f"{ctx.prefix}bible passage - Powered by API.Bible - {version.abbreviationLocal} ({version.languageLocal}, {version.descriptionLocal})",
|
||||
icon_url="attachment://icon.png"
|
||||
)
|
||||
embed.set_footer(text=f"{ctx.prefix}bible passage - Powered by API.Bible - {version.abbreviationLocal} ({version.languageLocal}, {version.descriptionLocal})", icon_url="attachment://icon.png")
|
||||
await ctx.send(embed=embed, file=icon)
|
||||
else:
|
||||
await ctx.send(f"## {passage['reference']}\n{passage['content']}")
|
||||
|
@ -317,10 +309,7 @@ class Bible(commands.Cog):
|
|||
description=passage["content"].replace("¶ ", ""),
|
||||
color=await ctx.embed_color(),
|
||||
)
|
||||
embed.set_footer(
|
||||
text=f"{ctx.prefix}bible random - Powered by API.Bible - {version.abbreviationLocal} ({version.languageLocal}, {version.descriptionLocal})",
|
||||
icon_url="attachment://icon.png"
|
||||
)
|
||||
embed.set_footer(text=f"{ctx.prefix}bible random - Powered by API.Bible - {version.abbreviationLocal} ({version.languageLocal}, {version.descriptionLocal})", icon_url="attachment://icon.png")
|
||||
await ctx.send(embed=embed, file=icon)
|
||||
else:
|
||||
await ctx.send(f"## {passage['reference']}\n{passage['content']}")
|
||||
|
|
|
@ -4,9 +4,7 @@ from redbot.core.utils.chat_formatting import error
|
|||
class BibleAccessError(Exception):
|
||||
def __init__(
|
||||
self,
|
||||
message: str = error(
|
||||
"The provided API key cannot retrieve sections from the configured Bible. Please report this to the bot owner."
|
||||
),
|
||||
message: str = error("The provided API key cannot retrieve sections from the configured Bible. Please report this to the bot owner."),
|
||||
):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
|
@ -15,9 +13,7 @@ class BibleAccessError(Exception):
|
|||
class Unauthorized(Exception):
|
||||
def __init__(
|
||||
self,
|
||||
message: str = error(
|
||||
"The API key for API.Bible is missing or invalid. Please report this to the bot owner.\nIf you are the bot owner, please check the documentation [here](<https://seacogs.coastalcommits.com/bible/#setup>)."
|
||||
),
|
||||
message: str = error("The API key for API.Bible is missing or invalid. Please report this to the bot owner.\nIf you are the bot owner, please check the documentation [here](<https://seacogs.coastalcommits.com/bible/#setup>)."),
|
||||
):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
|
@ -44,9 +40,7 @@ class ServiceUnavailable(Exception):
|
|||
class InexplicableError(Exception):
|
||||
def __init__(
|
||||
self,
|
||||
message: str = error(
|
||||
"An inexplicable 'Bad Request' error occurred. This error happens occassionally with the API.Bible service. Please try again. If the error persists, please report this to the bot owner."
|
||||
),
|
||||
message: str = error("An inexplicable 'Bad Request' error occurred. This error happens occasionally with the API.Bible service. Please try again. If the error persists, please report this to the bot owner."),
|
||||
):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
|
|
5
hotreload/__init__.py
Normal file
5
hotreload/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from .hotreload import HotReload
|
||||
|
||||
|
||||
async def setup(bot):
|
||||
await bot.add_cog(HotReload(bot))
|
109
hotreload/hotreload.py
Normal file
109
hotreload/hotreload.py
Normal file
|
@ -0,0 +1,109 @@
|
|||
from asyncio import run_coroutine_threadsafe
|
||||
from pathlib import Path
|
||||
from typing import Sequence
|
||||
|
||||
from red_commons.logging import RedTraceLogger, getLogger
|
||||
from redbot.core import commands
|
||||
from redbot.core.bot import Red
|
||||
from redbot.core.core_commands import CoreLogic
|
||||
from redbot.core.utils.chat_formatting import bold, humanize_list
|
||||
from watchdog.events import FileSystemEvent, FileSystemMovedEvent, RegexMatchingEventHandler
|
||||
from watchdog.observers import Observer
|
||||
|
||||
|
||||
class HotReload(commands.Cog):
|
||||
"""Automatically reload cogs in local cog paths on file change."""
|
||||
|
||||
__author__ = ["[cswimr](https://www.coastalcommits.com/cswimr)"]
|
||||
__git__ = "https://www.coastalcommits.com/cswimr/SeaCogs"
|
||||
__version__ = "1.1.1"
|
||||
__documentation__ = "https://seacogs.coastalcommits.com/hotreload/"
|
||||
|
||||
def __init__(self, bot: Red) -> None:
|
||||
super().__init__()
|
||||
self.bot: Red = bot
|
||||
self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReload")
|
||||
self.observer = None
|
||||
watchdog_loggers = [getLogger(name="watchdog.observers.inotify_buffer")]
|
||||
for watchdog_logger in watchdog_loggers:
|
||||
watchdog_logger.setLevel("INFO") # SHUT UP!!!!
|
||||
|
||||
def cog_load(self) -> None:
|
||||
"""Start the observer when the cog is loaded."""
|
||||
self.bot.loop.create_task(self.start_observer())
|
||||
|
||||
def cog_unload(self) -> None:
|
||||
"""Stop the observer when the cog is unloaded."""
|
||||
if self.observer:
|
||||
self.observer.stop()
|
||||
self.observer.join()
|
||||
self.logger.info("Stopped observer. No longer watching for file changes.")
|
||||
|
||||
def format_help_for_context(self, ctx: commands.Context) -> str:
|
||||
pre_processed = super().format_help_for_context(ctx) or ""
|
||||
n = "\n" if "\n\n" not in pre_processed else ""
|
||||
text = [
|
||||
f"{pre_processed}{n}",
|
||||
f"{bold('Cog Version:')} [{self.__version__}]({self.__git__})",
|
||||
f"{bold('Author:')} {humanize_list(self.__author__)}",
|
||||
f"{bold('Documentation:')} {self.__documentation__}",
|
||||
]
|
||||
return "\n".join(text)
|
||||
|
||||
async def get_paths(self) -> tuple[Path]:
|
||||
"""Retrieve user defined paths."""
|
||||
cog_manager = self.bot._cog_mgr
|
||||
cog_paths = await cog_manager.user_defined_paths()
|
||||
return (Path(path) for path in cog_paths)
|
||||
|
||||
async def start_observer(self) -> None:
|
||||
"""Start the observer to watch for file changes."""
|
||||
self.observer = Observer()
|
||||
paths = await self.get_paths()
|
||||
for path in paths:
|
||||
self.observer.schedule(event_handler=HotReloadHandler(bot=self.bot, path=path), path=path, recursive=True)
|
||||
self.observer.start()
|
||||
self.logger.info("Started observer. Watching for file changes.")
|
||||
|
||||
|
||||
class HotReloadHandler(RegexMatchingEventHandler):
|
||||
"""Handler for file changes."""
|
||||
|
||||
def __init__(self, bot: Red, path: Path) -> None:
|
||||
super().__init__(regexes=[r".*\.py$"])
|
||||
self.bot: Red = bot
|
||||
self.path: Path = path
|
||||
self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReload.Observer")
|
||||
|
||||
def on_any_event(self, event: FileSystemEvent) -> None:
|
||||
"""Handle filesystem events."""
|
||||
if event.is_directory:
|
||||
return
|
||||
|
||||
allowed_events = ("moved", "deleted", "created", "modified")
|
||||
if event.event_type not in allowed_events:
|
||||
return
|
||||
|
||||
relative_src_path = Path(event.src_path).relative_to(self.path)
|
||||
src_package_name = relative_src_path.parts[0]
|
||||
cogs_to_reload = [src_package_name]
|
||||
|
||||
if isinstance(event, FileSystemMovedEvent):
|
||||
dest = f" to {event.dest_path}"
|
||||
relative_dest_path = Path(event.dest_path).relative_to(self.path)
|
||||
dest_package_name = relative_dest_path.parts[0]
|
||||
if dest_package_name != src_package_name:
|
||||
cogs_to_reload.append(dest_package_name)
|
||||
else:
|
||||
dest = ""
|
||||
|
||||
self.logger.info(f"File {event.src_path} has been {event.event_type}{dest}.")
|
||||
|
||||
run_coroutine_threadsafe(self.reload_cogs(cogs_to_reload), loop=self.bot.loop)
|
||||
|
||||
async def reload_cogs(self, cog_names: Sequence[str]) -> None:
|
||||
"""Reload modified cog."""
|
||||
core_logic = CoreLogic(bot=self.bot)
|
||||
self.logger.info(f"Reloading cogs: {humanize_list(cog_names, style='unit')}")
|
||||
await core_logic._reload(pkg_names=cog_names)
|
||||
self.logger.info(f"Reloaded cogs: {humanize_list(cog_names, style='unit')}")
|
17
hotreload/info.json
Normal file
17
hotreload/info.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"author" : ["cswimr"],
|
||||
"install_msg" : "Thank you for installing HotReload!",
|
||||
"name" : "HotReload",
|
||||
"short" : "Automatically reload cogs in local cog paths on file change.",
|
||||
"description" : "Automatically reload cogs in local cog paths on file change.",
|
||||
"end_user_data_statement" : "This cog does not store end user data.",
|
||||
"hidden": false,
|
||||
"disabled": false,
|
||||
"min_bot_version": "3.5.0",
|
||||
"min_python_version": [3, 10, 0],
|
||||
"requirements": ["watchdog"],
|
||||
"tags": [
|
||||
"utility",
|
||||
"development"
|
||||
]
|
||||
}
|
|
@ -18,6 +18,7 @@ dependencies = [
|
|||
"py-dactyl",
|
||||
"pydantic>=2.9.2",
|
||||
"red-discordbot>=3.5.14",
|
||||
"watchdog>=5.0.3",
|
||||
"websockets>=13.1",
|
||||
]
|
||||
|
||||
|
|
2
uv.lock
generated
2
uv.lock
generated
|
@ -1667,6 +1667,7 @@ dependencies = [
|
|||
{ name = "py-dactyl" },
|
||||
{ name = "pydantic" },
|
||||
{ name = "red-discordbot" },
|
||||
{ name = "watchdog" },
|
||||
{ name = "websockets" },
|
||||
]
|
||||
|
||||
|
@ -1706,6 +1707,7 @@ requires-dist = [
|
|||
{ name = "py-dactyl", git = "https://github.com/cswimr/pydactyl" },
|
||||
{ name = "pydantic", specifier = ">=2.9.2" },
|
||||
{ name = "red-discordbot", specifier = ">=3.5.14" },
|
||||
{ name = "watchdog", specifier = ">=5.0.3" },
|
||||
{ name = "websockets", specifier = ">=13.1" },
|
||||
]
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue