Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -794,6 +794,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 @@ -2160,6 +2187,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
29 changes: 13 additions & 16 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -2134,11 +2134,7 @@ async def update(self, ctx, *, flag: str = ""):
data = await self.bot.api.get_user_info()
if data:
user = data["user"]
embed.set_author(
name=user["username"],
icon_url=user["avatar_url"] if user["avatar_url"] else None,
url=user["url"],
)
embed.set_author(name=user["username"], icon_url=user["avatar_url"], url=user["url"])
await ctx.send(embed=embed)
else:
error = None
Expand Down Expand Up @@ -2177,7 +2173,7 @@ async def update(self, ctx, *, flag: str = ""):

embed.set_author(
name=user["username"] + " - Updating bot",
icon_url=user["avatar_url"] if user["avatar_url"] else None,
icon_url=user["avatar_url"],
url=user["url"],
)

Expand All @@ -2195,13 +2191,18 @@ async def update(self, ctx, *, flag: str = ""):
color=self.bot.main_color,
)
embed.set_footer(text="Force update")
embed.set_author(
name=user["username"],
icon_url=user["avatar_url"] if user["avatar_url"] else None,
url=user["url"],
)
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 a git."
"You need to manually update the bot according to your hosting method."
"If you face any issues please don´t hesitate to contact modmail support.",
color=discord.Color.red(),
)
return await ctx.send(embed=embed)
command = "git pull"
proc = await asyncio.create_subprocess_shell(
command,
Expand All @@ -2214,11 +2215,7 @@ async def update(self, ctx, *, flag: str = ""):
res = res.decode("utf-8").rstrip()

if err and not res:
embed = discord.Embed(
title="Update failed",
description=err,
color=self.bot.error_color,
)
embed = discord.Embed(title="Update failed", description=err, color=self.bot.error_color)
await ctx.send(embed=embed)

elif res != "Already up to date.":
Expand Down
Loading