Skip to content

Commit e8e7914

Browse files
committed
Git Repository check for bot update
Adds a check mechanism for the `?update` command and the autoupdate task to ensure the bot has been installed via git before trying to update it.
1 parent c26af97 commit e8e7914

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

bot.py

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

797+
def check_local_git(self) -> bool:
798+
"""
799+
Checks if the bot is installed via git.
800+
"""
801+
valid_local_git = False
802+
git_folder_path = os.path.join(".git")
803+
804+
# Check if the .git folder exists and is a directory
805+
if os.path.exists(git_folder_path) and os.path.isdir(git_folder_path):
806+
required_files = ["config", "HEAD"]
807+
required_dirs = ["refs", "objects"]
808+
809+
# Verify required files exist
810+
for file in required_files:
811+
if not os.path.isfile(os.path.join(git_folder_path, file)):
812+
return valid_local_git
813+
814+
# Verify required directories exist
815+
for directory in required_dirs:
816+
if not os.path.isdir(os.path.join(git_folder_path, directory)):
817+
return valid_local_git
818+
819+
# If all checks pass, set valid_local_git to True
820+
valid_local_git = True
821+
822+
return valid_local_git
823+
797824
async def _process_blocked(self, message):
798825
_, blocked_emoji = await self.retrieve_emoji()
799826
if await self.is_blocked(message.author, channel=message.channel, send_message=True):
@@ -2160,6 +2187,12 @@ async def before_autoupdate(self):
21602187
self.autoupdate.cancel()
21612188
return
21622189

2190+
if not self.check_local_git():
2191+
logger.warning("Bot not installed via git.")
2192+
logger.warning("Autoupdates disabled.")
2193+
self.autoupdate.cancel()
2194+
return
2195+
21632196
@tasks.loop(hours=1, reconnect=False)
21642197
async def log_expiry(self):
21652198
log_expire_after = self.config.get("log_expiration")

cogs/utility.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,11 +2134,7 @@ async def update(self, ctx, *, flag: str = ""):
21342134
data = await self.bot.api.get_user_info()
21352135
if data:
21362136
user = data["user"]
2137-
embed.set_author(
2138-
name=user["username"],
2139-
icon_url=user["avatar_url"] if user["avatar_url"] else None,
2140-
url=user["url"],
2141-
)
2137+
embed.set_author(name=user["username"], icon_url=user["avatar_url"], url=user["url"])
21422138
await ctx.send(embed=embed)
21432139
else:
21442140
error = None
@@ -2177,7 +2173,7 @@ async def update(self, ctx, *, flag: str = ""):
21772173

21782174
embed.set_author(
21792175
name=user["username"] + " - Updating bot",
2180-
icon_url=user["avatar_url"] if user["avatar_url"] else None,
2176+
icon_url=user["avatar_url"],
21812177
url=user["url"],
21822178
)
21832179

@@ -2195,13 +2191,18 @@ async def update(self, ctx, *, flag: str = ""):
21952191
color=self.bot.main_color,
21962192
)
21972193
embed.set_footer(text="Force update")
2198-
embed.set_author(
2199-
name=user["username"],
2200-
icon_url=user["avatar_url"] if user["avatar_url"] else None,
2201-
url=user["url"],
2202-
)
2194+
embed.set_author(name=user["username"], icon_url=user["avatar_url"], url=user["url"])
22032195
await ctx.send(embed=embed)
22042196
else:
2197+
if self.bot.check_local_git() is False:
2198+
embed = discord.Embed(
2199+
title="Update Command Unavailable",
2200+
description="The bot cannot be updated due to not being installed via a git."
2201+
"You need to manually update the bot according to your hosting method."
2202+
"If you face any issues please don´t hesitate to contact modmail support.",
2203+
color=discord.Color.red(),
2204+
)
2205+
return await ctx.send(embed=embed)
22052206
command = "git pull"
22062207
proc = await asyncio.create_subprocess_shell(
22072208
command,
@@ -2214,11 +2215,7 @@ async def update(self, ctx, *, flag: str = ""):
22142215
res = res.decode("utf-8").rstrip()
22152216

22162217
if err and not res:
2217-
embed = discord.Embed(
2218-
title="Update failed",
2219-
description=err,
2220-
color=self.bot.error_color,
2221-
)
2218+
embed = discord.Embed(title="Update failed", description=err, color=self.bot.error_color)
22222219
await ctx.send(embed=embed)
22232220

22242221
elif res != "Already up to date.":

0 commit comments

Comments
 (0)