-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWaldyBotTwitch.js
166 lines (148 loc) · 6.89 KB
/
WaldyBotTwitch.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
165
166
/* eslint-disable import/no-extraneous-dependencies, import/no-unresolved, no-console */
// Imported Node Modules
const TwitchJs = require('twitch-js'); // TwitchJS https://www.npmjs.com/package/twitch-js
var fs = require('fs');
var globalVarsObjs = require('./Objects/GlobalVarsObjects.js');
var testAuthNotCommitedFile = require('./testauth.js');
// README: Instructions
// Go to './Objects/Auth.js' to setup YOUR bot's USERNAME + PASSWORD.
// Alongside the other nessecary authentication setup. Ie twitch channel selection, on joining, etc.
// Dev Notes:
// Youtube guide used: https://www.youtube.com/watch?v=K6N9dSMb7sM
// Some code taken from: https://dev.twitch.tv/docs/irc/. Specifically official Twitch example chatbot. Under "Step:2 Sample Code".
// Twitch IRC guide used: https://blog.bashtech.net/a-guide-to-twitch-irc/
// TwitchJS:
// https://twitch-apis.github.io/twitch-js/
// getting started: https://github.com/twitch-apis/twitch-js/blob/master/docs/HomeGettingStarted.md
// Also, TwitchJS https://twitch-apis.github.io/twitch-js/docs/getting-started
// TwitchJS how to setup: https://github.com/twitch-apis/twitch-js/blob/6c2d99d6ed2522cc958d18cb2e1ffe37ce71781b/docs/Examples.md
// Working example script: https://gist.github.com/JeffreyBodin/31ed9fdafe84535b4d54571c88f895dc.js
// Go here for a reference to the twitch userstate object (Chat users info): https://github.com/twitch-apis/twitch-js/blob/HEAD/docs/Chat/Configuration.md
// Global Vars
var packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
var waldyBotVersion = packageJson["version"];
const options = globalVarsObjs.authenticationObject.options;
const token = options.identity.password;
const username = options.identity.username;
const twitchJs = new TwitchJs.default({ token, username });
const optionsChannelsArray = globalVarsObjs.authenticationObject.currentTwitchChannels;
// Note: var currentTwitchChannels = optionsObjChannelsArrayClean;
const channelString = globalVarsObjs.authenticationObject.currentChannelString;
// Note: var currentChannelString = currentTwitchChannels[0];
// Global Objects
// STARTUP:
// See package.json to edit startup command. At:""start": "node WaldyBotTwitch.js","
// Default call "node WaldyBotTwitch.js" to start bot in powershell/command prompt etc..
twitchJs.chat.connect().then(globalUserState => {
// JOINING CHANNEL
twitchJs.chat.join(channelString).then(channelState => {
console.log(channelState);
// Do stuff with channelState...
});
// Logs:
// Initial Startup Logs
// PLACERHOLDER DO ADD THE FOLLOWING LIKE USED TO HAVE WITH TMI
// https://github.com/twitch-apis/twitch-js/blob/master/docs/Chat/Methods.md
twitchJs.chat.on('JOIN', (channel, userstate, message) => {
// On chat connection. Msgs logged to console:
console.log(twitchJs.chat.getChannels()); // Returns: [] pre-channel connection. Returns [ '#hdbeasta' ] post-channel connection.
//console.log(twitchJs.chat.readyState()); // Returns: "CONNECTING", "OPEN", "CLOSING" or "CLOSED".
// Isnt working.
// Error: .readyState() is aparently not defined/or isnt a function??
// To Do: Find up to date "readyState" function for use here.
console.log(`v` + waldyBotVersion); // Current version
console.log(`Ready....`);
}
);
// Continued Logs:
const log = msg => console.log(msg);
twitchJs.chat.on(twitchJs.chatConstants.EVENTS.ALL, log); // Return all events. So every chat message is logged, alongside said msg's data.
// Listen/Log to console incoming flagged chat. (Ie commands)
//twitchJs.chat.connect().then(responseLog => {
// console.log(responseLog);
// //console.log(
// // `Message "${message}" received from ${userstate['displayName']}`,
// //);
//});
// IN PROGRESS CONTINUE THIS TOMMORROW......... <------------------------------------- HERE
twitchJs.chat.on('*', (message, userstate) => {
// Returns: Message "[object Object]" received from undefined
console.log(`Message "${message}" received from ${username['displayName']}`);
});
twitchJs.chat.on('*', (message, userstate) => {
// Returns: Message "undefined" received from undefined
console.log(`Message "${message[message]}" received from ${username['username']}`);
});
// Event Handlers: (Defined Below)
// Listen to '*' (All Messages)
twitchJs.chat.on('*', (channel, userstate, message) => {
if (options.identity && message === 'w help') {
// If an identity was provided, respond in channel with message.
twitchJs.chat.say(channel, 'Hello world!');
}
if (message === 'w help') {
// If an identity was provided, respond in channel with message.
twitchJs.chat.say(channel, 'Hello world!');
}
});
// Listen to 'PRIVMSG' (Private Messages)
twitchJs.chat.on('PRIVMSG', privateMessage => {
// Do stuff with privateMessage ...
if (privateMessage === 'w help') {
// If an identity was provided, respond in channel with message.
twitchJs.chat.say(channel, 'Hello world!');
}
if (privateMessage === 'w help') {
// If an identity was provided, respond in channel with message.
twitchJs.chat.say(channel, 'Hello world!');
}
});
twitchJs.chat.on('PRIVMSG', (command, channel, message) => {
// Do stuff with privateMessage ...
if (message === 'w help') {
// If an identity was provided, respond in channel with message.
twitchJs.chat.say(channel, 'Hello world!');
}
if (command === 'w help') {
// If an identity was provided, respond in channel with message.
twitchJs.chat.say(channel, 'Hello world!');
}
});
// Commands:
// Event listener that will respond to "w help" messages with: "Hello world!"
// placeholder
//twitchJs.chat.on('chat', (channel, userstate, message) => {
// if (options.identity && message === 'w help') {
// // If an identity was provided, respond in channel with message.
// twitchJs.chat.say(channel, 'Hello world!');
// }
// if(message == '@waldybot') {
// twitchJs.chat.say(channel, userstate['displayName'] + " fuck you");
// }
//});
// Event will repond to 'w help' with a whisper to the user.
// placeholder
//twitchJs.chat.on('*', (message, user) =>{
// if(message === 'w help'){
// twitchJs.chat.whisper(user, message);
// }
//});
// Placeholder Command 2
});
// Noted for later how to map and join multi-channels.
//Promise.all(options.channels.map(channel => twitchJs.chat.join(channel)))
// .then(channelStates => {
// // Listen to all PRIVMSG
// chat.on('PRIVMSG', privateMessage => {
// // Do stuff with privateMessage ...
// });
//
// // Listen to PRIVMSG from #dallas ONLY
// chat.on('PRIVMSG/#dallas', privateMessage => {
// // Do stuff with privateMessage ...
// });
// // Listen to all PRIVMSG from #ronni ONLY
// chat.on('PRIVMSG/#ronni', privateMessage => {
// // Do stuff with privateMessage ...
// });
// });