Skip to content

Commit

Permalink
Update regex, add !package command
Browse files Browse the repository at this point in the history
  • Loading branch information
stekc committed Apr 13, 2024
1 parent a991213 commit 24c7a96
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Unofficial Canister/ios.cfw.guide bot for r/Jailbreak
Commands:

- Search for packages by commenting `[[tweak name]]`
- !package [name] `!package snowboard`
- !repo [slug] `!repo chariz`
- !jailbreak, !jb [name] `!jailbreak taurine`

Expand All @@ -11,3 +12,4 @@ Credits:
- [GIRRewrite](https://github.com/DiscordGIR/GIRRewrite)
- [Canister](https://canister.me)
- [ios.cfw.guide](https://ios.cfw.guide)
- [dhinakg](https://github.com/dhinakg)
46 changes: 27 additions & 19 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,29 @@ async def format_package_info(package):


async def process_comment(comment):
pattern = re.compile(
r".*?(?<!\[)+\[\[((?!\s+)([\w+\ \&\+\-\<\>\#\:\;\%\(\)]){2,})\]\](?!\])+.*"
)
command_pattern = r".*!(\w+)(?:\s+(\w+))?$"
try:
if comment.author.name in [os.getenv("USERNAME"), "AutoModerator"]:
return

# Package search
pattern = re.compile(r"(\\?\[){2}(P?.*?)(\\?\]){2}")
if match := pattern.match(comment.body):
query = match.group(2)
if len(query) < 3:
return
print(
f"Package from u/{comment.author.name} matched ({query})\nhttps://reddit.com{comment.permalink}\n"
)

if packages := await get_packages_from_canister(query):
response = "\n".join(
[await format_package_info(package) for package in packages]
)
response += footer
await comment.reply(response)

# Command handling
command_pattern = r".*!(\w+)(?:\s+(\w+))?$"
match = re.search(command_pattern, comment.body.lower().strip())
if match:
command = match.group(1)
Expand Down Expand Up @@ -166,22 +180,16 @@ async def process_comment(comment):
reply_text += f"\n\nType: {jb_type}\n\nCompatible: {compatible}"
reply_text += footer
await comment.reply(reply_text)

# Package search
if match := pattern.match(comment.body):
query = match.group(1)
if len(query) < 3:
return
print(
f"Package from u/{comment.author.name} matched ({query})\nhttps://reddit.com{comment.permalink}\n"
)

if packages := await get_packages_from_canister(query):
response = "\n".join(
[await format_package_info(package) for package in packages]
if command == "package" or command == "tweak":
print(
f"!package {subquery} ran by u/{comment.author.name} \nhttps://reddit.com{comment.permalink}\n"
)
response += footer
await comment.reply(response)
if packages := await get_packages_from_canister(subquery):
response = "\n".join(
[await format_package_info(package) for package in packages]
)
response += footer
await comment.reply(response)

except Exception as e:
print(f"Something went wrong.\n{e}\n")
Expand Down

0 comments on commit 24c7a96

Please sign in to comment.