Skip to content

Commit

Permalink
Fixes for Zeppelin log archive mirroring
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanC committed Oct 29, 2023
1 parent 7923819 commit 15393b9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 53 deletions.
59 changes: 29 additions & 30 deletions components/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from hikari.files import Bytes
from loguru import logger
from tanjun import Client, Component
from urlextract import URLExtract

from helpers import Responses, Utility

Expand Down Expand Up @@ -241,52 +242,50 @@ async def EventMirror(
) -> None:
"""Handler for automatically mirroring Zeppelin log archives."""

if not ctx.author.is_bot:
if int(ctx.channel_id) != config["channels"]["moderation"]:
return
elif int(ctx.author.id) != config["users"]["zeppelin"]:
elif not ctx.author.is_bot:
return
elif int(ctx.author.id) == config["users"]["bot"]:
return
elif not hasattr(ctx.message, "content"):
return
elif ctx.message.content is None:
return

content: str = ctx.message.content.lower()
url: Optional[str] = None
urls: List[str] = []
extractor: URLExtract = URLExtract()

if "api.zeppelin.gg/archives/" not in content:
return
urls = extractor.find_urls(content, True)

logger.trace(content)
logger.trace(urls)

try:
url = content.split("(")[-1].split(")")[0]

if not url.startswith("https://"):
raise Exception(f"expected startswith https://, got {url}")
except Exception as e:
logger.opt(exception=e).debug("Failed to validate Zeppelin log archive URL")
for url in urls:
if not url.startswith("https://api.zeppelin.gg/archives/"):
continue

return
data: Optional[str] = await Utility.GET(url)

data: Optional[str] = await Utility.GET(url)
if data is None:
return

if data is None:
return
result: str = Responses.Log("mirror", f"Mirror of Zeppelin log archive <{url}>")
filename: str = "archive"

result: str = Responses.Log("mirror", f"Mirror of Zeppelin log archive <{url}>")
filename: Optional[str] = "archive"
try:
filename = url.split("/")[-1]
except Exception as e:
logger.opt(exception=e).warning(
f"Failed to determine Zeppelin log archive filename"
)

try:
filename = url.split("/")[-1]
except Exception as e:
logger.opt(exception=e).warning(
f"Failed to determine Zeppelin log archive filename"
await client.rest.create_message(
config["channels"]["moderation"],
result,
attachment=Bytes(data, f"{filename}.txt"),
reply=ctx.message,
)

await client.rest.create_message(
config["channels"]["moderation"],
result,
attachment=Bytes(data, f"{filename}.txt"),
)

logger.success(f"Mirrored Zeppelin log archive {url}")
logger.success(f"Mirrored Zeppelin log archive {url}")
3 changes: 1 addition & 2 deletions config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
},
"users": {
"owner": 1234567890,
"bot": 1234567890,
"zeppelin": 1234567890
"bot": 1234567890
},
"roles": {
"limit": true,
Expand Down
84 changes: 64 additions & 20 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ loguru-discord = "^1.1.0"
hikari-tanjun = "^2.17.1"
httpx = "0.25.0"
asyncpraw = "^7.7.1"
urlextract = "^1.8.0"
uvloop = {version = "^0.19.0", platform = "linux"}

[tool.poetry.dev-dependencies]
pylint = "^3.0.2"
black = "^23.10.0"
black = "^23.10.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down

0 comments on commit 15393b9

Please sign in to comment.