Skip to content

Commit

Permalink
codebase: misc fixes and suppression of NotFound in EmbedPages
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Przybyszewski committed Aug 19, 2022
1 parent 2805fa1 commit d8fa388
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 46 deletions.
44 changes: 11 additions & 33 deletions lib/api/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,60 +88,45 @@ async def getitem(self, resource: str, default: Any = None) -> Any:
@github_cached
@validate_github_name('user')
async def get_user_repos(self, user: GitHubUser) -> Optional[list[dict]]:
return list(r for r in await self.getitem(f'/users/{user}/repos') if r['private'] is False)
return list(r for r in await self.getitem(f'/users/{user}/repos', []) if r['private'] is False)

@github_cached
@validate_github_name('org')
async def get_org(self, org: GitHubOrganization) -> Optional[dict]:
try:
return await self.gh.getitem(f'/orgs/{org}')
except BadRequest:
return None
return await self.getitem(f'/orgs/{org}')

@github_cached
@validate_github_name('org', default=[])
async def get_org_repos(self, org: GitHubOrganization) -> list[dict]:
try:
res = list(r for r in await self.gh.getitem(f'/orgs/{org}/repos') if r['private'] is False)
return res
except BadRequest:
return []
return list(r for r in await self.getitem(f'/orgs/{org}/repos', []) if r['private'] is False)

@normalize_repository
async def get_repo_files(self, repo: GitHubRepository) -> list[dict]:
if repo.count('/') != 1:
return []
try:
return await self.gh.getitem(f'/repos/{repo}/contents')
except BadRequest:
return []
return await self.getitem(f'/repos/{repo}/contents', [])

@normalize_repository
async def get_tree_file(self, repo: GitHubRepository, path: str) -> dict | list | None:
if repo.count('/') != 1:
return None
if path[0] == '/':
path = path[1:]
try:
return await self.gh.getitem(f'/repos/{repo}/contents/{path}')
except BadRequest:
return None
return await self.getitem(f'/repos/{repo}/contents/{path}')

@github_cached
@validate_github_name('user', default=[])
async def get_user_orgs(self, user: GitHubUser) -> list[dict]:
try:
return list(await self.gh.getitem(f'/users/{user}/orgs'))
except BadRequest:
return []
return await self.getitem(f'/users/{user}/orgs', [])

@github_cached
@validate_github_name('org', default=[])
async def get_org_members(self, org: GitHubOrganization) -> list[dict]:
try:
return list(await self.gh.getitem(f'/orgs/{org}/public_members'))
except BadRequest:
return []
return await self.getitem(f'/orgs/{org}/public_members', [])

@github_cached
async def get_gist(self, gist_id: str) -> Optional[dict]:
return await self.getitem(f'/gists/{gist_id}')

@github_cached
@validate_github_name('user')
Expand All @@ -153,13 +138,6 @@ async def get_user_gists(self, user: GitHubUser):

return data['user']

@github_cached
async def get_gist(self, gist_id: str) -> Optional[dict]:
try:
return dict(await self.gh.getitem(f'/gists/{gist_id}'))
except BadRequest:
return None

@normalize_repository
async def get_latest_commit(self, repo: GitHubRepository) -> Optional[dict] | Literal[False]:
split: list = repo.split('/')
Expand Down
26 changes: 13 additions & 13 deletions lib/structs/discord/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,17 @@ async def start(self, ctx: 'GitBotContext') -> None:
:param ctx: The context to start the paginator in.
"""
self._ensure_perms(ctx.channel)
self.start_time = time()
self.context: GitBotContext = ctx
self._edit_embed_footer(self.pages[self.current_page])
message: discord.Message = await ctx.send(embed=self.pages[self.current_page])
for embed in self.pages[1:]:
self._edit_embed_footer(embed)
self._set_initial_message_attrs(message)
await self._add_controls()
await self.__loop()
with contextlib.suppress(discord.errors.NotFound):
self._ensure_perms(ctx.channel)
self.start_time = time()
self.context: GitBotContext = ctx
self._edit_embed_footer(self.pages[self.current_page])
message: discord.Message = await ctx.send(embed=self.pages[self.current_page])
for embed in self.pages[1:]:
self._edit_embed_footer(embed)
self._set_initial_message_attrs(message)
await self._add_controls()
await self.__loop()

async def edit(self, state: GitBotCommandState | int) -> None:
"""
Expand Down Expand Up @@ -181,9 +182,8 @@ def _set_initial_message_attrs(self, message: discord.Message):

async def _add_controls(self):
if self.message:
with contextlib.suppress(discord.errors.NotFound):
for control in EmbedPagesControl:
await self.message.add_reaction(control.value) # noqa
for control in EmbedPagesControl:
await self.message.add_reaction(control.value) # noqa

async def __loop(self):
while not self.should_die:
Expand Down

0 comments on commit d8fa388

Please sign in to comment.