Skip to content

Commit

Permalink
add /agenda command
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzdom committed Nov 23, 2024
1 parent bba617d commit 660c4dc
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ DISCORD_UID_MAP="user1=1234,user2=4567,user3=7890"

LDAP_USERNAME=
LDAP_PASSWORD=


AGENDA_TEMPLATE_URL="https://md.redbrick.dcu.ie/foo"
4 changes: 4 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"bot-private": 853071983452225536,
"bots-cmt": 1162038557922312312,
"action-items": 1029132014210793513,
"cowboys-and-cowgirls-committee": 578712722330353684,
"committee-announcements": 763113612340363304,
}

# TODO: query API/LDAP for these
Expand All @@ -30,3 +32,5 @@

LDAP_USERNAME = os.environ.get("LDAP_USERNAME")
LDAP_PASSWORD = os.environ.get("LDAP_PASSWORD")

AGENDA_TEMPLATE_URL = os.environ.get("AGENDA_TEMPLATE_URL")
152 changes: 152 additions & 0 deletions src/extensions/agenda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import arc
import hikari
import aiohttp
from urllib.parse import urlparse
import datetime

from src.utils import role_mention, hedgedoc_login
from src.hooks import restrict_to_channels, restrict_to_roles
from src.config import CHANNEL_IDS, ROLE_IDS, UID_MAPS, AGENDA_TEMPLATE_URL


plugin = arc.GatewayPlugin(name="Agenda")


def generate_date_choices():
"""Generate date options for the next 7 days."""
today = datetime.date.today()
return [(today + datetime.timedelta(days=i)).strftime("%Y-%m-%d") for i in range(7)]


def generate_time_choices():
"""Generate time options for every hour"""
base_time = datetime.time(0, 0)
times = []
for hour in range(24):
current_time = (
datetime.datetime.combine(datetime.date.today(), base_time)
+ datetime.timedelta(hours=hour)
).time()
times.append(current_time.strftime("%H:%M"))
return times


@plugin.include
@arc.with_hook(restrict_to_channels(channel_ids=[CHANNEL_IDS["bots-cmt"]]))
@arc.with_hook(restrict_to_roles(role_ids=[ROLE_IDS["committee"]]))
@arc.slash_command(
"agenda",
"Generate a new agenda for committee meetings",
is_dm_enabled=False,
autodefer=arc.AutodeferMode.EPHEMERAL,
)
async def gen_agenda(
ctx: arc.GatewayContext,
date: arc.Option[
str,
arc.StrParams("Select a date", choices=generate_date_choices()),
],
time: arc.Option[
str,
arc.StrParams(
"Enter the time in HH:MM format", choices=generate_time_choices()
),
],
room: arc.Option[
str,
arc.StrParams("Select a Room"),
],
url: arc.Option[
str, arc.StrParams("URL of the agenda template from the MD")
] = AGENDA_TEMPLATE_URL,
aiohttp_client: aiohttp.ClientSession = arc.inject(),
) -> None:
"""Create a new agenda for committee meetings"""

parsed_date = datetime.datetime.strptime(date, "%Y-%m-%d").date()
parsed_time = datetime.datetime.strptime(time, "%H:%M").time()

parsed_datetime = datetime.datetime.combine(parsed_date, parsed_time)

DATE = parsed_datetime.strftime("%Y-%m-%d")
TIME = parsed_datetime.strftime("%H:%M")
full_datetime = parsed_datetime.strftime("%Y-%m-%d %A %H:%M")

if "https://md.redbrick.dcu.ie" not in url:
await ctx.respond(
f"❌ `{url}` is not a valid MD URL. Please provide a valid URL.",
flags=hikari.MessageFlag.EPHEMERAL,
)
return

await hedgedoc_login(aiohttp_client)

parsed_url = urlparse(url)
request_url = (
f"{parsed_url.scheme}://{parsed_url.hostname}{parsed_url.path}/download"
)

async with aiohttp_client.get(request_url) as response:
if response.status != 200:
await ctx.respond(
f"❌ Failed to fetch the minutes. Status code: `{response.status}`",
flags=hikari.MessageFlag.EPHEMERAL,
)
return

content = await response.text()

modified_content = content.format(DATE=DATE, TIME=TIME, ROOM=room)

post_url = f"{parsed_url.scheme}://{parsed_url.hostname}/new"
post_headers = {"Content-Type": "text/markdown"}

async with aiohttp_client.post(
url=post_url,
headers=post_headers,
data=modified_content,
) as response:
if response.status != 200:
await ctx.respond(
f"❌ Failed to generate the agenda. Status code: `{response.status}`",
flags=hikari.MessageFlag.EPHEMERAL,
)
return

new_agenda_url = response.url
announce_text = f"""
## 📣 Agenda for this week's meeting | {full_datetime} | {room} <:bigRed:634311607039819776>
[{DATE} Agenda](<{new_agenda_url}>)
- Please fill in your sections with anything you would like to discuss.
- Put your Redbrick `username` beside any agenda items you add.
- If you can't attend the meeting, please DM <@{UID_MAPS["kronos"]}> with your reason.
- React with <:bigRed:634311607039819776> if you can make it.
||{role_mention(ROLE_IDS["committee"])}||
"""

announce = await plugin.client.rest.create_message(
CHANNEL_IDS["bots-cmt"],
content=announce_text,
)

await plugin.client.rest.add_reaction(
channel=announce.channel_id,
message=announce.id,
emoji="<:bigRed:634311607039819776>",
)

# respond with success if it executes successfully
await ctx.respond(
"✅ Agenda generated. Announcement sent successfully!",
flags=hikari.MessageFlag.EPHEMERAL,
)
return


@arc.loader
def loader(client: arc.GatewayClient) -> None:
client.add_plugin(plugin)

0 comments on commit 660c4dc

Please sign in to comment.