Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix utils/views/menus/canister.py for important repos, add priority #47

Open
acattoXD opened this issue Dec 5, 2024 · 0 comments
Open

Comments

@acattoXD
Copy link

acattoXD commented Dec 5, 2024

hi
i'm writing this cuz the current gir code for canister is broken

here's the class you want probably would want to replace it with
i'm too lazy to make a push request so i'll just put it here

i also added priority because chariz is much more likely to be up to date than bigboss repo
maybe change priority too while ur at it cuz idk good and bad repos

class TweakDropdown(discord.ui.Select):
    def __init__(self, author, entries, interaction, should_whisper):
        self.repo_icons = {
            "https://repo.chariz.com": {"icon": "<:colored_sileo_tweak_icon_chariz:957470675411537980>", "priority": 1},
            "http://apt.thebigboss.org/repofiles/cydia": {"icon": "<:colored_sileo_tweak_icon_bigboss:957470630956138496>", "priority": 2},
            "https://repo.chimera.sh": {"icon": "<:colored_sileo_tweak_icon_chimera:957470798602440714>", "priority": 3},
            "https://repo.dynastic.co": {"icon": "<:colored_sileo_tweak_icon_dynasti:957470833952043038>", "priority": 4},
            "https://apt.bingner.com": {"icon": "<:colored_sileo_tweak_icon_elu:957471017578684496>", "priority": 5},
            "https://havoc.app": {"icon": "<:colored_sileo_tweak_icon_havoc:957471075942428723>", "priority": 6},
            "https://repo.theodyssey.dev": {"icon": "<:colored_sileo_tweak_icon_odyssey:957471133844799518>", "priority": 7},
            "https://repo.packix.com": {"icon": "<:colored_sileo_tweak_icon_packix:957471186638479470>", "priority": 8},
            "https://apt.procurs.us": {"icon": "<:colored_sileo_tweak_icon_procurs:957471236718460959>", "priority": 9},
            "https://repo.twickd.com": {"icon": "<:colored_sileo_tweak_icon_twickd:957471321351135233>", "priority": 10},
        }
        self.author = author
        self.interaction = interaction
        self.raw_entries = sorted(entries, key=self._sort_by_repo_priority)
        self.should_whisper = should_whisper
        entries = self.raw_entries[:24]

        self.current_entry = entries[0]
        self.entries = {}
        options = []
        seen_packages = []
        
        for index, option in enumerate(entries):
            if option.get("package") in seen_packages:
                continue
                
            value = f"{index}_{option.get('uuid', str(random.randint(1000, 9999)))}"
            
            repo_uri = option.get('repository', {}).get('uri')
            emoji = self.repo_icons.get(repo_uri, {}).get('icon', "<:sileo_tweak_icon:957456295898779678>")
                
            options.append(
                discord.SelectOption(
                    label=(option.get("name") or option.get('package'))[:100] or "No title",
                    description=f"{option.get('author').split('<')[0] if option.get('author') is not None else option.get('maintainer').split('<')[0]}{option.get('repository').get('name')}"[:100],
                    emoji=emoji,
                    value=value
                )
            )
            self.entries[value] = option
            seen_packages.append(option.get("package"))
            
        if len(self.raw_entries) > 24:
            options.append(discord.SelectOption(
                label=f"View {len(self.raw_entries) - 24} more results...",
                value="view_more"
            ))
        
        if not options:
            options.append(discord.SelectOption(
                label="No results",
                value="none"
            ))
            
        super().__init__(placeholder='Pick a tweak to view...',
                        min_values=1, max_values=1, options=options)

    def start(self, ctx):
        self.ctx = ctx

    def _sort_by_repo_priority(self, entry):
        repo_uri = entry.get('repository', {}).get('uri')
        repo_data = self.repo_icons.get(repo_uri, {'priority': 999})
        return repo_data['priority']

    async def callback(self, interaction):
        if interaction.user != self.author and not gatekeeper.has(interaction.guild, interaction.user, 4):
            return

        self.ctx.interaction = interaction

        if self.values[0] == "view_more":
            await canister(self.ctx, self.should_whisper, self.raw_entries)
            self._view.stop()
            return

        selected_value = self.entries.get(self.values[0])
        if selected_value is None:
            return

        self.refresh_view(selected_value)
        await self.ctx.interaction.response.edit_message(embed=await self.format_tweak_page(selected_value), view=self._view)

    async def on_timeout(self):
        self.disabled = True
        self.placeholder = "Timed out"
        if isinstance(self.ctx, GIRContext):
            await self.ctx.edit(view=self._view)
        else:
            await self.ctx.message.edit(view=self._view)

    async def format_tweak_page(self, entry):
        embed = tweak_embed_format(entry)
        return embed

    def generate_buttons(self, entry):
        repo = entry.get('repository').get('uri')
        depiction = entry.get('depiction')

        for r in default_repos:
            if r in entry.get('repository').get('uri'):
                repo = None
                break

        if repo is not None:
            extra_buttons = [
                discord.ui.Button(
                    label='Add Repo to Sileo',
                    emoji="<:sileo:679466569407004684>",
                    url=f'https://repos.slim.rocks/repo/?repoUrl={repo}&manager=sileo',
                    style=discord.ButtonStyle.url
                ),
                discord.ui.Button(
                    label='Add Repo to Zebra',
                    emoji="<:Zeeb:959129860603801630>",
                    url=f'https://repos.slim.rocks/repo/?repoUrl={repo}&manager=zebra',
                    style=discord.ButtonStyle.url
                ),
                discord.ui.Button(
                    label='Other Package Managers',
                    emoji="<:Add:947354227171262534>",
                    url=f'https://repos.slim.rocks/repo/?repoUrl={repo}',
                    style=discord.ButtonStyle.url
                )
            ]
        else:
            extra_buttons = [
                discord.ui.Button(label='Cannot add default repo', emoji="<:sileo:679466569407004684>",
                                  url=f'https://repos.slim.rocks/repo/?repoUrl={repo}&manager=sileo', disabled=True, style=discord.ButtonStyle.url),
                discord.ui.Button(label='Cannot add default repo', emoji="<:Zeeb:959129860603801630>",
                                  url=f'https://repos.slim.rocks/repo/?repoUrl={repo}&manager=zebra', disabled=True, style=discord.ButtonStyle.url),
                discord.ui.Button(label='Cannot add default repo', emoji="<:Add:947354227171262534>",
                                  url=f'https://repos.slim.rocks/repo/?repoUrl={repo}', disabled=True, style=discord.ButtonStyle.url)
            ]
        if depiction is not None:
            extra_buttons.insert(0,
                                 discord.ui.Button(label='View Depiction', emoji="<:Depiction:947358756033949786>",
                                                   url=depiction, style=discord.ButtonStyle.url),
                                 )
        return extra_buttons

    def refresh_view(self, entry):
        extra_buttons = self.generate_buttons(entry)
        self._view.clear_items()

        if len(self.raw_entries) > 1:
            self._view.add_item(self)

        for button in extra_buttons:
            self._view.add_item(button)```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant