Skip to content

Commit 359c00d

Browse files
committed
Check for local git repository
Implements a checking mechanismen for local git repository.
1 parent 57ac0f1 commit 359c00d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

bot.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,33 @@ def check_manual_blocked(self, author: discord.Member) -> bool:
758758
logger.debug("User blocked, user %s.", author.name)
759759
return False
760760

761+
def check_local_git(self) -> bool:
762+
"""
763+
Checks if the bot is installed via git.
764+
"""
765+
valid_local_git = False
766+
git_folder_path = os.path.join(".git")
767+
768+
# Check if the .git folder exists and is a directory
769+
if os.path.exists(git_folder_path) and os.path.isdir(git_folder_path):
770+
required_files = ["config", "HEAD"]
771+
required_dirs = ["refs", "objects"]
772+
773+
# Verify required files exist
774+
for file in required_files:
775+
if not os.path.isfile(os.path.join(git_folder_path, file)):
776+
return valid_local_git
777+
778+
# Verify required directories exist
779+
for directory in required_dirs:
780+
if not os.path.isdir(os.path.join(git_folder_path, directory)):
781+
return valid_local_git
782+
783+
# If all checks pass, set valid_local_git to True
784+
valid_local_git = True
785+
786+
return valid_local_git
787+
761788
async def _process_blocked(self, message):
762789
_, blocked_emoji = await self.retrieve_emoji()
763790
if await self.is_blocked(message.author, channel=message.channel, send_message=True):
@@ -1721,6 +1748,12 @@ async def before_autoupdate(self):
17211748
self.autoupdate.cancel()
17221749
return
17231750

1751+
if not self.check_local_git():
1752+
logger.warning("Bot not installed via git.")
1753+
logger.warning("Autoupdates disabled.")
1754+
self.autoupdate.cancel()
1755+
return
1756+
17241757
@tasks.loop(hours=1, reconnect=False)
17251758
async def log_expiry(self):
17261759
log_expire_after = self.config.get("log_expiration")

cogs/utility.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,14 @@ async def update(self, ctx, *, flag: str = ""):
20032003
embed.set_author(name=user["username"], icon_url=user["avatar_url"], url=user["url"])
20042004
await ctx.send(embed=embed)
20052005
else:
2006+
if self.bot.check_local_git() is False:
2007+
embed = discord.Embed(
2008+
title="Update Command Unavailable",
2009+
description="The bot cannot be updated due to not being installed via a git."
2010+
"You need to manually update the bot according to your hosting method.",
2011+
color=discord.Color.red(),
2012+
)
2013+
return await ctx.send(embed=embed)
20062014
command = "git pull"
20072015
proc = await asyncio.create_subprocess_shell(
20082016
command,

0 commit comments

Comments
 (0)