-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (108 loc) · 3.37 KB
/
index.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
require('dotenv').config();
const tmi = require('tmi.js');
const shuffle = require('knuth-shuffle').knuthShuffle;
const fighters = require('./fighters.js');
// Define config options
const opts = {
connections: {
reconnect: true,
secure: true,
},
identity: {
username: 'Stark-Bot',
password: process.env.OAUTH_TOKEN,
},
channels: ['VinMags16'],
};
// Create client
const client = new tmi.client(opts);
// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);
// Connect to Twitch:
client.connect();
// Key is channel name, value is array of fighter names for ironman
const ironmans = {};
function onMessageHandler(target, context, msg, self) {
if (self) return; // ignore self messages
const message = msg.trim();
if (message.substring(0, 7) != '!stark ') return;
// Parse out the command to get only the body
const content = message.substring(7, message.length);
switch (content) {
// Start ironman
case 'start':
if (!context.badges.broadcaster) {
console.log(`${context.username} not authorized to run "start"`);
return;
}
console.log(`ironman started for ${context.username}`);
ironmans[context.username] = shuffle([...fighters]);
client.say(
target,
`First 5: ${ironmans[context.username]
.slice(0, 5)
.toString()
.replace(/,/g, ', ')}`,
);
break;
// Won match, get next fighter
case 'win':
if (!context.badges.broadcaster) {
console.log(`${context.username} not authorized to run "win"`);
return;
}
console.log(`${context.username} ran "win"`);
if (!hasIronMan(target, context.username)) return;
// Get new fighter
ironmans[context.username].shift();
if (ironmans[context.username].length === 0) {
client.say(target, 'Ironman complete! Congratulations!');
return;
}
const newFighter = ironmans[context.username][0];
client.say(
target,
`Next fighter: ${newFighter}. ${ironmans[context.username].length} left`,
);
break;
// Display next 5 fighters and number of fighters left
case 'list':
console.log(`${context.username} ran "list"`);
if (!hasIronMan(target, context.username)) return;
const nextFive = ironmans[context.username]
.slice(1, 6)
.toString()
.replace(/,/g, ', ');
client.say(
target,
`Next 5: ${nextFive}. ${ironmans[context.username].length} left`,
);
break;
// Show current fighter
case 'current':
console.log(`${context.username} ran "current"`);
if (!hasIronMan(target, context.username)) return;
client.say(target, `Current fighter: ${ironmans[context.username][0]}`);
break;
default:
console.log(
`Unknown command "${content}" ran by user ${context.username}`,
);
break;
}
}
function hasIronMan(target, username) {
if (ironmans[username] === undefined || ironmans[username].length <= 0) {
console.log(`${username} tried "win" but does not have a current ironman`);
client.say(
target,
'No ironman started, message "!stark ironman" to start one',
);
return false;
}
return true;
}
function onConnectedHandler(addr, port) {
console.log(`* Connected to ${addr}:${port}`);
}