-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitchToolsVX.js
164 lines (136 loc) · 5.97 KB
/
TwitchToolsVX.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const {
GetDeviceResponse,
GetTokenWithDeviceCode,
HandleError,
SpamCaule
} = require("./utils/utils");
const TwitchModerationAPI = require("twitch-moderation-tools");
const axios = require("axios");
const { checkWordIlicita } = require("swearguard-vx");
class TwtchToolsVX {
constructor(authTokenBot, clientId) {
this.clientId = clientId;
this.authTokenBot = authTokenBot;
this.headers = {
'Authorization': `Bearer ${authTokenBot}`,
'Client-Id': clientId,
'Content-Type': 'application/json'
};
this.TwitchModeration = new TwitchModerationAPI(authTokenBot, clientId);
}
async SetTitleLive(authTokenMain, channel, title, botChannel = false) {
try {
const idChannel = await this.TwitchModeration.GetInfoUser(channel);
const updateLiveStreamTitle = async (authToken) => {
try {
const response = await axios.patch(`https://api.twitch.tv/helix/channels?broadcaster_id=${idChannel[0].user_id}`, {
title: title
}, {
headers: {
'Client-ID': this.clientId,
'Authorization': `Bearer ${authToken}`
}
});
return response ? `Changed live stream title for ${channel}` : false;
} catch (error) {
return HandleError(error, error.response?.data?.message);
}
};
return botChannel ? updateLiveStreamTitle(this.authTokenBot) : updateLiveStreamTitle(authTokenMain);
} catch (error) {
return HandleError(error, error.response?.data?.message);
}
}
async SetTegLive(authTokenMain, channel, teg, botChannel = false) {
try {
const idChannel = await this.TwitchModeration.GetInfoUser(channel);
const gameId = await this.TwitchModeration.GetGameId(teg);
const updateLiveStreamTeg = async (authToken) => {
try {
const response = await axios.patch(`https://api.twitch.tv/helix/channels?broadcaster_id=${idChannel[0].user_id}`, {
game_id: gameId.id
}, {
headers: {
'Client-ID': this.clientId,
'Authorization': `Bearer ${authToken}`
}
});
return response ? `Changed live stream tag for ${channel}` : false;
} catch (error) {
return HandleError(error, error.response.data.message);
}
}
return botChannel ? updateLiveStreamTeg(this.authTokenBot) : updateLiveStreamTeg(authTokenMain);
} catch (error) {
return HandleError(error, error.response?.data?.message);
}
}
async GetTokenTwitchDevice(waitTimeInSeconds, scopes = []) {
if (scopes.length > 0) {
const device = await GetDeviceResponse(this.clientId, scopes);
if (device) {
console.log(device);
await new Promise(resolve => setTimeout(resolve, waitTimeInSeconds));
const tokenDevice = await GetTokenWithDeviceCode(device, this.clientId);
return tokenDevice
}
}
return `Not found scopes`;
}
async RefreshToken(refreshToken, clientSecret) {
try {
const response = await axios.post('https://id.twitch.tv/oauth2/token', {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
client_id: this.clientId,
client_secret: clientSecret,
grant_type: 'refresh_token',
refresh_token: refreshToken
});
return response ? response.data : HandleError(null, "Erro in refresh token");
} catch (error) {
return HandleError(error, error.response.data.message);
}
}
async RemoveSpamCaules(channel, botModerator, message, tags) {
try {
return SpamCaule(message) ? this.TwitchModeration.RemoveMessageChat(channel, botModerator, tags.id) : HandleError(null, "Erro ao remover a mensagem");
} catch (error) {
HandleError(error, null);
}
}
async BanSpamCaules(channel, botModerator, userBanned, message) {
try {
return SpamCaule(message) ? this.TwitchModeration.BanUser(channel, botModerator, userBanned, "Spam caule.") : HandleError(null, "Erro ao remover a mensagem");
} catch (error) {
HandleError(error, null);
}
}
async RemoveMessageSwearWord(channel, botModerator, tags, message) {
try {
return checkWordIlicita(message) ? this.TwitchModeration.RemoveMessageChat(channel, botModerator, tags.id) : HandleError(null, "Erro ao remover a mensagem");
} catch (error) {
return HandleError(error, null);
}
}
async BanSwearWord(channel, botModerator, userBanned, message) {
try {
return checkWordIlicita(message) ? this.TwitchModeration.BanUser(channel, botModerator, userBanned, "SwearWord") : HandleError(null, "Erro ao remover a mensagem");
} catch (error) {
return HandleError(error, null);
}
}
async StreamerIsLive(streamer) {
try {
const response = await axios.get(`https://api.twitch.tv/helix/streams?user_login=${streamer}`,
{
headers: this.headers
});
return response && response.data.data.length > 0;
} catch (error) {
return HandleError(error, null);
}
}
}
module.exports = TwtchToolsVX;