-
Notifications
You must be signed in to change notification settings - Fork 0
/
canister.py
150 lines (123 loc) · 6.42 KB
/
canister.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import re
import discord
from data.services import guild_service
from discord import app_commands
from discord.ext import commands
from utils import GIRContext, canister_search_package, cfg, transform_context
from utils.fetchers import canister_fetch_repos
from utils.framework import gatekeeper, whisper_in_general
from utils.views import TweakDropdown, default_repos, repo_autocomplete
class Canister(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_message(self, message):
if message.guild is None:
return
author = message.guild.get_member(message.author.id)
if author is None:
return
if not gatekeeper.has(message.guild, author, 5) and message.channel.id == guild_service.get_guild().channel_general:
return
pattern = re.compile(
r".*?(?<!\[)+\[\[((?!\s+)([\w+\ \&\+\-\<\>\#\:\;\%\(\)]){2,})\]\](?!\])+.*")
if not pattern.match(message.content):
return
matches = pattern.findall(message.content)
if not matches:
return
search_term = matches[0][0].replace('[[', '').replace(']]', '')
if not search_term:
return
ctx = await self.bot.get_context(message)
async with ctx.typing():
result = list(await canister_search_package(search_term))
if not result:
embed = discord.Embed(
title=":(\nI couldn't find that package", color=discord.Color.red())
embed.description = f"Try broadening your search query."
await ctx.send(embed=embed, delete_after=8)
return
view = discord.ui.View(timeout=30)
td = TweakDropdown(author, result, interaction=False,
should_whisper=False)
view.add_item(td)
td.refresh_view(result[0])
view.on_timeout = td.on_timeout
message = await ctx.send(embed=await td.format_tweak_page(result[0]), view=view)
new_ctx = await self.bot.get_context(message)
td.start(new_ctx)
@app_commands.guilds(cfg.guild_id)
@app_commands.command(description="Search for a jailbreak tweak or package")
@app_commands.describe(query="Name of package to search for")
@transform_context
async def package(self, ctx: GIRContext, query: str) -> None:
if len(query) < 2:
raise commands.BadArgument("Please enter a longer query.")
should_whisper = False
if not gatekeeper.has(ctx.guild, ctx.author, 5) and ctx.channel.id == guild_service.get_guild().channel_general:
should_whisper = True
await ctx.defer(ephemeral=should_whisper)
result = list(await canister_search_package(query))
if not result:
embed = discord.Embed(
title=":(\nI couldn't find that package", color=discord.Color.red())
embed.description = f"Try broadening your search query."
await ctx.respond(embed=embed)
return
view = discord.ui.View(timeout=30)
td = TweakDropdown(ctx.author, result, interaction=True,
should_whisper=should_whisper)
view.on_timeout = td.on_timeout
view.add_item(td)
td.refresh_view(result[0])
await ctx.respond(embed=await td.format_tweak_page(result[0]), view=view)
td.start(ctx)
@app_commands.guilds(cfg.guild_id)
@app_commands.command(description="Search for a tweak repository")
@app_commands.describe(query="Name of repository to search for")
@app_commands.autocomplete(query=repo_autocomplete)
@transform_context
@whisper_in_general
async def repo(self, ctx: GIRContext, query: str) -> None:
repos = await canister_fetch_repos()
matches = [repo for repo in repos if repo.get("slug") and repo.get(
"slug") is not None and repo.get("slug").lower() == query.lower()]
if not matches:
await ctx.send_error("That repository isn't registered with Canister's database.")
return
repo_data = matches[0]
for repo in default_repos:
if repo in repo_data.get('uri'):
ctx.repo = None
break
embed = discord.Embed(title=repo_data.get(
'name'), color=discord.Color.blue())
embed.add_field(name="URL", value=repo_data.get('uri'), inline=True)
embed.set_thumbnail(url=f'{repo_data.get("uri")}/CydiaIcon.png')
embed.set_footer(text="Powered by Canister")
this_repo = repo_data.get("uri")
view = discord.ui.View()
for repo in default_repos:
if repo in this_repo:
[view.add_item(item) for item in [
discord.ui.Button(label='Cannot add default repo', emoji="<:Sileo:959128883498729482>",
url=f'https://repos.slim.rocks/repo/?repoUrl={this_repo}&manager=sileo', disabled=True, style=discord.ButtonStyle.url, row=1),
discord.ui.Button(label='Cannot add default repo', emoji="<:Zeeb:959129860603801630>",
url=f'https://repos.slim.rocks/repo/?repoUrl={this_repo}&manager=zebra', disabled=True, style=discord.ButtonStyle.url, row=1),
discord.ui.Button(label='Cannot add default repo', emoji="<:Add:947354227171262534>",
url=f'https://repos.slim.rocks/repo/?repoUrl={this_repo}', style=discord.ButtonStyle.url, disabled=True, row=1)
]]
break
if not view.children:
[view.add_item(item) for item in [
discord.ui.Button(label='Add Repo to Sileo', emoji="<:Sileo:959128883498729482>",
url=f'https://repos.slim.rocks/repo/?repoUrl={this_repo}&manager=sileo', style=discord.ButtonStyle.url, row=1),
discord.ui.Button(label='Add Repo to Zebra', emoji="<:Zeeb:959129860603801630>",
url=f'https://repos.slim.rocks/repo/?repoUrl={this_repo}&manager=zebra', style=discord.ButtonStyle.url, row=1),
discord.ui.Button(label='Other Package Managers', emoji="<:Add:947354227171262534>",
url=f'https://repos.slim.rocks/repo/?repoUrl={this_repo}', style=discord.ButtonStyle.url, row=1)
]]
await ctx.respond(embed=embed, ephemeral=ctx.whisper, view=view)
async def setup(bot):
await bot.add_cog(Canister(bot))