Skip to content

Commit

Permalink
Include userIds in log archive mirrors
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanC committed Feb 12, 2024
1 parent 55b7e82 commit 758ac88
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
17 changes: 16 additions & 1 deletion components/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,23 @@ async def EventMirror(

if data is None:
return

found: list[str] = []

result: str = Responses.Log("mirror", f"Mirror of Zeppelin log archive <{url}>")
for line in data.splitlines():
for find in Utility.FindNumbers(line, 17, 19):
if await Utility.IsValidUser(find, client):
found.append(f"`{find}`")

# Ensure there are no duplicate users
found: list[str] = list(set(found))

result: str = f"Mirror of Zeppelin log archive <{url}>"

if len(found) > 0:
result += f" ({", ".join(found)})"

result = Responses.Log("mirror", result)
filename: str = "archive"

try:
Expand Down
10 changes: 1 addition & 9 deletions components/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,17 +386,9 @@ async def CommandParseUsers(
return

for result in results:
try:
logger.debug(
f"Validated {result} as user {(await ctx.rest.fetch_user(result)).username}"
)
except Exception as e:
if not await Utility.IsValidUser(result, ctx.client):
results.remove(result)

logger.opt(exception=e).debug(
f"{result} is not a user ID, removed from results"
)

if len(results) == 0:
await ctx.respond(
embed=Responses.Warning(
Expand Down
20 changes: 19 additions & 1 deletion helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from hikari import GatewayBot, Member, NotFoundError
from httpx import Response
from loguru import logger
from tanjun import Client

from .responses import Responses

Expand Down Expand Up @@ -37,7 +38,7 @@ async def GET(
try:
return res.json()
except Exception as e:
logger.opt(exception=e).debug("Failed to parse response as JSON")
logger.opt(exception=e).trace("Failed to parse response as JSON")

return res.text

Expand Down Expand Up @@ -109,6 +110,8 @@ def FindNumbers(
if len(entry) > maxLen:
continue

logger.debug(f"Found number {entry} in string {input}")

results.append(int(entry))
except Exception as e:
logger.opt(exception=e).debug("Failed to find numbers in string")
Expand Down Expand Up @@ -154,3 +157,18 @@ async def UserHasRole(
)

return False

async def IsValidUser(userId: int, client: Client) -> bool:
"""
Determine if the provided integer is a valid
Discord user ID.
"""

try:
logger.debug(f"Validated {userId} as user {(await client.rest.fetch_user(userId)).username}")
except Exception as e:
logger.opt(exception=e).debug(f"Invalidated potential user ID {userId}")

return

return True

0 comments on commit 758ac88

Please sign in to comment.