Skip to content

Commit

Permalink
imp: Allow updating bot version without restarting
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Oct 13, 2024
1 parent 4a093f5 commit 0ba11af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
20 changes: 16 additions & 4 deletions hoyo_buddy/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ def __init__(
pool: asyncpg.Pool,
config: Config,
) -> None:
self.repo = git.Repo()
self.version = get_repo_version(self.repo)

super().__init__(
command_prefix=commands.when_mentioned,
intents=discord.Intents(guilds=True, members=True, emojis=True, messages=True),
Expand All @@ -84,7 +81,6 @@ def __init__(
max_messages=None,
member_cache_flags=discord.MemberCacheFlags.none(),
tree_cls=CommandTree,
activity=discord.CustomActivity(f"{self.version} | hb.seria.moe"),
allowed_contexts=discord.app_commands.AppCommandContext(
guild=True, dm_channel=True, private_channel=True
),
Expand Down Expand Up @@ -115,6 +111,19 @@ def __init__(

self.geetest_command_task: asyncio.Task | None = None

@property
def repo(self) -> git.Repo:
return git.Repo()

@property
def version(self) -> str:
return get_repo_version()

async def set_version_status(self) -> None:
await self.change_presence(
activity=discord.CustomActivity(f"{self.version} | hb.seria.moe")
)

async def setup_hook(self) -> None:
# Initialize genshin.py sqlite cache
async with aiosqlite.connect("genshin_py.db") as conn:
Expand Down Expand Up @@ -146,6 +155,9 @@ async def setup_hook(self) -> None:
listener.run({"geetest_command": self.handle_geetest_notify}, notification_timeout=2)
)

async def on_ready(self) -> None:
await self.set_version_status()

def capture_exception(self, e: Exception) -> None:
# Errors to suppress
if isinstance(e, aiohttp.ClientConnectorError | aiohttp.ServerDisconnectedError):
Expand Down
5 changes: 5 additions & 0 deletions hoyo_buddy/cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ async def fill_lb_command(self, ctx: commands.Context) -> Any:

await ctx.send("Done.")

@commands.command(name="update-version")
async def update_version_command(self, ctx: commands.Context) -> Any:
await self.bot.set_version_status()
await ctx.send("Done.")


async def setup(bot: HoyoBuddy) -> None:
await bot.add_cog(Admin(bot))
4 changes: 2 additions & 2 deletions hoyo_buddy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ def dict_cookie_to_str(cookie_dict: dict[str, str]) -> str:
return "; ".join([f"{key}={value}" for key, value in cookie_dict.items()])


def get_repo_version(repo: git.Repo | None = None) -> str:
repo = repo or git.Repo()
def get_repo_version() -> str:
repo = git.Repo()
tags = sorted(repo.tags, key=lambda t: t.commit.committed_datetime)
return tags[-1].name

Expand Down

0 comments on commit 0ba11af

Please sign in to comment.