Finish pobierz command

This commit is contained in:
JelNiSław 2022-06-06 00:10:12 +02:00
parent 87a8aa5d96
commit 6f956629fa
No known key found for this signature in database
GPG key ID: EA41571A0A88E97E
6 changed files with 67 additions and 37 deletions

View file

@ -48,7 +48,3 @@ class Wulkabot(commands.Bot):
async def load_extensions(self, package: types.ModuleType) -> None:
for extension in self.find_extensions(package):
await self.load_extension(extension)
async def reload_extensions(self) -> None:
for extension in set(self.extensions):
await self.reload_extension(extension)

View file

@ -30,17 +30,6 @@ class Development(commands.Cog):
f"Synced **{len(commands)} {destination}** commands\n{commands_str}"
)
@app_commands.command()
async def reload(self, interaction: discord.Interaction):
"""Przeładowuje komendy bota"""
# temporary check, needs refactoring
if not await self.bot.is_owner(interaction.user):
await interaction.response.send_message("nie", ephemeral=True)
return
await self.bot.reload_extensions()
await interaction.response.send_message("Reloaded successfuly")
async def setup(bot: bot.Wulkabot):
await bot.add_cog(Development(bot))

View file

@ -7,12 +7,11 @@ from discord.ext import commands
from .. import bot
from ..utils import github
from ..utils.constants import REPO
from ..utils.views import DeleteButton
Repo = tuple[str, str]
DEFAULT_REPO: Repo = ("wulkanowy", "wulkanowy")
def parse_repo(text: str, *, default_owner: str | None = None) -> Repo | None:
"""
@ -157,20 +156,20 @@ class GitHub(commands.Cog):
try:
topic = message.channel.topic # type: ignore
except AttributeError:
channel_repo = DEFAULT_REPO
channel_repo = REPO
else:
if topic is not None:
channel_repo = find_repo_in_channel_topic(topic)
if channel_repo is None:
channel_repo = DEFAULT_REPO
channel_repo = REPO
else:
channel_repo = DEFAULT_REPO
channel_repo = REPO
for word in words:
match = parse_issue(word, default_owner=channel_repo[0], default_repo=channel_repo[1])
if match is not None:
repo, issue_number = match
repo = repo or DEFAULT_REPO
repo = repo or REPO
try:
issue = await self.github.fetch_issue(*repo, issue_number)
except aiohttp.ClientResponseError:

View file

@ -6,8 +6,19 @@ from discord.ext import commands
from .. import bot
from ..utils import github, wulkanowy_manager
from ..utils.constants import ACCENT_COLOR, BUILDS_CHANNEL_ID, REPO
from ..utils.wulkanowy_manager import WulkanowyBuild, WulkanowyManagerException
OTHER_DOWNLOADS = " | ".join(
(
"[Google Play](https://play.google.com/store/apps/details?id=io.github.wulkanowy)",
"[GitHub](https://github.com/wulkanowy/wulkanowy/releases)",
"[F-Droid](https://f-droid.org/en/packages/io.github.wulkanowy)",
"[AppGallery](https://appgallery.huawei.com/app/C101440411)",
f"<#{BUILDS_CHANNEL_ID}>",
)
)
class Wulkanowy(commands.Cog):
def __init__(self, bot: bot.Wulkabot) -> None:
@ -22,20 +33,44 @@ class Wulkanowy(commands.Cog):
@app_commands.command()
async def pobierz(self, interaction: discord.Interaction):
branches = await self.github.fetch_branches("wulkanowy", "wulkanowy")
await interaction.response.defer(thinking=True)
release = await self.github.fetch_latest_release(*REPO)
download_urls = []
for asset in release["assets"]:
name = asset["name"]
url = asset["browser_download_url"]
download_urls.append(f"[{name}]({url})")
download_urls = "\n".join(download_urls)
pulls = await self.github.fetch_open_pulls(*REPO)
branches = ["develop"]
branches.extend((pull["head"]["ref"] for pull in pulls))
builds: list[WulkanowyBuild | WulkanowyManagerException] = await asyncio.gather(
*(map(self.wulkanowy_manager.fetch_branch_build, branches)), return_exceptions=True
*map(self.wulkanowy_manager.fetch_branch_build, branches), return_exceptions=True
)
text = []
for branch, build in zip(branches, builds, strict=True):
if isinstance(build, WulkanowyBuild):
text.append(f"{branch}: [pobierz]({build.download_url})")
else:
text.append(f"{branch}: brak")
embed = discord.Embed(
title="Pobierz najnowsze wersje testowe!", description="\n".join(text)
lines = "\n".join(
(
f"`{build.build_number}` [{branch}]({build.download_url})"
for branch, build in zip(branches, builds, strict=True)
if isinstance(build, WulkanowyBuild)
)
)
await interaction.response.send_message(embed=embed)
text = "\n".join(
(
f'**Najnowsza wersja {release["name"]}**',
download_urls,
"",
"**Wersje testowe**",
lines,
"",
"**Inne źródła**",
OTHER_DOWNLOADS,
)
)
embed = discord.Embed(title="Pobierz Wulkanowego!", description=text, color=ACCENT_COLOR)
await interaction.followup.send(embed=embed)
async def setup(bot: bot.Wulkabot):

View file

@ -0,0 +1,4 @@
REPO = ("wulkanowy", "wulkanowy")
ACCENT_COLOR = 0xD32F2F
DEFAULT_BRANCH = "develop"
BUILDS_CHANNEL_ID = 983106478426693653

View file

@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Literal
import aiohttp
@ -14,16 +14,23 @@ class GitHub:
response.raise_for_status()
return await response.json()
async def fetch_branches(self, owner: str, repo: str) -> list[str]:
response = await self._http.get(f"/repos/{owner}/{repo}/branches")
async def fetch_open_pulls(self, owner: str, repo: str):
response = await self._http.get(
f"/repos/{owner}/{repo}/pulls",
params={"state": "open", "sort": "updated", "direction": "desc", "per_page": 25},
)
response.raise_for_status()
branches = await response.json()
return [branch["name"] for branch in branches]
return await response.json()
async def fetch_issue(self, owner: str, repo: str, issue_number: int) -> dict[str, Any]:
response = await self._http.get(f"/repos/{owner}/{repo}/issues/{issue_number}")
response.raise_for_status()
return await response.json()
async def fetch_latest_release(self, owner: str, repo: str):
response = await self._http.get(f"/repos/{owner}/{repo}/releases/latest")
response.raise_for_status()
return await response.json()
async def close(self):
await self._http.close()