Skip to content

Commit 7ffb37b

Browse files
fix: /gh user now doesn't break for organizations
and also, it shows number of members and the type of user a login is
1 parent 43fb4a0 commit 7ffb37b

File tree

1 file changed

+52
-23
lines changed

1 file changed

+52
-23
lines changed

bot/src/ghutils/cogs/app_commands/github.py

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,6 @@ async def user(
166166
user: UserOption,
167167
visibility: MessageVisibility = "private",
168168
):
169-
# Use GraphQL to get # of repos starred by user...?
170-
async with self.bot.github_app(interaction) as (github, _):
171-
result = await github.async_graphql(
172-
"""
173-
query($username: String!) {
174-
user(login: $username) {
175-
starredRepositories {
176-
totalCount
177-
}
178-
}
179-
}
180-
""",
181-
{
182-
"username": user.login,
183-
},
184-
)
185-
num_stars: int = result["user"]["starredRepositories"]["totalCount"]
186-
187169
# Pylette ints are actually int64s, thanks NumPy
188170
user_rgb = [
189171
int(val)
@@ -194,6 +176,7 @@ async def user(
194176
.rgb
195177
]
196178

179+
# Start creating the embed first (see GraphQL queries)
197180
embed = (
198181
Embed(
199182
description=user.bio,
@@ -202,19 +185,65 @@ async def user(
202185
)
203186
.set_thumbnail(url=user.avatar_url)
204187
.add_field(name="Repositories", value=user.public_repos, inline=True)
205-
.add_field(name="Stars", value=num_stars, inline=True)
206188
)
207189

208-
# In case user doesn't have a display name
190+
footer_text: str = f"{user.type}{user.followers} followers"
191+
192+
async with self.bot.github_app(interaction) as (github, state):
193+
match user.type:
194+
# Users only: get # of repos starred and # following
195+
case "User":
196+
result = await github.async_graphql(
197+
"""
198+
query($username: String!) {
199+
user(login: $username) {
200+
starredRepositories {
201+
totalCount
202+
}
203+
}
204+
}
205+
""",
206+
{
207+
"username": user.login,
208+
},
209+
)
210+
num_stars: int = result["user"]["starredRepositories"]["totalCount"]
211+
embed.add_field(name="Stars", value=num_stars, inline=True)
212+
footer_text += f" • {user.following} following"
213+
214+
# Organizations only: add # of people in org (only works if logged in)
215+
case "Organization":
216+
if state == LoginState.LOGGED_IN:
217+
result = await github.async_graphql(
218+
"""
219+
query ($name: String!) {
220+
organization(login: $name) {
221+
membersWithRole {
222+
totalCount
223+
}
224+
}
225+
}
226+
""",
227+
{
228+
"name": user.login,
229+
},
230+
)
231+
num_members: int = result["organization"]["membersWithRole"][
232+
"totalCount"
233+
]
234+
embed.add_field(name="People", value=num_members, inline=True)
235+
236+
case _:
237+
logger.warning(f"Unhandled user type: {user.type}")
238+
239+
# In case there's no display name
209240
if user.name is None:
210241
embed.title = user.login
211242
else:
212243
embed.title = user.name
213244
embed.set_author(name=user.login)
214245

215-
embed.set_footer(
216-
text=f"{user.followers} followers • {user.following} following"
217-
)
246+
embed.set_footer(text=footer_text)
218247

219248
await respond_with_visibility(interaction, visibility, embed=embed)
220249

0 commit comments

Comments
 (0)