Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for local git repository #3358

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,33 @@ def check_manual_blocked(self, author: discord.Member) -> bool:
logger.debug("User blocked, user %s.", author.name)
return False

def check_local_git(self) -> bool:
"""
Checks if the bot is installed via git.
"""
valid_local_git = False
git_folder_path = os.path.join(".git")

# Check if the .git folder exists and is a directory
if os.path.exists(git_folder_path) and os.path.isdir(git_folder_path):
required_files = ["config", "HEAD"]
required_dirs = ["refs", "objects"]

# Verify required files exist
for file in required_files:
if not os.path.isfile(os.path.join(git_folder_path, file)):
return valid_local_git

# Verify required directories exist
for directory in required_dirs:
if not os.path.isdir(os.path.join(git_folder_path, directory)):
return valid_local_git

# If all checks pass, set valid_local_git to True
valid_local_git = True

return valid_local_git

async def _process_blocked(self, message):
_, blocked_emoji = await self.retrieve_emoji()
if await self.is_blocked(message.author, channel=message.channel, send_message=True):
Expand Down Expand Up @@ -1721,6 +1748,12 @@ async def before_autoupdate(self):
self.autoupdate.cancel()
return

if not self.check_local_git():
logger.warning("Bot not installed via git.")
logger.warning("Autoupdates disabled.")
self.autoupdate.cancel()
return

@tasks.loop(hours=1, reconnect=False)
async def log_expiry(self):
log_expire_after = self.config.get("log_expiration")
Expand Down
8 changes: 8 additions & 0 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,14 @@ async def update(self, ctx, *, flag: str = ""):
embed.set_author(name=user["username"], icon_url=user["avatar_url"], url=user["url"])
await ctx.send(embed=embed)
else:
if self.bot.check_local_git() is False:
embed = discord.Embed(
title="Update Command Unavailable",
description="The bot cannot be updated due to not being installed via git."
"You need to manually update the bot according to your hosting method.",
color=discord.Color.red(),
)
return await ctx.send(embed=embed)
command = "git pull"
proc = await asyncio.create_subprocess_shell(
command,
Expand Down
Loading