Skip to content

Commit 32278f7

Browse files
Initial commit
0 parents  commit 32278f7

File tree

106 files changed

+29697
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+29697
-0
lines changed

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/out
6+
/.pnp
7+
.pnp.js
8+
9+
# testing
10+
/coverage
11+
12+
# production
13+
/build
14+
15+
# misc
16+
.DS_Store
17+
.env.local
18+
.env.development.local
19+
.env.test.local
20+
.env.production.local
21+
22+
npm-debug.log*
23+
yarn-debug.log*
24+
yarn-error.log*

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Getting Started with Create React App
2+
3+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4+
5+
## Available Scripts
6+
7+
In the project directory, you can run:
8+
9+
### `npm start`
10+
11+
Runs the app in the development mode.\
12+
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13+
14+
The page will reload when you make changes.\
15+
You may also see any lint errors in the console.
16+
17+
### `npm test`
18+
19+
Launches the test runner in the interactive watch mode.\
20+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21+
22+
### `npm run build`
23+
24+
Builds the app for production to the `build` folder.\
25+
It correctly bundles React in production mode and optimizes the build for the best performance.
26+
27+
The build is minified and the filenames include the hashes.\
28+
Your app is ready to be deployed!
29+
30+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31+
32+
### `npm run eject`
33+
34+
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
35+
36+
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37+
38+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39+
40+
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41+
42+
## Learn More
43+
44+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45+
46+
To learn React, check out the [React documentation](https://reactjs.org/).
47+
48+
### Code Splitting
49+
50+
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51+
52+
### Analyzing the Bundle Size
53+
54+
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55+
56+
### Making a Progressive Web App
57+
58+
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59+
60+
### Advanced Configuration
61+
62+
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63+
64+
### Deployment
65+
66+
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67+
68+
### `npm run build` fails to minify
69+
70+
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)

electron/bot/bot.js

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
const { StaticAuthProvider } = require('@twurple/auth');
2+
const { ChatClient } = require('@twurple/chat');
3+
const { PubSubClient } = require('@twurple/pubsub');
4+
5+
const EventQueue = require('./components/base/eventQueue');
6+
7+
const readline = require('readline');
8+
9+
const requestPlugin = require('./botPlugins/requests');
10+
const deathCounterPlugin = require('./botPlugins/deathCounter');
11+
const cameraObscura = require('./botPlugins/cameraObscura');
12+
const modTools = require('./botPlugins/modTools');
13+
14+
const versionNumber = "3.0b";
15+
16+
/*
17+
* INDEXES
18+
*/
19+
20+
let client, pubSubClient;
21+
let listeners = [];
22+
let cooldowns = {};
23+
let units = {
24+
ms: 1,
25+
s: 1000,
26+
m: 60 * 1000,
27+
h: 60 * 60 * 1000
28+
}
29+
30+
const performCustomCommand = (command, {type, coolDown, target}, botContext) => {
31+
console.log("COOLDOWN LEFT: " + cooldowns[command] - Date.now());
32+
if (cooldowns[command] && cooldowns[command] - Date.now() <= 0) {
33+
console.log("COOLDOWN OVER");
34+
delete cooldowns[command];
35+
} else if (cooldowns[command] && cooldowns[command] - Date.now() > 0) {
36+
throw "Custom command '" + command + "' is on cooldown until " + new Date(cooldowns[command]);
37+
}
38+
39+
let match = coolDown.match(/(\d+)(ms|s|m|h)/);
40+
if (!match) {
41+
throw "Custom command has invalid cooldown string";
42+
}
43+
44+
console.log("COOLDOWN PARSED: " + match[1] + " " + match[2]);
45+
46+
cooldowns[command] = Date.now() + parseInt(match[1]) * units[match[2]];
47+
48+
console.log("COOLDOWN ENDS AT: " + cooldowns[command]);
49+
50+
if (type === "VIDEO") {
51+
let {url, volume, name, chromaKey} = botContext.botConfig.videoPool.find(video => video.id === target);
52+
53+
EventQueue.sendEvent({
54+
type,
55+
targets: ["panel"],
56+
eventData: {
57+
message: [''],
58+
mediaName: name,
59+
url,
60+
chromaKey,
61+
volume,
62+
results: {}
63+
}
64+
});
65+
} else if (type === "AUDIO") {
66+
let {url, volume, name} = botContext.botConfig.audioPool.find(audio => audio.id === target);
67+
68+
EventQueue.sendEvent({
69+
type,
70+
targets: ["panel"],
71+
eventData: {
72+
message: [''],
73+
mediaName: name,
74+
url,
75+
volume,
76+
results: {}
77+
}
78+
});
79+
}
80+
}
81+
82+
// Define configuration options for chat bot
83+
const startBot = async (botConfig) => {
84+
try {
85+
let {accessToken, clientId, twitchChannel, devMode} = botConfig;
86+
let botContext = {};
87+
88+
let plugins = [deathCounterPlugin, requestPlugin, cameraObscura, modTools];
89+
90+
const onConsoleCommand = (command) => {
91+
client.say(twitchChannel, command);
92+
}
93+
94+
// Called every time a message comes in
95+
const onMessageHandler = async (target, context, msg) => {
96+
let commands = {};
97+
plugins.forEach((plugin) => {
98+
commands = {...commands, ...plugin.commands};
99+
});
100+
101+
const caller = {
102+
id: context["user-id"],
103+
name: context.username
104+
}
105+
106+
// Remove whitespace from chat message
107+
const command = msg.trim();
108+
const [commandName, ...text] = command.split(" ");
109+
const tokens = command.split(" ");
110+
111+
const commandText = text.join(" ");
112+
113+
// Handle battle commands here
114+
if (command.startsWith("!")) {
115+
context.command = command;
116+
context.commandName = commandName;
117+
context.text = commandText;
118+
context.tokens = tokens;
119+
context.caller = caller;
120+
context.target = target;
121+
122+
console.log("Received command!")
123+
console.log("Tokens: " + context.tokens);
124+
125+
try {
126+
switch (context.tokens[0]) {
127+
case "!about":
128+
EventQueue.sendInfoToChat(`Streamcrabs version ${versionNumber} written by thetruekingofspace`);
129+
break;
130+
default:
131+
if (commands[context.tokens[0]]) {
132+
await commands[context.tokens[0]](context, botContext);
133+
} else if (botContext.botConfig.commands[context.tokens[0]]) {
134+
await performCustomCommand(context.tokens[0], botContext.botConfig.commands[context.tokens[0]], botContext);
135+
}
136+
}
137+
} catch (e) {
138+
console.error(e.message + ": " + e.stack);
139+
EventQueue.sendErrorToChat(new Error(e));
140+
}
141+
}
142+
}
143+
144+
// Called every time the bot connects to Twitch chat
145+
const onConnectedHandler = async () => {
146+
if (devMode) {
147+
console.log("* RUNNING IN DEV MODE");
148+
}
149+
console.log("* Connected to Twitch Chat");
150+
151+
botContext = {botConfig, plugins, client};
152+
153+
// Initialize all plugins
154+
for (let plugin of plugins) {
155+
plugin.init(botContext);
156+
}
157+
158+
// Start queue consumer
159+
await EventQueue.startEventListener(botContext);
160+
161+
// Announce restart
162+
EventQueue.sendInfoToChat(`Streamcrabs version ${versionNumber} is online. All systems nominal.`);
163+
}
164+
165+
const onRaid = async (channel, username, viewers) => {
166+
console.log("RAID DETECTED: " + channel + ":" + username + ":" + viewers);
167+
let raidContext = {channel, username, viewers};
168+
169+
// Run raid function of each plugin
170+
for (let plugin of plugins) {
171+
if (plugin.raidHook) {
172+
plugin.raidHook(raidContext, botContext);
173+
}
174+
}
175+
}
176+
177+
const onSubscription = async (subMessage) => {
178+
try {
179+
// Run through subscription plugin hooks
180+
for (let plugin of plugins) {
181+
if (plugin.subscriptionHook) {
182+
plugin.subscriptionHook(subMessage, botContext);
183+
}
184+
}
185+
} catch (error) {
186+
console.error("SUB FAILURE: " + error);
187+
}
188+
}
189+
190+
const onBits = async (bitsMessage) => {
191+
try {
192+
// Run through bit plugin hooks
193+
for (let plugin of plugins) {
194+
if (plugin.bitsHook) {
195+
plugin.bitsHook(bitsMessage, botContext);
196+
}
197+
}
198+
} catch (error) {
199+
console.error("BIT FAILURE: " + error);
200+
}
201+
}
202+
203+
const onRedemption = async (redemptionMessage) => {
204+
try {
205+
// Run through redemption plugin hooks
206+
for (let plugin of plugins) {
207+
if (plugin.redemptionHook) {
208+
plugin.redemptionHook(redemptionMessage, botContext);
209+
}
210+
}
211+
} catch (error) {
212+
console.error("REDEMPTION FAILURE: " + error);
213+
}
214+
}
215+
216+
const rl = readline.createInterface({
217+
input: process.stdin,
218+
output: process.stdout,
219+
terminal: true
220+
});
221+
222+
const authProvider = new StaticAuthProvider(clientId, accessToken, ["chat:read", "chat:edit", "channel:read:redemptions", "channel:read:subscriptions", "bits:read", "channel_subscriptions"], "user");
223+
client = new ChatClient({authProvider, channels: [twitchChannel]});
224+
pubSubClient = new PubSubClient();
225+
const userId = await pubSubClient.registerUserListener(authProvider);
226+
227+
rl.on('line', onConsoleCommand);
228+
229+
// Register our event handlers (defined below)
230+
client.onMessage((channel, username, message) => {
231+
onMessageHandler(channel, {username, id: ""}, message);
232+
});
233+
client.onConnect(onConnectedHandler);
234+
client.onRaid((channel, username, {viewerCount}) => {onRaid(channel, username, viewerCount)});
235+
let subListener = await pubSubClient.onSubscription(userId, onSubscription);
236+
let cheerListener = await pubSubClient.onBits(userId, onBits);
237+
let redemptionListener = await pubSubClient.onRedemption(userId, onRedemption);
238+
239+
listeners = [subListener, cheerListener, redemptionListener];
240+
241+
// Connect to twitch chat and pubsub
242+
client.connect();
243+
} catch (error) {
244+
console.error(`* Failed to start bot: ${error}`);
245+
}
246+
};
247+
248+
const stopBot = () => {
249+
EventQueue.stopEventListener();
250+
client.quit();
251+
listeners.forEach((listener) => {
252+
listener.remove();
253+
});
254+
};
255+
256+
module.exports = {
257+
startBot,
258+
stopBot
259+
}

0 commit comments

Comments
 (0)