diff --git a/components/logs.py b/components/logs.py index 7f08c12..fee15c4 100644 --- a/components/logs.py +++ b/components/logs.py @@ -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 @@ -241,9 +242,11 @@ 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 @@ -251,42 +254,38 @@ async def EventMirror( 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}") diff --git a/config_example.json b/config_example.json index fb7e68a..dcbae2d 100644 --- a/config_example.json +++ b/config_example.json @@ -11,8 +11,7 @@ }, "users": { "owner": 1234567890, - "bot": 1234567890, - "zeppelin": 1234567890 + "bot": 1234567890 }, "roles": { "limit": true, diff --git a/poetry.lock b/poetry.lock index 12620fb..bdb9d4f 100644 --- a/poetry.lock +++ b/poetry.lock @@ -266,29 +266,29 @@ tests-no-zope = ["cloudpickle", "hypothesis", "mypy (>=1.1.1)", "pympler", "pyte [[package]] name = "black" -version = "23.10.0" +version = "23.10.1" description = "The uncompromising code formatter." optional = false python-versions = ">=3.8" files = [ - {file = "black-23.10.0-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:f8dc7d50d94063cdfd13c82368afd8588bac4ce360e4224ac399e769d6704e98"}, - {file = "black-23.10.0-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:f20ff03f3fdd2fd4460b4f631663813e57dc277e37fb216463f3b907aa5a9bdd"}, - {file = "black-23.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d3d9129ce05b0829730323bdcb00f928a448a124af5acf90aa94d9aba6969604"}, - {file = "black-23.10.0-cp310-cp310-win_amd64.whl", hash = "sha256:960c21555be135c4b37b7018d63d6248bdae8514e5c55b71e994ad37407f45b8"}, - {file = "black-23.10.0-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:30b78ac9b54cf87bcb9910ee3d499d2bc893afd52495066c49d9ee6b21eee06e"}, - {file = "black-23.10.0-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:0e232f24a337fed7a82c1185ae46c56c4a6167fb0fe37411b43e876892c76699"}, - {file = "black-23.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:31946ec6f9c54ed7ba431c38bc81d758970dd734b96b8e8c2b17a367d7908171"}, - {file = "black-23.10.0-cp311-cp311-win_amd64.whl", hash = "sha256:c870bee76ad5f7a5ea7bd01dc646028d05568d33b0b09b7ecfc8ec0da3f3f39c"}, - {file = "black-23.10.0-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:6901631b937acbee93c75537e74f69463adaf34379a04eef32425b88aca88a23"}, - {file = "black-23.10.0-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:481167c60cd3e6b1cb8ef2aac0f76165843a374346aeeaa9d86765fe0dd0318b"}, - {file = "black-23.10.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f74892b4b836e5162aa0452393112a574dac85e13902c57dfbaaf388e4eda37c"}, - {file = "black-23.10.0-cp38-cp38-win_amd64.whl", hash = "sha256:47c4510f70ec2e8f9135ba490811c071419c115e46f143e4dce2ac45afdcf4c9"}, - {file = "black-23.10.0-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:76baba9281e5e5b230c9b7f83a96daf67a95e919c2dfc240d9e6295eab7b9204"}, - {file = "black-23.10.0-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:a3c2ddb35f71976a4cfeca558848c2f2f89abc86b06e8dd89b5a65c1e6c0f22a"}, - {file = "black-23.10.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:db451a3363b1e765c172c3fd86213a4ce63fb8524c938ebd82919bf2a6e28c6a"}, - {file = "black-23.10.0-cp39-cp39-win_amd64.whl", hash = "sha256:7fb5fc36bb65160df21498d5a3dd330af8b6401be3f25af60c6ebfe23753f747"}, - {file = "black-23.10.0-py3-none-any.whl", hash = "sha256:e223b731a0e025f8ef427dd79d8cd69c167da807f5710add30cdf131f13dd62e"}, - {file = "black-23.10.0.tar.gz", hash = "sha256:31b9f87b277a68d0e99d2905edae08807c007973eaa609da5f0c62def6b7c0bd"}, + {file = "black-23.10.1-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:ec3f8e6234c4e46ff9e16d9ae96f4ef69fa328bb4ad08198c8cee45bb1f08c69"}, + {file = "black-23.10.1-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:1b917a2aa020ca600483a7b340c165970b26e9029067f019e3755b56e8dd5916"}, + {file = "black-23.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9c74de4c77b849e6359c6f01987e94873c707098322b91490d24296f66d067dc"}, + {file = "black-23.10.1-cp310-cp310-win_amd64.whl", hash = "sha256:7b4d10b0f016616a0d93d24a448100adf1699712fb7a4efd0e2c32bbb219b173"}, + {file = "black-23.10.1-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:b15b75fc53a2fbcac8a87d3e20f69874d161beef13954747e053bca7a1ce53a0"}, + {file = "black-23.10.1-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:e293e4c2f4a992b980032bbd62df07c1bcff82d6964d6c9496f2cd726e246ace"}, + {file = "black-23.10.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7d56124b7a61d092cb52cce34182a5280e160e6aff3137172a68c2c2c4b76bcb"}, + {file = "black-23.10.1-cp311-cp311-win_amd64.whl", hash = "sha256:3f157a8945a7b2d424da3335f7ace89c14a3b0625e6593d21139c2d8214d55ce"}, + {file = "black-23.10.1-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:cfcce6f0a384d0da692119f2d72d79ed07c7159879d0bb1bb32d2e443382bf3a"}, + {file = "black-23.10.1-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:33d40f5b06be80c1bbce17b173cda17994fbad096ce60eb22054da021bf933d1"}, + {file = "black-23.10.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:840015166dbdfbc47992871325799fd2dc0dcf9395e401ada6d88fe11498abad"}, + {file = "black-23.10.1-cp38-cp38-win_amd64.whl", hash = "sha256:037e9b4664cafda5f025a1728c50a9e9aedb99a759c89f760bd83730e76ba884"}, + {file = "black-23.10.1-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:7cb5936e686e782fddb1c73f8aa6f459e1ad38a6a7b0e54b403f1f05a1507ee9"}, + {file = "black-23.10.1-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:7670242e90dc129c539e9ca17665e39a146a761e681805c54fbd86015c7c84f7"}, + {file = "black-23.10.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5ed45ac9a613fb52dad3b61c8dea2ec9510bf3108d4db88422bacc7d1ba1243d"}, + {file = "black-23.10.1-cp39-cp39-win_amd64.whl", hash = "sha256:6d23d7822140e3fef190734216cefb262521789367fbdc0b3f22af6744058982"}, + {file = "black-23.10.1-py3-none-any.whl", hash = "sha256:d431e6739f727bb2e0495df64a6c7a5310758e87505f5f8cde9ff6c0f2d7e4fe"}, + {file = "black-23.10.1.tar.gz", hash = "sha256:1f8ce316753428ff68749c65a5f7844631aa18c8679dfd3ca9dc1a289979c258"}, ] [package.dependencies] @@ -487,6 +487,22 @@ requests = ">=2.28.1,<3.0.0" [package.extras] async = ["httpx (>=0.23.0,<0.24.0)"] +[[package]] +name = "filelock" +version = "3.13.0" +description = "A platform independent file lock." +optional = false +python-versions = ">=3.8" +files = [ + {file = "filelock-3.13.0-py3-none-any.whl", hash = "sha256:a552f4fde758f4eab33191e9548f671970f8b06d436d31388c9aa1e5861a710f"}, + {file = "filelock-3.13.0.tar.gz", hash = "sha256:63c6052c82a1a24c873a549fbd39a26982e8f35a3016da231ead11a5be9dad44"}, +] + +[package.extras] +docs = ["furo (>=2023.9.10)", "sphinx (>=7.2.6)", "sphinx-autodoc-typehints (>=1.24)"] +testing = ["covdefaults (>=2.3)", "coverage (>=7.3.2)", "diff-cover (>=8)", "pytest (>=7.4.3)", "pytest-cov (>=4.1)", "pytest-mock (>=3.12)", "pytest-timeout (>=2.2)"] +typing = ["typing-extensions (>=4.8)"] + [[package]] name = "frozenlist" version = "1.4.0" @@ -962,6 +978,34 @@ dev = ["black", "flake8", "pytest (>=2.7.3)"] lint = ["black", "flake8"] test = ["pytest (>=2.7.3)"] +[[package]] +name = "uritools" +version = "4.0.2" +description = "URI parsing, classification and composition" +optional = false +python-versions = ">=3.7" +files = [ + {file = "uritools-4.0.2-py3-none-any.whl", hash = "sha256:607b15eae1e7b69a120f463a7d98f91a56671e1ab92aae13f8e1f25c017fe60e"}, + {file = "uritools-4.0.2.tar.gz", hash = "sha256:04df2b787d0eb76200e8319382a03562fbfe4741fd66c15506b08d3b8211d573"}, +] + +[[package]] +name = "urlextract" +version = "1.8.0" +description = "Collects and extracts URLs from given text." +optional = false +python-versions = "*" +files = [ + {file = "urlextract-1.8.0-py3-none-any.whl", hash = "sha256:98b38aca4a555116e8b46e5a134b9e4e54e351b8e37169d2857730d1d0ce42c7"}, + {file = "urlextract-1.8.0.tar.gz", hash = "sha256:3573f6b812814efe06ca46e91e82d984edaa3cd07daaaaa296a467ad9881a037"}, +] + +[package.dependencies] +filelock = "*" +idna = "*" +platformdirs = "*" +uritools = "*" + [[package]] name = "urllib3" version = "2.0.7" @@ -1127,4 +1171,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = ">=3.11,<3.12" -content-hash = "26d10043a37659098dda3182a30fce528fdff641e91df939de2a2cef185b9ba2" +content-hash = "be12c3ff89e15828e9fdd25fce2397988b2e8f3291a14b276d37e3937e318906" diff --git a/pyproject.toml b/pyproject.toml index e51d93d..0b7cb5e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"]