Skip to content

Commit cd456cc

Browse files
committed
Automate fetching events from discord
1 parent a569758 commit cd456cc

File tree

5 files changed

+120
-1
lines changed

5 files changed

+120
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Fetch Discord Events (Node.js)
2+
3+
on:
4+
schedule:
5+
# Run every 4 hours
6+
- cron: '0 8,12,16,20 * * *'
7+
workflow_dispatch: # Allows for a manual run
8+
9+
jobs:
10+
fetch_and_dump:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: write
14+
15+
steps:
16+
# Step 1: Check out the repository code
17+
- name: Checkout repository
18+
uses: actions/checkout@v4
19+
20+
# Step 2: Set up a Node.js environment
21+
- name: Set up Node.js
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: '20.x' # Or any version you prefer
25+
26+
# Step 3: Install Node.js dependencies
27+
- name: Install dependencies
28+
run: npm install
29+
working-directory: 'automation'
30+
31+
# Step 4: Run the Node.js script to fetch events and save to a file
32+
- name: Fetch and save events
33+
run: node get_events.js > ../scheduled_events.json
34+
working-directory: 'automation'
35+
env:
36+
# Use the GitHub Secret we created to pass the token
37+
DISCORD_BOT_TOKEN: ${{ secrets.DISCORD_BOT_TOKEN }}
38+
39+
# Step 5: Commit the new JSON file back to the repository
40+
- name: Commit and push changes
41+
run: |
42+
git config user.name github-actions
43+
git config user.email github-actions@github.com
44+
git add scheduled_events.json
45+
git commit -m "chore(scheduled-events): Update scheduled events from Discord API" || echo "No changes to commit"
46+
git push

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# dependencies
44
/node_modules
5+
*/node_modules
56

67
# vite
78
/dist/
@@ -33,4 +34,4 @@ yarn.lock
3334
# IDEs
3435
/.vscode/*
3536
/.idea/*
36-
/.DS_Store
37+
/.DS_Store

automation/get_events.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const axios = require('axios');
2+
const fs = require('fs');
3+
4+
// Get your Discord bot token from an environment variable
5+
const DISCORD_BOT_TOKEN = process.env.DISCORD_BOT_TOKEN;
6+
7+
// Replace with the actual Guild ID you want to query
8+
const GUILD_ID = '780140656597008387'; // e.g., '780140656597008387'
9+
10+
// The Discord API endpoint for scheduled events
11+
const API_ENDPOINT = `https://discord.com/api/v10/guilds/${GUILD_ID}/scheduled-events`;
12+
13+
// Function to fetch scheduled events
14+
async function getScheduledEvents() {
15+
if (!DISCORD_BOT_TOKEN) {
16+
console.error('Error: DISCORD_BOT_TOKEN environment variable not set.');
17+
process.exit(1);
18+
}
19+
20+
if (GUILD_ID === 'YOUR_GUILD_ID_HERE') {
21+
console.error("Error: Please replace 'YOUR_GUILD_ID_HERE' with your actual guild ID.");
22+
process.exit(1);
23+
}
24+
25+
try {
26+
const response = await axios.get(API_ENDPOINT, {
27+
headers: {
28+
Authorization: `Bot ${DISCORD_BOT_TOKEN}`,
29+
'Content-Type': 'application/json',
30+
},
31+
});
32+
33+
// Log the JSON data to standard output
34+
console.log(JSON.stringify(response.data, null, 2));
35+
} catch (error) {
36+
if (error.response) {
37+
// The request was made and the server responded with a status code
38+
// that falls out of the range of 2xx
39+
console.error(
40+
`Error querying Discord API: ${error.response.status} ${error.response.statusText}`
41+
);
42+
console.error('Response data:', error.response.data);
43+
} else if (error.request) {
44+
// The request was made but no response was received
45+
console.error('No response received from Discord API.');
46+
} else {
47+
// Something happened in setting up the request that triggered an error
48+
console.error('Error during request setup:', error.message);
49+
}
50+
process.exit(1);
51+
}
52+
}
53+
54+
// Run the function
55+
getScheduledEvents();
56+

automation/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "automation",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"axios": "^1.11.0"
14+
}
15+
}

scheduled_events.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)