Skip to content

Commit 64e82cc

Browse files
authored
Merge pull request #1 from ut-code/joji-bot
bot/joji-bot: init
1 parent 1c89434 commit 64e82cc

10 files changed

Lines changed: 202 additions & 0 deletions

File tree

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/bots/asakatsu-bot/ @nakaterm
55
/bots/auto-moderator/ @chelproc @aster-void
66
/bots/gsc-report/ @na-trium-144
7+
/bots/joji-bot/ @tknkaa

bots/joji-bot/.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
WEBHOOK_URL=https://discord.com/api/webhooks/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2+
LAST_ID_FILE=~/run/last_video_id.txt

bots/joji-bot/.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# dependencies (bun install)
2+
node_modules
3+
4+
# output
5+
out
6+
dist
7+
*.tgz
8+
9+
# code coverage
10+
coverage
11+
*.lcov
12+
13+
# logs
14+
logs
15+
_.log
16+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
17+
18+
# dotenv environment variable files
19+
.env
20+
.env.development.local
21+
.env.test.local
22+
.env.production.local
23+
.env.local
24+
25+
# caches
26+
.eslintcache
27+
.cache
28+
*.tsbuildinfo
29+
30+
# IntelliJ based IDEs
31+
.idea
32+
33+
# Finder (MacOS) folder config
34+
.DS_Store
35+
36+
last_video_id.txt

bots/joji-bot/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# joji-bot
2+
3+
To install dependencies:
4+
5+
```bash
6+
bun install
7+
```
8+
9+
To run:
10+
11+
```bash
12+
bun run index.ts
13+
```
14+
15+
This project was created using `bun init` in bun v1.3.5. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.

bots/joji-bot/bun.lock

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bots/joji-bot/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import Parser from 'rss-parser';
2+
3+
const WEBHOOK_URL = process.env.WEBHOOK_URL;
4+
const CHANNEL_ID = 'UCGxzWLH1_1ABKKfSiy2WIAw';
5+
const RSS_URL = `https://www.youtube.com/feeds/videos.xml?channel_id=${CHANNEL_ID}`;
6+
const LAST_ID_FILE = process.env.LAST_ID_FILE || 'last_video_id.txt';
7+
8+
async function main() {
9+
const parser = new Parser();
10+
const response = await fetch(RSS_URL);
11+
const feedText = await response.text();
12+
const feed = await parser.parseString(feedText);
13+
if (!feed.items || feed.items.length === 0) {
14+
console.log('No entries found in RSS feed');
15+
return;
16+
}
17+
18+
const latestVideo = feed.items[0];
19+
const latestId = latestVideo.id?.split(':').pop();
20+
const videoUrl = latestVideo.link;
21+
const videoTitle = latestVideo.title;
22+
console.log(`Latest video: ${videoTitle} (ID: ${latestId})`);
23+
24+
let lastId = '';
25+
try {
26+
lastId = await Bun.file(LAST_ID_FILE).text();
27+
} catch {}
28+
if (lastId === latestId) {
29+
console.log(`No new video (last ID: ${lastId})`);
30+
return;
31+
}
32+
33+
const message = {
34+
content: `**${videoTitle}**\nNew video uploaded!\n${videoUrl}`
35+
};
36+
const res = await fetch(WEBHOOK_URL!, {
37+
method: 'POST',
38+
headers: { 'Content-Type': 'application/json' },
39+
body: JSON.stringify(message)
40+
});
41+
console.log(`Discord response: ${res.status}`);
42+
43+
if (res.status === 204) {
44+
await Bun.write(LAST_ID_FILE, latestId!);
45+
console.log('Notification sent successfully!');
46+
} else {
47+
const errorText = await res.text();
48+
console.log(`Failed to send notification: ${errorText}`);
49+
}
50+
}
51+
52+
main()
53+

bots/joji-bot/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "joji-bot",
3+
"module": "index.ts",
4+
"type": "module",
5+
"private": true,
6+
"devDependencies": {
7+
"@types/bun": "latest"
8+
},
9+
"peerDependencies": {
10+
"typescript": "^5"
11+
},
12+
"dependencies": {
13+
"rss-parser": "^3.13.0"
14+
}
15+
}

bots/joji-bot/run.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
bun install --frozen-lockfile
5+
bun --env-file=.env run index.ts

bots/joji-bot/tsconfig.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"compilerOptions": {
3+
// Environment setup & latest features
4+
"lib": ["ESNext"],
5+
"target": "ESNext",
6+
"module": "Preserve",
7+
"moduleDetection": "force",
8+
"jsx": "react-jsx",
9+
"allowJs": true,
10+
11+
// Bundler mode
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"verbatimModuleSyntax": true,
15+
"noEmit": true,
16+
17+
// Best practices
18+
"strict": true,
19+
"skipLibCheck": true,
20+
"noFallthroughCasesInSwitch": true,
21+
"noUncheckedIndexedAccess": true,
22+
"noImplicitOverride": true,
23+
24+
// Some stricter flags (disabled by default)
25+
"noUnusedLocals": false,
26+
"noUnusedParameters": false,
27+
"noPropertyAccessFromIndexSignature": false
28+
}
29+
}

rollcron.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ jobs:
3434
env_file: ~/run/discord-bots/asakatsu-bot/env
3535
log: ~/run/discord-bots/asakatsu-bot/log
3636

37+
joji-bot:
38+
schedule: "7pm"
39+
working_dir: bots/joji-bot
40+
run: ./run.sh
41+
env_file: ~/run/discord-bots/joji-bot/env
42+
log: ~/run/discord-bots/joji-bot/log
43+

0 commit comments

Comments
 (0)