-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
109 lines (83 loc) · 3.45 KB
/
main.py
File metadata and controls
109 lines (83 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import discord
import os
import aiohttp
from dotenv import load_dotenv
load_dotenv()
bot = discord.Bot()
BASE_URL = "https://api.github.com/repos/NCSU-App-Development-Club/"
REPOS = {}
def create_url(repo: str, endpoint: str) -> str:
return f"{BASE_URL}{repo}/{endpoint}"
def filter_open_issues(issue):
return issue["state"] == "open" and "pull_request" not in issue.keys()
def filter_prs(issue):
return "pull_request" in issue.keys()
def format_issue(issue):
assignee = issue["assignee"]["login"] if issue["assignee"] else "None"
return f"- [#{issue['number']}: {issue['title']}]({issue['html_url']}) - {assignee}"
def format_pr(pr):
assignee = pr["user"]["login"] if pr["user"] else "None"
return f"- [#{pr['number']}: {pr['title']}]({pr['html_url']}) - {assignee}"
def format_repo(repo):
return f"- [{repo['name']}]({repo['html_url']}) - {repo['description'] or 'No description'}"
@bot.event
async def on_ready():
global REPOS
async with aiohttp.ClientSession() as session:
async with session.get(
"https://api.github.com/orgs/NCSU-App-Development-Club/repos"
) as response:
REPOS = {repo["name"]: repo["html_url"] for repo in await response.json()}
print(REPOS)
print(f"{bot.user} is ready and online!")
@bot.slash_command(
name="repos",
description="View NCSU App Development Club repositories",
)
async def repos(ctx: discord.ApplicationContext):
repo_list = "\n".join(
[f"- [{repo}]({REPOS[repo]})" for repo in sorted(REPOS.keys())]
)
await ctx.send(
"## NCSU App Development Club Repositories:\n" + repo_list, suppress=True
)
await ctx.respond("✅", ephemeral=True)
@bot.slash_command(
name="issues",
description="View open issues for NCSU App Development Club repositories",
)
async def issues(ctx: discord.ApplicationContext, repo: str = "ncsuguessr"):
async with aiohttp.ClientSession() as session:
async with session.get(
create_url(repo, "issues"),
) as response:
if response.status != 200:
await ctx.respond(
f"❌ Error fetching issues for repository '{repo}'. Please ensure the repository exists and try again.",
ephemeral=True,
)
return
issues = filter(filter_open_issues, await response.json())
issues_text = "\n".join([format_issue(issue) for issue in issues])
await ctx.send(f"## Open Issues in {repo}:\n" + issues_text, suppress=True)
await ctx.respond("✅", ephemeral=True)
@bot.slash_command(
name="prs",
description="View open PRs for NCSU App Development Club repositories",
)
async def prs(ctx: discord.ApplicationContext, repo: str = "ncsuguessr"):
async with aiohttp.ClientSession() as session:
async with session.get(
create_url(repo, "issues"),
) as response:
if response.status != 200:
await ctx.respond(
f"❌ Error fetching issues for repository '{repo}'. Please ensure the repository exists and try again.",
ephemeral=True,
)
return
issues = filter(filter_prs, await response.json())
issues_text = "\n".join([format_pr(issue) for issue in issues])
await ctx.send(f"## Open PRs in {repo}:\n" + issues_text, suppress=True)
await ctx.respond("✅", ephemeral=True)
bot.run(os.getenv("DISCORD_BOT_TOKEN"))