-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
121 lines (104 loc) · 3.47 KB
/
Copy pathbot.js
File metadata and controls
121 lines (104 loc) · 3.47 KB
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
require("dotenv").config();
const axios = require("axios");
const tmi = require("tmi.js");
const _GamerApi = require("./lib/gamerApi");
const _ChatCommands = require("./lib/chatCommands");
// Twitch bot options
const opts = {
options: { debug: true },
identity: {
username: process.env.TWITCH_BOT_USERNAME,
password: process.env.TWITCH_OAUTH_TOKEN,
},
channels: [process.env.TWITCH_CHANNEL],
};
// Create a client with our options
const client = new tmi.client(opts);
const gamerApi = new _GamerApi();
let chatCommands;
//console.log(process.argv);
if (process.argv[2] !== undefined) {
if (process.argv[2] === "test") {
chatCommands = new _ChatCommands(null, gamerApi);
onMessageHandler(process.env.TWITCH_CHANNEL, {mod:true}, process.argv[3]);
return;
}
const queryParams = {};
if (process.argv[3] !== undefined) {
const params = new URLSearchParams(process.argv[3]);
for (const [key, value] of params.entries()) {
queryParams[key] = value;
}
}
const response = gamerApi.fetch(process.argv[2], queryParams).then((data) => {
//console.log(data);
});
return;
}
chatCommands = new _ChatCommands(client, gamerApi);
// Register our event handlers (defined below)
client.on("message", onMessageHandler);
client.on("connected", onConnectedHandler);
// Connect to Twitch
client
.connect()
.catch((error) => console.error("Failed to connect to Twitch chat:", error));
// Called every time a message comes in
function onMessageHandler(channel, context, msg, self) {
if (self) {
return;
} // Ignore messages from the bot
// Remove whitespace from chat message
const commandName = msg.trim();
if (context.mod) {
// console.log(`${context.username} is a mod`);
}
//console.log(context);
// If the command starts with !, handle it
if (msg.startsWith("!")) {
const regexpCommand = /^!([a-zA-Z0-9]+)(?:\W+)?(.*)?/;
const matchResult = msg.match(regexpCommand);
const [raw, command, argument] = matchResult ? matchResult : [];
if (command) {
chatCommands.handle(channel, command, argument, context);
}
}
}
async function getAppAccessToken() {
try {
const response = await axios.post(`https://id.twitch.tv/oauth2/token?client_id=${process.env.TWITCH_CLIENT_ID}&client_secret=${process.env.TWITCH_CLIENT_SECRET}&grant_type=client_credentials`);
return response.data.access_token;
} catch (error) {
console.error('Error getting app access token:', error);
return null;
}
}
setInterval(() => {
getAppAccessToken()
.then(appAccessToken => {
return axios.get(`https://api.twitch.tv/helix/streams?user_login=${process.env.TWITCH_CHANNEL}`, {
headers: {
'Client-ID': process.env.TWITCH_CLIENT_ID,
'Authorization': `Bearer ${appAccessToken}`
}
});
})
.then(response => {
//console.log(response.data.data[0])
const stream = response.data.data[0];
if (stream) {
//console.log(`Stream is live: ${stream.title}`);
chatCommands.findNewResults(process.env.TWITCH_CHANNEL);
} else {
//console.log('Stream is offline');
}
//chatCommands.findNewResults(process.env.TWITCH_CHANNEL);
})
.catch(error => {
console.error('Error fetching channel status:', error);
});
}, 45000);
// Called every time the bot connects to Twitch chat
function onConnectedHandler(addr, port) {
console.log(`* Connected to ${addr}:${port}`);
}