Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

/iskirilive command #2

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"discord.enabled": true
}
102 changes: 90 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"test": "nodemon --ext ts --exec ts-node src/index.ts",
"build": "tsc --outDir target/ src/index.ts",
"start": "node target/",
"lint": "eslint ."
"lint": "eslint src/"
},
"repository": {
"type": "git",
Expand All @@ -20,7 +20,10 @@
"homepage": "https://github.com/kiriDevs/ksmpdcbot#readme",
"description": "",
"dependencies": {
"@discordjs/builders": "^0.12.0",
"@discordjs/rest": "^0.3.0",
"axios": "^0.25.0",
"discord-api-types": "^0.26.1",
"discord.js": "^13.6.0",
"dotenv": "^15.0.0"
},
Expand Down
7 changes: 7 additions & 0 deletions src/RequestEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type DoneRequestEvent = { type: "done", data: object };
type RetryRequestEvent = { type: "retry" };
type ErrorRequestEvent = { type: "error", error: Error|string };

type RequestEvent = DoneRequestEvent | RetryRequestEvent | ErrorRequestEvent;

export default RequestEvent;
172 changes: 172 additions & 0 deletions src/Twitch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import axios from "axios";
import RequestEvent from "./RequestEvent";
import TwitchRoutes from "./TwitchRoutes";
const { get, post } = axios;

export type TTwitchSession =
{ authenticated: true, token: string }
| { authenticated: false, token: null }

export interface ITwitchClient {
credentials: {
cid: string,
secret: string
},
session: TTwitchSession,
authenticate: () => void,
deauthenticate: () => void;
get: (
url: string,
params: object,
cb: (event: RequestEvent) => void,
isRetry?: boolean
) => void,
post: (
url: string,
params: object,
cb: (event: RequestEvent) => void,
isRetry?: boolean
) => void
}

export const TwitchClient = class implements ITwitchClient {
static API_SCOPES = "";

credentials: ITwitchClient["credentials"];
session: TTwitchSession;

constructor(appId: string, clientSecret: string) {
this.credentials = {
cid: appId,
secret: clientSecret
};
this.session = {
authenticated: false,
token: null
};
}

authenticate = () => {
post(TwitchRoutes.auth.login as string, null, { params: {
client_id: this.credentials.cid,
client_secret: this.credentials.secret,
grant_type: "client_credentials"
}}).then((res) => {
this.session = { authenticated: true, token: res.data.access_token };
console.log("Authenticated with TwitchAPI!");
}).catch((err) => {
console.error("Error authenticating with TwitchAPI:");
console.error(err.message);
});
};

deauthenticate = () => {
post(TwitchRoutes.auth.revoke as string, null, { params: {
client_id: this.credentials.cid,
token: this.session.token
}}).then((res) => {
this.session = { authenticated: false, token: null };
console.log("Revoked token!");
console.log(res.data);
}).catch((err) => {
console.error("Error revoking TwitchAPI Token:");
console.error(err/*.message*/);
});
};

private _get = async (url: string, params: object) => {
if (!this.session.authenticated) {
console.error("ERROR: Tried making _GET request while not authenticated!");
return null;
}

return get(url, {
headers: {
Authorization: `Bearer ${this.session.token}`,
"Client-Id": this.credentials.cid
},
params: params
});
};

private _post = (url: string, params: object) => {
if (!this.session.authenticated) {
console.error("ERROR: Tried making _POST request while not authenticated!");
return null;
}

return post(url, {
headers: {
Authorization: `Bearer ${this.session.token}`,
"Client-Id": this.credentials.cid
},
params: params
});
};

get = (url: string, params: object, cb: (event: RequestEvent) => void, isRetry = false) => {
this._get(url, params).then((res) => {
if (res == null) {
cb({ type: "error", error: "F @axios" });
return;
}

if (res.data == null) {
cb({ type: "error", error: "F @Twitch" });
return;
}

cb({ type: "done", data: res.data });
}).catch((err) => {
if (err.response.status in [401,403]) {
if (isRetry) {
cb({ type: "error", error: "Can't authenticate." });
return;
}

cb({ type: "retry" });
this.authenticate();
setTimeout(() => { this.get(url, params, cb, true); }, 3000);
} else {
console.log(err.response);
cb({ type: "error", error: "?" });
}
});
};

post = (url: string, params: object, cb: (event: RequestEvent) => void, isRetry = false) => {
const postReq = this._post(url, params);

if (postReq == null) {
cb({ type: "error", error: "F @axios" });
return;
}

postReq.then((res) => {
if (res == null) {
cb({ type: "error", error: "F @axios" });
return;
}

if (res.data == null) {
cb({ type: "error", error: "F @Twitch" });
return;
}

cb({ type: "done", data: res.data });
}).catch((err) => {
if (err.response.data.status in [401,403]) {
if (isRetry) {
cb({ type: "error", error: "Can't authenticate." });
return;
}

cb({ type: "retry" });
this.authenticate();
setTimeout(() => { this.post(url, params, cb, true); }, 3000);
} else {
cb({ type: "error", error: "F @Twitch" });
}
});
};
};
Loading