Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add action items command #55

Merged
merged 13 commits into from
Nov 18, 2024
3 changes: 3 additions & 0 deletions .github/deploy/production.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ job "blockbot" {
data = <<EOF
TOKEN={{ key "blockbot/discord/token" }}
DEBUG=false
LDAP_USERNAME={{ key "blockbot/ldap/username" }}
LDAP_PASSWORD={{ key "blockbot/ldap/password" }}
UID_MAP={{ key "blockbot/ldap/uid_map" }}
wizzdom marked this conversation as resolved.
Show resolved Hide resolved
EOF
destination = "local/.env"
env = true
Expand Down
3 changes: 3 additions & 0 deletions .github/deploy/review.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ job "blockbot-[[.environment_slug]]" {
data = <<EOF
TOKEN={{ key "blockbot-dev/discord/token" }}
DEBUG=false
LDAP_USERNAME={{ key "blockbot-dev/ldap/username" }}
LDAP_PASSWORD={{ key "blockbot-dev/ldap/password" }}
UID_MAP={{ key "blockbot-dev/ldap/uid_map" }}
wizzdom marked this conversation as resolved.
Show resolved Hide resolved
EOF
destination = "local/.env"
env = true
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ hikari-miru==4.1.1
python-dotenv==1.0.1
pyfiglet==1.0.2
fortune-python==1.1.1
requests==2.32.3
23 changes: 22 additions & 1 deletion src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,26 @@

TOKEN = os.environ.get("TOKEN") # required
DEBUG = os.environ.get("DEBUG", False)
UID_MAP = os.environ.get("UID_MAP")
wizzdom marked this conversation as resolved.
Show resolved Hide resolved

CHANNEL_IDS: dict[str, int] = {"lobby": 627542044390457350}
CHANNEL_IDS: dict[str, int] = {
"lobby": 627542044390457350,
"bot-private": 853071983452225536,
"bots-cmt": 1162038557922312312,
"action-items": 1029132014210793513,
}

ROLE_IDS: dict[str, int] = {
"all": 568762266992902179,
"everyone": 568762266992902179,
"committee": 568762266992902179,
"cmt": 568762266992902179,
"events": 807389174009167902,
"admins": 585512338728419341,
"helpdesk": 1194683307921772594,
}

UID_MAPS = dict(item.split("=") for item in UID_MAP.split(","))

LDAP_USERNAME = os.environ.get("LDAP_USERNAME")
LDAP_PASSWORD = os.environ.get("LDAP_PASSWORD")
wizzdom marked this conversation as resolved.
Show resolved Hide resolved
111 changes: 111 additions & 0 deletions src/extensions/action_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import arc
import hikari
import requests
import re

# not used, functionality for times/deadlines to be added later
from datetime import datetime, timezone

from src.utils import role_mention
from src.hooks import restrict_to_channels, restrict_to_roles
from src.config import CHANNEL_IDS, ROLE_IDS, UID_MAPS, LDAP_USERNAME, LDAP_PASSWORD


action_items = arc.GatewayPlugin(name="Action Items")

session = requests.Session()
novanai marked this conversation as resolved.
Show resolved Hide resolved

data = {
"username": LDAP_USERNAME,
"password": LDAP_PASSWORD,
}

response = session.post("https://md.redbrick.dcu.ie/auth/ldap", data=data)
if response.ok:
print("Login to MD successful!")
else:
print("Login to MD failed!")


@action_items.include
# @arc.with_hook(restrict_to_channels(channel_ids=[CHANNEL_IDS["action-items"]]))
# @arc.with_hook(restrict_to_roles(role_ids=[ROLE_IDS["committee"]]))
@arc.slash_command("action_items", "Display the action items from the MD")
async def get_action_items(
ctx: arc.GatewayContext,
url: arc.Option[str, arc.StrParams("URL of the minutes from the MD")],
) -> None:
"""Display the action items from the MD!"""

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

url_without_fragment = url.split("#", 1)[0]

clean_url = url_without_fragment.split("?", 1)[0]

request = clean_url + "/download"
response = session.get(request)
if not response.status_code == 200:
await ctx.respond(
f"❌ Failed to fetch the minutes. Status code: `{response.status_code}`",
flags=hikari.MessageFlag.EPHEMERAL,
)
return
content = response.text

action_items_section = re.search(
r"# Action Items:?\n(.*?)(\n# |\n---|$)", content, re.DOTALL
wizzdom marked this conversation as resolved.
Show resolved Hide resolved
)

if not action_items_section:
await ctx.respond("❌ No `Action Items` section found.")
return

# Get the matched content (excluding the "Action Items" heading itself)
action_items_content = action_items_section.group(1)

bullet_points = re.findall(r"^\s*[-*]\s+(.+)", action_items_content, re.MULTILINE)
formatted_bullet_points = [
"- " + re.sub(r"^\[.\]\s+", "", item) for item in bullet_points
]

# Replace user names with user mentions
for i, item in enumerate(formatted_bullet_points):
for name, uid in UID_MAPS.items():
item = item.replace(f"`{name}`", f"<@{uid}>")
formatted_bullet_points[i] = item

# Replace role names with role mentions
for i, item in enumerate(formatted_bullet_points):
for role, role_id in ROLE_IDS.items():
item = item.replace(f"`{role}`", role_mention(role_id))
formatted_bullet_points[i] = item

# Send title to the action-items channel
await action_items.client.rest.create_message(
CHANNEL_IDS["action-items"],
content="# Action Items:",
)

# send each bullet point seperately
for item in formatted_bullet_points:
await action_items.client.rest.create_message(
CHANNEL_IDS["action-items"],
mentions_everyone=False,
user_mentions=True,
role_mentions=True,
content=item,
)
# respond with success if it executes successfully
await ctx.respond("✅ Action Items sent successfully!")
return


@arc.loader
def loader(client: arc.GatewayClient) -> None:
client.add_plugin(action_items)
27 changes: 27 additions & 0 deletions src/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import arc
import hikari
import typing


def restrict_to_roles(
wizzdom marked this conversation as resolved.
Show resolved Hide resolved
ctx: arc.GatewayContext, role_ids: typing.Sequence[hikari.Snowflake]
) -> arc.HookResult:
if not any(role_id in ctx.author.role_ids for role_id in role_ids):
ctx.respond(
"❌ This command is restricted. Only allowed roles are permitted to use this command.",
flags=hikari.MessageFlag.EPHEMERAL,
)
return arc.HookResult(abort=True)
return arc.HookResult()


def restrict_to_channels(
ctx: arc.GatewayContext, channel_ids: typing.Sequence[hikari.Snowflake]
) -> arc.HookResult:
if ctx.channel_id not in channel_ids:
ctx.respond(
"❌ This command cannot be used in this channel.",
flags=hikari.MessageFlag.EPHEMERAL,
)
return arc.HookResult(abort=True)
return arc.HookResult()