Updated list of commands to v4.3. Added groups & permissions. Added connections. Updated redemptions and actions to v4.3.

This commit is contained in:
Tom
2024-08-14 20:33:40 +00:00
parent 6548ce33e0
commit b92529d8c0
51 changed files with 3910 additions and 799 deletions

View File

@ -1,50 +0,0 @@
type Listener = (value: any) => void;
type Topics = {
[name: string]: Listener[];
};
export const createPubSub = () => {
let topics: Topics = {};
let destroyed = false;
const getTopic = (name: string) => {
if (!topics[name]) {
topics[name] = [];
}
return topics[name];
};
return {
subscribe(topic: string, fn: Listener) {
const listeners = getTopic(topic);
listeners.push(fn);
const unsubscribe = () => {
const index = listeners.indexOf(fn);
listeners.splice(index, 1);
};
return unsubscribe;
},
publish(topic: string, value: any) {
const listeners = getTopic(topic);
const currentListeners = listeners.slice();
currentListeners.forEach((listener) => {
if (!destroyed) {
listener(value);
}
});
},
destroy() {
topics = {};
destroyed = true;
},
};
};

79
lib/twitch.ts Normal file
View File

@ -0,0 +1,79 @@
import axios from 'axios'
import { db } from "@/lib/db"
export async function TwitchUpdateAuthorization(userId: string) {
try {
const connection = await db.twitchConnection.findFirst({
where: {
userId
}
})
if (!connection) {
return null
}
try {
const { expires_in }: { client_id:string, login:string, scopes:string[], user_id:string, expires_in:number } = (await axios.get("https://id.twitch.tv/oauth2/validate", {
headers: {
Authorization: 'OAuth ' + connection.accessToken
}
})).data;
if (expires_in > 3600) {
const data = await db.twitchConnection.findFirst({
where: {
userId
}
})
const dataFormatted = {
user_id: userId,
access_token: data?.accessToken,
refresh_token: data?.refreshToken,
broadcaster_id: connection.broadcasterId,
expires_in
}
return dataFormatted;
}
} catch (error) {
}
// Post to https://id.twitch.tv/oauth2/token
const token: { access_token:string, expires_in:number, refresh_token:string, token_type:string, scope:string[] } = (await axios.post("https://id.twitch.tv/oauth2/token", {
client_id: process.env.TWITCH_BOT_CLIENT_ID,
client_secret: process.env.TWITCH_BOT_CLIENT_SECRET,
grant_type: "refresh_token",
refresh_token: connection.refreshToken
})).data
// Fetch values from token.
const { access_token, expires_in, refresh_token, token_type } = token
if (!access_token || !refresh_token || token_type !== "bearer") {
return null
}
await db.twitchConnection.update({
where: {
userId
},
data: {
accessToken: access_token,
refreshToken: refresh_token
}
})
const data = {
user_id: userId,
access_token,
refresh_token,
broadcaster_id: connection.broadcasterId,
expires_in
}
return data
} catch (error) {
console.log("[ACCOUNT]", error);
return null
}
}