forked from callowcreation/basic-twitch-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
113 lines (93 loc) · 3.06 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { config } from "dotenv";
import TwitchOAuth from './src/twitch-oauth';
import express from 'express';
import crypto from 'crypto';
if (module === require.main) {
config();
const app = express();
const buffer = crypto.randomBytes(16);
const state = buffer.toString('hex');
const twitchOAuth = new TwitchOAuth({
client_id: process.env.CLIENT_ID as string,
client_secret: process.env.CLIENT_SECRET as string,
redirect_uri: process.env.REDIRECT_URI as string,
scopes: [
'user:edit:broadcast',
'moderation:read'
]
}, state);
const error_message = 'Something went wrong <a href="/home">return home</a><br />Read more in the server terminal'
const broadcaster_id = '75987197';
app.get('/', (_req, res) => {
res.status(200).send(`<a href="/authorize">Authorize</a>`);
});
app.get('/home', (_req, res) => {
res.status(200).send(
`<a href="/extensions">Example Extensions (GET request)</a><br />
<a href="/moderation">Example AutoMod (POST request)</a><br />
<a href="/tags">Example Tags (PUT request)</a><br /><br />`
);
});
app.get('/extensions', async (_req, res) => {
const url: string = `https://api.twitch.tv/helix/users/extensions?user_id=${broadcaster_id}`;
try {
const json = await twitchOAuth.getEndpoint(url);
res.status(200).json(json);
} catch (error) {
console.error(error);
res.send(error_message);
}
});
app.get('/moderation', async (_req, res) => {
const url: string = `https://api.twitch.tv/helix/moderation/enforcements/status?broadcaster_id=${broadcaster_id}`;
const data = [
{ msg_id: '0', msg_text: 'I killing this', user_id: '101223367' },
{ msg_id: '1', msg_text: 'that was a death blow', user_id: '75987197' }
];
try {
const json = await twitchOAuth.postEndpoint(url, { data });
res.status(200).json(json);
} catch (error) {
console.error(error);
res.send(error_message);
}
});
app.get('/tags', async (_req, res) => {
const url: string = `https://api.twitch.tv/helix/streams/tags?broadcaster_id=${broadcaster_id}`;
const tag_ids = [
'621fb5bf-5498-4d8f-b4ac-db4d40d401bf',
'79977fb9-f106-4a87-a386-f1b0f99783dd'
];
try {
const json = await twitchOAuth.putEndpoint(url, { tag_ids });
res.status(200).json(json)
} catch (error) {
console.error(error);
res.send(error_message);
}
});
app.get('/auth-callback', async (req, res) => {
try {
const code: string = req.query.code as string;
const state: string = req.query.state as string;
twitchOAuth.confirmState(state);
await twitchOAuth.fetchToken(code);
res.redirect('/home');
} catch (err) {
console.error(err);
res.send(error_message);
}
});
// ---- Initiate the oauth process
// ---- Or below app.listen(process.env.PORT...
app.get('/authorize', (_req, res) => {
res.redirect(twitchOAuth.authorizeUrl);
});
// ---- Initiate the oauth process
// ---- Or above app.get('/authorize'...
app.listen(process.env.PORT || 4000, () => {
console.log(`App listening on port ${process.env.PORT || 4000}`);
const open = require('open');
open(twitchOAuth.authorizeUrl);
});
}