Skip to content

Commit d8fa388

Browse files
author
Paul Przybyszewski
committed
codebase: misc fixes and suppression of NotFound in EmbedPages
1 parent 2805fa1 commit d8fa388

File tree

2 files changed

+24
-46
lines changed

2 files changed

+24
-46
lines changed

lib/api/github.py

+11-33
Original file line numberDiff line numberDiff line change
@@ -88,60 +88,45 @@ async def getitem(self, resource: str, default: Any = None) -> Any:
8888
@github_cached
8989
@validate_github_name('user')
9090
async def get_user_repos(self, user: GitHubUser) -> Optional[list[dict]]:
91-
return list(r for r in await self.getitem(f'/users/{user}/repos') if r['private'] is False)
91+
return list(r for r in await self.getitem(f'/users/{user}/repos', []) if r['private'] is False)
9292

9393
@github_cached
9494
@validate_github_name('org')
9595
async def get_org(self, org: GitHubOrganization) -> Optional[dict]:
96-
try:
97-
return await self.gh.getitem(f'/orgs/{org}')
98-
except BadRequest:
99-
return None
96+
return await self.getitem(f'/orgs/{org}')
10097

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

110103
@normalize_repository
111104
async def get_repo_files(self, repo: GitHubRepository) -> list[dict]:
112105
if repo.count('/') != 1:
113106
return []
114-
try:
115-
return await self.gh.getitem(f'/repos/{repo}/contents')
116-
except BadRequest:
117-
return []
107+
return await self.getitem(f'/repos/{repo}/contents', [])
118108

119109
@normalize_repository
120110
async def get_tree_file(self, repo: GitHubRepository, path: str) -> dict | list | None:
121111
if repo.count('/') != 1:
122112
return None
123113
if path[0] == '/':
124114
path = path[1:]
125-
try:
126-
return await self.gh.getitem(f'/repos/{repo}/contents/{path}')
127-
except BadRequest:
128-
return None
115+
return await self.getitem(f'/repos/{repo}/contents/{path}')
129116

130117
@github_cached
131118
@validate_github_name('user', default=[])
132119
async def get_user_orgs(self, user: GitHubUser) -> list[dict]:
133-
try:
134-
return list(await self.gh.getitem(f'/users/{user}/orgs'))
135-
except BadRequest:
136-
return []
120+
return await self.getitem(f'/users/{user}/orgs', [])
137121

138122
@github_cached
139123
@validate_github_name('org', default=[])
140124
async def get_org_members(self, org: GitHubOrganization) -> list[dict]:
141-
try:
142-
return list(await self.gh.getitem(f'/orgs/{org}/public_members'))
143-
except BadRequest:
144-
return []
125+
return await self.getitem(f'/orgs/{org}/public_members', [])
126+
127+
@github_cached
128+
async def get_gist(self, gist_id: str) -> Optional[dict]:
129+
return await self.getitem(f'/gists/{gist_id}')
145130

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

154139
return data['user']
155140

156-
@github_cached
157-
async def get_gist(self, gist_id: str) -> Optional[dict]:
158-
try:
159-
return dict(await self.gh.getitem(f'/gists/{gist_id}'))
160-
except BadRequest:
161-
return None
162-
163141
@normalize_repository
164142
async def get_latest_commit(self, repo: GitHubRepository) -> Optional[dict] | Literal[False]:
165143
split: list = repo.split('/')

lib/structs/discord/pages.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,17 @@ async def start(self, ctx: 'GitBotContext') -> None:
117117
118118
:param ctx: The context to start the paginator in.
119119
"""
120-
self._ensure_perms(ctx.channel)
121-
self.start_time = time()
122-
self.context: GitBotContext = ctx
123-
self._edit_embed_footer(self.pages[self.current_page])
124-
message: discord.Message = await ctx.send(embed=self.pages[self.current_page])
125-
for embed in self.pages[1:]:
126-
self._edit_embed_footer(embed)
127-
self._set_initial_message_attrs(message)
128-
await self._add_controls()
129-
await self.__loop()
120+
with contextlib.suppress(discord.errors.NotFound):
121+
self._ensure_perms(ctx.channel)
122+
self.start_time = time()
123+
self.context: GitBotContext = ctx
124+
self._edit_embed_footer(self.pages[self.current_page])
125+
message: discord.Message = await ctx.send(embed=self.pages[self.current_page])
126+
for embed in self.pages[1:]:
127+
self._edit_embed_footer(embed)
128+
self._set_initial_message_attrs(message)
129+
await self._add_controls()
130+
await self.__loop()
130131

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

182183
async def _add_controls(self):
183184
if self.message:
184-
with contextlib.suppress(discord.errors.NotFound):
185-
for control in EmbedPagesControl:
186-
await self.message.add_reaction(control.value) # noqa
185+
for control in EmbedPagesControl:
186+
await self.message.add_reaction(control.value) # noqa
187187

188188
async def __loop(self):
189189
while not self.should_die:

0 commit comments

Comments
 (0)