edit
This commit is contained in:
4
.env
4
.env
@@ -1,5 +1,5 @@
|
|||||||
CLIENT_ID=1d55t6jkkl2l0triq2irmog3lo3ver
|
CLIENT_ID=uw6yemlkkf2efouaoln2dtgt33uobr
|
||||||
CLIENT_SECRET=tmfe55kvjzcpv7coen38yxjzj7efhn
|
CLIENT_SECRET=o34pbqc9qk85a779zxo3vnwo3okmsq
|
||||||
|
|
||||||
BROADCASTER_ID=86025329
|
BROADCASTER_ID=86025329
|
||||||
|
|
||||||
|
|||||||
143
.gitignore
vendored
Normal file
143
.gitignore
vendored
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
|
||||||
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
|
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
*.lcov
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# Snowpack dependency directory (https://snowpack.dev/)
|
||||||
|
web_modules/
|
||||||
|
|
||||||
|
# TypeScript cache
|
||||||
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Optional stylelint cache
|
||||||
|
.stylelintcache
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variable files
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
.cache
|
||||||
|
.parcel-cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
.next
|
||||||
|
out
|
||||||
|
|
||||||
|
# Nuxt.js build / generate output
|
||||||
|
.nuxt
|
||||||
|
dist
|
||||||
|
.output
|
||||||
|
|
||||||
|
# Gatsby files
|
||||||
|
.cache/
|
||||||
|
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||||
|
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||||
|
# public
|
||||||
|
|
||||||
|
# vuepress build output
|
||||||
|
.vuepress/dist
|
||||||
|
|
||||||
|
# vuepress v2.x temp directory
|
||||||
|
.temp
|
||||||
|
|
||||||
|
# Sveltekit cache directory
|
||||||
|
.svelte-kit/
|
||||||
|
|
||||||
|
# vitepress build output
|
||||||
|
**/.vitepress/dist
|
||||||
|
|
||||||
|
# vitepress cache directory
|
||||||
|
**/.vitepress/cache
|
||||||
|
|
||||||
|
# Docusaurus cache and generated files
|
||||||
|
.docusaurus
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# Firebase cache directory
|
||||||
|
.firebase/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
.tern-port
|
||||||
|
|
||||||
|
# Stores VSCode versions used for testing VSCode extensions
|
||||||
|
.vscode-test
|
||||||
|
|
||||||
|
# pnpm
|
||||||
|
.pnpm-store
|
||||||
|
|
||||||
|
# yarn v3
|
||||||
|
.pnp.*
|
||||||
|
.yarn/*
|
||||||
|
!.yarn/patches
|
||||||
|
!.yarn/plugins
|
||||||
|
!.yarn/releases
|
||||||
|
!.yarn/sdks
|
||||||
|
!.yarn/versions
|
||||||
|
|
||||||
|
# Vite files
|
||||||
|
vite.config.js.timestamp-*
|
||||||
|
vite.config.ts.timestamp-*
|
||||||
|
.vite/
|
||||||
@@ -3,8 +3,12 @@ require("dotenv").config();
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
clientId: process.env.CLIENT_ID,
|
clientId: process.env.CLIENT_ID,
|
||||||
clientSecret: process.env.CLIENT_SECRET,
|
clientSecret: process.env.CLIENT_SECRET,
|
||||||
|
|
||||||
broadcasterId: process.env.BROADCASTER_ID,
|
broadcasterId: process.env.BROADCASTER_ID,
|
||||||
|
|
||||||
|
// THIS becomes your USER token after login
|
||||||
|
token: null,
|
||||||
|
|
||||||
rewards: {
|
rewards: {
|
||||||
HYDRATE: process.env.REWARD_HYDRATE_ID,
|
HYDRATE: process.env.REWARD_HYDRATE_ID,
|
||||||
SOUND: process.env.REWARD_SOUND_ID,
|
SOUND: process.env.REWARD_SOUND_ID,
|
||||||
|
|||||||
10
src/index.js
10
src/index.js
@@ -3,14 +3,16 @@ const { connect } = require("./twitch/websocket");
|
|||||||
|
|
||||||
const auth = startAuthServer();
|
const auth = startAuthServer();
|
||||||
|
|
||||||
// wait until token exists before starting twitch
|
// wait for login before starting Twitch connection
|
||||||
const waitForToken = setInterval(() => {
|
const wait = setInterval(() => {
|
||||||
const token = auth.getToken();
|
const token = auth.getToken();
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
clearInterval(waitForToken);
|
clearInterval(wait);
|
||||||
|
|
||||||
process.env.APP_ACCESS_TOKEN = token;
|
// inject token into runtime config
|
||||||
|
const config = require("./config");
|
||||||
|
config.token = token;
|
||||||
|
|
||||||
console.log("🚀 Starting Twitch connection...");
|
console.log("🚀 Starting Twitch connection...");
|
||||||
connect();
|
connect();
|
||||||
|
|||||||
6
src/rewards/Hydrate.js
Normal file
6
src/rewards/Hydrate.js
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
module.exports = {
|
||||||
|
name: "Hydrate",
|
||||||
|
run: (event) => {
|
||||||
|
console.log("💧 Hydration triggered for", event.user_name);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
module.exports = (event) => {
|
|
||||||
console.log("💧 Hydrate triggered by", event.user_name);
|
|
||||||
};
|
|
||||||
29
src/rewards/index.js
Normal file
29
src/rewards/index.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
const rewards = new Map();
|
||||||
|
|
||||||
|
function loadRewards() {
|
||||||
|
const files = fs.readdirSync(__dirname);
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
if (file === "index.js") continue;
|
||||||
|
if (!file.endsWith(".js")) continue;
|
||||||
|
|
||||||
|
const reward = require(path.join(__dirname, file));
|
||||||
|
|
||||||
|
if (!reward.name || !reward.run) {
|
||||||
|
console.warn(`⚠️ Invalid reward file: ${file}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
rewards.set(reward.name.toLowerCase(), reward.run);
|
||||||
|
console.log(`📦 Loaded reward: ${reward.name}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getReward(name) {
|
||||||
|
return rewards.get(name.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { loadRewards, getReward };
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
module.exports = (event) => {
|
module.exports = {
|
||||||
console.log("🔊 Sound triggered by", event.user_name);
|
name: "Sound",
|
||||||
|
run: (event) => {
|
||||||
// later: play sound, trigger OBS, etc.
|
console.log("🔊 Sound triggered for", event.user_name);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
const axios = require("axios");
|
|
||||||
const config = require("../config");
|
|
||||||
|
|
||||||
let tokenCache = null;
|
|
||||||
let expiresAt = 0;
|
|
||||||
|
|
||||||
async function getAppToken() {
|
|
||||||
const now = Date.now();
|
|
||||||
|
|
||||||
// reuse token if still valid
|
|
||||||
if (tokenCache && now < expiresAt) {
|
|
||||||
return tokenCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await axios.post(
|
|
||||||
"https://id.twitch.tv/oauth2/token",
|
|
||||||
null,
|
|
||||||
{
|
|
||||||
params: {
|
|
||||||
client_id: config.clientId,
|
|
||||||
client_secret: config.clientSecret,
|
|
||||||
grant_type: "client_credentials",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
tokenCache = res.data.access_token;
|
|
||||||
expiresAt = now + res.data.expires_in * 1000 - 60000; // refresh 1 min early
|
|
||||||
|
|
||||||
console.log("🔐 New Twitch App Token generated");
|
|
||||||
|
|
||||||
return tokenCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { getAppToken };
|
|
||||||
@@ -1,22 +1,29 @@
|
|||||||
const http = require("http");
|
const http = require("http");
|
||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
const config = require("../config");
|
const config = require("../config");
|
||||||
|
const { setTokens, startAutoRefresh } = require("./token");
|
||||||
|
|
||||||
let accessToken = null;
|
let authServerStarted = false;
|
||||||
|
|
||||||
function startAuthServer() {
|
function startAuthServer() {
|
||||||
|
if (authServerStarted) return;
|
||||||
|
authServerStarted = true;
|
||||||
|
|
||||||
const server = http.createServer(async (req, res) => {
|
const server = http.createServer(async (req, res) => {
|
||||||
const url = new URL(req.url, "http://localhost:3000");
|
const url = new URL(req.url, "http://localhost:3000");
|
||||||
|
|
||||||
|
// OAuth callback endpoint
|
||||||
if (url.pathname === "/callback") {
|
if (url.pathname === "/callback") {
|
||||||
const code = url.searchParams.get("code");
|
const code = url.searchParams.get("code");
|
||||||
|
|
||||||
if (!code) {
|
if (!code) {
|
||||||
res.end("Missing code");
|
res.writeHead(400);
|
||||||
|
res.end("Missing OAuth code");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Exchange code for tokens
|
||||||
const tokenRes = await axios.post(
|
const tokenRes = await axios.post(
|
||||||
"https://id.twitch.tv/oauth2/token",
|
"https://id.twitch.tv/oauth2/token",
|
||||||
null,
|
null,
|
||||||
@@ -31,35 +38,42 @@ function startAuthServer() {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
accessToken = tokenRes.data.access_token;
|
const accessToken = tokenRes.data.access_token;
|
||||||
|
const refreshToken = tokenRes.data.refresh_token;
|
||||||
|
const expiresIn = tokenRes.data.expires_in;
|
||||||
|
|
||||||
console.log("🔐 Twitch user token acquired!");
|
// Store tokens globally in auth system
|
||||||
|
setTokens(accessToken, refreshToken);
|
||||||
|
|
||||||
res.end("Auth successful! You can close this tab.");
|
// Start auto-refresh loop
|
||||||
|
startAutoRefresh();
|
||||||
|
|
||||||
|
console.log("🔐 Twitch authentication successful");
|
||||||
|
console.log(`⏱ Token expires in: ${expiresIn}s`);
|
||||||
|
|
||||||
|
res.writeHead(200, { "Content-Type": "text/plain" });
|
||||||
|
res.end("Authentication successful. You can close this window.");
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err.response?.data || err.message);
|
console.error("OAuth token exchange failed:", err.response?.data || err.message);
|
||||||
res.end("Auth failed");
|
|
||||||
|
res.writeHead(500);
|
||||||
|
res.end("Authentication failed. Check terminal logs.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(3000, () => {
|
server.listen(3000, () => {
|
||||||
console.log("\n👉 Open this URL to login:\n");
|
const authUrl =
|
||||||
|
"https://id.twitch.tv/oauth2/authorize" +
|
||||||
const url =
|
|
||||||
`https://id.twitch.tv/oauth2/authorize` +
|
|
||||||
`?client_id=${config.clientId}` +
|
`?client_id=${config.clientId}` +
|
||||||
`&redirect_uri=http://localhost:3000/callback` +
|
`&redirect_uri=http://localhost:3000/callback` +
|
||||||
`&response_type=code` +
|
`&response_type=code` +
|
||||||
`&scope=channel:read:redemptions`;
|
`&scope=channel:read:redemptions`;
|
||||||
|
|
||||||
console.log(url + "\n");
|
console.log("\n👉 Twitch Login URL:");
|
||||||
|
console.log(authUrl + "\n");
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
|
||||||
getToken: () => accessToken,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { startAuthServer };
|
module.exports = { startAuthServer };
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
const config = require("../config");
|
const { loadRewards, getReward } = require("../rewards");
|
||||||
|
|
||||||
const hydrate = require("../rewards/hydrate");
|
loadRewards();
|
||||||
const sound = require("../rewards/sound");
|
|
||||||
|
|
||||||
const seen = new Set();
|
|
||||||
|
|
||||||
function handleEvent(payload) {
|
function handleEvent(payload) {
|
||||||
const { subscription, event } = payload;
|
const { subscription, event } = payload;
|
||||||
@@ -15,24 +12,18 @@ function handleEvent(payload) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (seen.has(event.id)) return;
|
const rewardName = event.reward.title;
|
||||||
seen.add(event.id);
|
|
||||||
setTimeout(() => seen.delete(event.id), 60000);
|
|
||||||
|
|
||||||
console.log(`🎁 ${event.user_name} → ${event.reward.title}`);
|
console.log(`🎁 ${event.user_name} → ${rewardName}`);
|
||||||
|
|
||||||
switch (event.reward.id) {
|
const reward = getReward(rewardName);
|
||||||
case config.rewards.HYDRATE:
|
|
||||||
hydrate(event);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case config.rewards.SOUND:
|
if (!reward) {
|
||||||
sound(event);
|
console.log("Unknown reward:", rewardName);
|
||||||
break;
|
return;
|
||||||
|
|
||||||
default:
|
|
||||||
console.log("Unknown reward:", event.reward.id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reward(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { handleEvent };
|
module.exports = { handleEvent };
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
const config = require("../config");
|
const config = require("../config");
|
||||||
const { getAppToken } = require("./auth");
|
const { getAccessToken } = require("./token");
|
||||||
|
|
||||||
async function createSubscription(sessionId) {
|
async function createSubscription(sessionId) {
|
||||||
try {
|
try {
|
||||||
const token = await getAppToken();
|
const token = getAccessToken();
|
||||||
|
|
||||||
await axios.post(
|
const res = await axios.post(
|
||||||
"https://api.twitch.tv/helix/eventsub/subscriptions",
|
"https://api.twitch.tv/helix/eventsub/subscriptions",
|
||||||
{
|
{
|
||||||
type: "channel.channel_points_custom_reward_redemption.add",
|
type: "channel.channel_points_custom_reward_redemption.add",
|
||||||
@@ -30,10 +30,7 @@ async function createSubscription(sessionId) {
|
|||||||
|
|
||||||
console.log("📡 Subscription created");
|
console.log("📡 Subscription created");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(
|
console.error("❌ Subscription error:", err.response?.data || err.message);
|
||||||
"❌ Subscription error:",
|
|
||||||
err.response?.data || err.message
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
52
src/twitch/token.js
Normal file
52
src/twitch/token.js
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
const axios = require("axios");
|
||||||
|
const config = require("../config");
|
||||||
|
|
||||||
|
let accessToken = null;
|
||||||
|
let refreshToken = null;
|
||||||
|
|
||||||
|
function setTokens(initialAccess, initialRefresh) {
|
||||||
|
accessToken = initialAccess;
|
||||||
|
refreshToken = initialRefresh;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAccessToken() {
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function refreshAccessToken() {
|
||||||
|
if (!refreshToken) return;
|
||||||
|
|
||||||
|
const res = await axios.post(
|
||||||
|
"https://id.twitch.tv/oauth2/token",
|
||||||
|
null,
|
||||||
|
{
|
||||||
|
params: {
|
||||||
|
grant_type: "refresh_token",
|
||||||
|
refresh_token: refreshToken,
|
||||||
|
client_id: config.clientId,
|
||||||
|
client_secret: config.clientSecret,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
accessToken = res.data.access_token;
|
||||||
|
refreshToken = res.data.refresh_token;
|
||||||
|
|
||||||
|
console.log("🔄 Twitch token refreshed");
|
||||||
|
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
function startAutoRefresh() {
|
||||||
|
// refresh every 3 hours (safe before expiry)
|
||||||
|
setInterval(() => {
|
||||||
|
refreshAccessToken().catch(console.error);
|
||||||
|
}, 3 * 60 * 60 * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
setTokens,
|
||||||
|
getAccessToken,
|
||||||
|
refreshAccessToken,
|
||||||
|
startAutoRefresh,
|
||||||
|
};
|
||||||
@@ -25,6 +25,7 @@ function connect(url = reconnectUrl) {
|
|||||||
payload.session.reconnect_url ||
|
payload.session.reconnect_url ||
|
||||||
"wss://eventsub.wss.twitch.tv/ws";
|
"wss://eventsub.wss.twitch.tv/ws";
|
||||||
|
|
||||||
|
// IMPORTANT: must be immediate
|
||||||
await createSubscription(sessionId);
|
await createSubscription(sessionId);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user