Created
November 8, 2025 00:17
-
-
Save user890104/712e263f98eadc5e70b04092167af644 to your computer and use it in GitHub Desktop.
Sync Wordpress Event Manager events to Discord
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # CONFIGURATION | |
| # test | |
| #DISCORD_GUILD_ID="1434925424957460520" | |
| # prod | |
| DISCORD_GUILD_ID="1393669204238008350" | |
| # bot token is always the same | |
| DISCORD_BOT_TOKEN="xxx" | |
| TODAY=$(date --rfc-3339=seconds | cut -d + -f 1) | |
| # Helper: Convert WordPress attachment ID to direct image URL | |
| get_featured_image_url() { | |
| local post_id="$1" | |
| local image_id | |
| image_id=$(wp post meta get "$post_id" _thumbnail_id --quiet 2>/dev/null || echo "") | |
| if [ -n "$image_id" ]; then | |
| wp post get "$image_id" --field=guid --quiet 2>/dev/null || echo "" | |
| else | |
| echo "" | |
| fi | |
| } | |
| cd www | |
| # Get only future events via WP-CLI meta_query | |
| EVENTS_JSON=$(wp post list \ | |
| --post_type=event \ | |
| --post_status=publish \ | |
| --meta_key=_event_start_local \ | |
| --meta_value="${TODAY}" \ | |
| --meta_compare='>=' \ | |
| --orderby=meta_value \ | |
| --order=ASC \ | |
| --fields=ID,post_title \ | |
| --format=json) | |
| # Iterate through events | |
| echo "$EVENTS_JSON" | jq -c '.[]' | while read -r event; do | |
| EVENT_ID=$(echo "$event" | jq -r '.ID') | |
| TITLE=$(echo "$event" | jq -r '.post_title') | |
| DESCRIPTION=$(wp eval " | |
| \$desc = wp_strip_all_tags(get_post($EVENT_ID)->post_content); | |
| \$maxLength = 1000; | |
| if (mb_strlen(\$desc) > \$maxLength) { | |
| \$desc = mb_substr(\$desc, 0, \$maxLength - 1) . '…'; | |
| } | |
| echo \$desc; | |
| " --quiet) | |
| START_TIMESTAMP=$(wp post meta get "$EVENT_ID" _event_start --quiet 2>/dev/null || echo "") | |
| END_TIMESTAMP=$(wp post meta get "$EVENT_ID" _event_end --quiet 2>/dev/null || echo "") | |
| LOCATION_ID=$(wp post meta get "$EVENT_ID" _location_id --quiet 2>/dev/null || echo "") | |
| if [ -n "$LOCATION_ID" ]; then | |
| LOCATION=$(wp db query " | |
| SELECT location_name | |
| FROM il_em_locations | |
| WHERE location_id = ${LOCATION_ID} | |
| LIMIT 1 | |
| " --skip-column-names | cat) | |
| else | |
| LOCATION='init Lab' | |
| fi | |
| # Get featured image URL | |
| IMAGE_URL=$(get_featured_image_url "$EVENT_ID") | |
| # Get existing Discord event ID from WP post meta | |
| DISCORD_EVENT_ID=$(wp post meta get "$EVENT_ID" discord_event_id --quiet 2>/dev/null || echo "") | |
| # Prepare JSON payload for Discord API | |
| EVENT_PAYLOAD=$(jq -n \ | |
| --arg name "$TITLE" \ | |
| --arg description "$DESCRIPTION" \ | |
| --arg start_time "$START_TIMESTAMP" \ | |
| --arg end_time "$END_TIMESTAMP" \ | |
| --argjson privacy_level 2 \ | |
| --argjson entity_type 3 \ | |
| --argjson entity_metadata "{\"location\":\"$LOCATION\"}" \ | |
| '{name: $name, description: $description, scheduled_start_time: $start_time, scheduled_end_time: $end_time, privacy_level: $privacy_level, entity_type: $entity_type, entity_metadata: $entity_metadata}') | |
| if [ -z "$DISCORD_EVENT_ID" ]; then | |
| echo "Creating new Discord event for WP Event ID $EVENT_ID..." | |
| RESPONSE=$(curl -s -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 "$EVENT_PAYLOAD") | |
| DISCORD_EVENT_ID=$(echo "$RESPONSE" | jq -r '.id // empty') | |
| if [ -n "$DISCORD_EVENT_ID" ]; then | |
| wp post meta set "$EVENT_ID" discord_event_id "$DISCORD_EVENT_ID" | |
| echo "Created Discord event $DISCORD_EVENT_ID for WP Event $EVENT_ID" | |
| else | |
| echo "Failed to create Discord event for $EVENT_ID: $RESPONSE" | |
| continue | |
| fi | |
| else | |
| echo "Updating existing Discord event $DISCORD_EVENT_ID for WP Event $EVENT_ID..." | |
| RESPONSE=$(curl -s -X PATCH "https://discord.com/api/v10/guilds/$DISCORD_GUILD_ID/scheduled-events/$DISCORD_EVENT_ID" \ | |
| -H "Authorization: Bot $DISCORD_BOT_TOKEN" \ | |
| -H "Content-Type: application/json" \ | |
| -d "$EVENT_PAYLOAD") | |
| UPDATED_ID=$(echo "$RESPONSE" | jq -r '.id // empty') | |
| if [ -n "$UPDATED_ID" ]; then | |
| echo "Updated Discord event $DISCORD_EVENT_ID for WP Event $EVENT_ID" | |
| else | |
| echo "Failed to update Discord event for $EVENT_ID: $RESPONSE" | |
| continue | |
| fi | |
| fi | |
| # Upload event image if available | |
| if [ -n "$IMAGE_URL" ]; then | |
| echo "Uploading image for Discord event ID ${DISCORD_EVENT_ID}..." | |
| echo -n '{"image": "data:image/jpeg;base64,' > /tmp/discordimage | |
| curl -s "$IMAGE_URL" | base64 | tr -d '\n' >> /tmp/discordimage | |
| echo '"}' >> /tmp/discordimage | |
| IMAGE_RESPONSE=$(curl -s -X PATCH "https://discord.com/api/v10/guilds/$DISCORD_GUILD_ID/scheduled-events/${DISCORD_EVENT_ID}" \ | |
| -H "Authorization: Bot $DISCORD_BOT_TOKEN" \ | |
| -H "Content-Type: application/json" \ | |
| -d @/tmp/discordimage) | |
| if echo "$IMAGE_RESPONSE" | jq -e '.id' >/dev/null 2>&1; then | |
| echo "Updated image for Discord event ${DISCORD_EVENT_ID}" | |
| else | |
| echo "Failed to upload image for ${DISCORD_EVENT_ID}" | |
| fi | |
| fi | |
| done | |
| rm -f /tmp/discordimage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment