Files
site/create_event.sh

177 lines
5.2 KiB
Bash
Executable File

#!/bin/bash
# Function to prompt for input with a default value
prompt() {
local prompt_text="$1"
local default_val="$2"
local var_name="$3"
if [ -n "$default_val" ]; then
read -p "$prompt_text [$default_val]: " input
eval $var_name="\${input:-$default_val}"
else
read -p "$prompt_text: " input
eval $var_name="\$input"
fi
}
# --- 1. Gather Event Details ---
echo "--- New Event Wizard ---"
prompt "Event Name" "" EVENT_NAME
prompt "Date (YYYY-MM-DD)" "$(date +%Y-%m-%d)" EVENT_DATE
prompt "Time (e.g. 6-9 PM)" "6-9 PM" EVENT_TIME
prompt "Location" "Engineering IV, Maxwell Room (57-124)" EVENT_LOCATION
prompt "Ping Preference (1: @everyone, 2: @UCLA, 3: None)" "3" PING_CHOICE
case $PING_CHOICE in
1) PING_TEXT="@everyone" ;;
2) PING_TEXT="<@&1000205372327481444>" ;; # Assuming @UCLA role ID, replace if known or use generic
*) PING_TEXT="" ;;
esac
echo "Enter Description (Markdown supported). Press Ctrl+D on a new line to finish:"
EVENT_DESCRIPTION=$(cat)
echo "" # Add newline after input
# Slugify event name for filename
SLUG=$(echo "$EVENT_NAME" | iconv -t ascii//TRANSLIT | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g | tr A-Z a-z)
FILENAME="content/events/${SLUG}.md"
# --- 2. Generate Hugo Content ---
echo "Generating $FILENAME..."
cat <<EOF > "$FILENAME"
---
title: "$EVENT_NAME"
date: $(date +%Y-%m-%d)
tags: [events]
author: LUG Board
---
$EVENT_DESCRIPTION
* **Date**: $EVENT_DATE
* **Time**: $EVENT_TIME
* **Location**: $EVENT_LOCATION
EOF
echo "Hugo page created."
# --- 3. Discord Notification (Webhook) ---
# Check for env var or prompt
if [ -z "$DISCORD_WEBHOOK_URL" ]; then
prompt "Enter Discord Webhook URL (leave empty to skip)" "" DISCORD_WEBHOOK_URL
fi
if [ -n "$DISCORD_WEBHOOK_URL" ]; then
echo "Sending Discord announcement..."
# Construct JSON payload
# Note: Using jq would be safer, but manual construction avoids dependencies if simple
# Escaping quotes in description for JSON
SAFE_DESC=$(echo "$EVENT_DESCRIPTION" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
JSON_PAYLOAD=$(cat <<EOF
{
"content": "$PING_TEXT **New Event: $EVENT_NAME**",
"embeds": [
{
"title": "$EVENT_NAME",
"description": "$SAFE_DESC",
"color": 5814783,
"fields": [
{
"name": "When",
"value": "$EVENT_DATE @ $EVENT_TIME",
"inline": true
},
{
"name": "Where",
"value": "$EVENT_LOCATION",
"inline": true
}
]
}
]
}
EOF
)
curl -H "Content-Type: application/json" -d "$JSON_PAYLOAD" "$DISCORD_WEBHOOK_URL"
echo -e "\nAnnouncement sent."
else
echo "Skipping Discord announcement."
fi
# --- 4. Discord Calendar Event ---
# Requires Bot Token and Guild ID
if [ -z "$DISCORD_BOT_TOKEN" ]; then
prompt "Enter Discord Bot Token (leave empty to skip calendar)" "" DISCORD_BOT_TOKEN
fi
if [ -z "$DISCORD_GUILD_ID" ] && [ -n "$DISCORD_BOT_TOKEN" ]; then
prompt "Enter Discord Guild ID" "" DISCORD_GUILD_ID
fi
if [ -n "$DISCORD_BOT_TOKEN" ] && [ -n "$DISCORD_GUILD_ID" ]; then
echo "Creating Discord Calendar Event..."
# Need ISO8601 timestamp for start_time.
# Attempt to parse date and time. This is tricky in bash without strict format.
# Assuming user input follows YYYY-MM-DD and roughly HH:MM format or we just use the date at a default time if parsing fails?
# Let's try to be smart: Date + Start time.
# Extract start time from "6-9 PM" -> "18:00"
# This is a simplification. For robust parsing, might need python or date utils.
# Extract start hour and minute
START_HOUR=$(echo "$EVENT_TIME" | grep -oE '^[0-9]+' | head -1)
START_MINUTE=$(echo "$EVENT_TIME" | grep -oE ':[0-9]+' | sed 's/://' | head -1)
START_MINUTE=${START_MINUTE:-00}
if [[ "$EVENT_TIME" == *"PM"* ]] && [ "$START_HOUR" -lt 12 ]; then
START_HOUR=$((START_HOUR + 12))
fi
# Construct ISO string with timezone using date command
START_DATE_STR="$EVENT_DATE $START_HOUR:$START_MINUTE"
START_ISO=$(date -d "$START_DATE_STR" --iso-8601=seconds)
# End time: Start + 2 hours
# Use START_ISO as base to ensure correct calculation
END_ISO=$(date -d "$START_ISO + 2 hours" --iso-8601=seconds)
# Location Type 3 is "External"
CAL_PAYLOAD=$(cat <<EOF
{
"name": "$EVENT_NAME",
"description": "$SAFE_DESC",
"scheduled_start_time": "$START_ISO",
"scheduled_end_time": "$END_ISO",
"privacy_level": 2,
"entity_type": 3,
"entity_metadata": {
"location": "$EVENT_LOCATION"
}
}
EOF
)
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "https://discord.com/api/v10/guilds/$DISCORD_GUILD_ID/scheduled-events" \
-H "Authorization: Bot $DISCORD_BOT_TOKEN" \
-H "Content-Type: application/json" \
-d "$CAL_PAYLOAD")
HTTP_BODY=$(echo "$RESPONSE" | head -n -1)
HTTP_STATUS=$(echo "$RESPONSE" | tail -n 1)
if [[ "$HTTP_STATUS" =~ ^2 ]]; then
echo -e "\nCalendar event created."
else
echo -e "\nFailed to create calendar event. HTTP $HTTP_STATUS"
echo "Response: $HTTP_BODY"
echo "Payload: $CAL_PAYLOAD"
fi
else
echo "Skipping Calendar event."
fi
echo "Done!"