-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
164 lines (145 loc) · 4.63 KB
/
main.js
File metadata and controls
164 lines (145 loc) · 4.63 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
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 MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const _ = require('underscore');
const mongojs = require('mongojs');
const CURRENT_SET = (process.env.CURRENT_SET || 1);
const CHANNEL = (process.env.CHANNEL || 'hackerloop');
const MONGODB_URL = (process.env.MONGODB_URL || 'mongodb://localhost:27017/hackerloop');
const TESTMODE = process.env.TESTMODE || false;
global.db = mongojs(MONGODB_URL);
var tmi = require("tmi.js");
var options = {
options: {
debug: true
},
connection: {
reconnect: true
},
identity: {
username: "hackerloop",
password: "oauth:fkck47zva046kyip43ew2q0yqhm1bx"
},
// channels: ["#hanryang1125"]
channels: [`${CHANNEL}`]
};
var client = new tmi.client(options);
// Connect the client to the server..
client.connect();
// Connection URL
const url = MONGODB_URL;
// Use connect method to connect to the Server
MongoClient.connect(url,{
// retry to connect for 60 times
reconnectTries: 60,
// wait 1 second before retrying
reconnectInterval: 100,
autoReconnect : true
}, function(err, db) {
assert.equal(null, err);
if(err) throw err;
console.log("Connected correctly to MongoDB");
// db.listCollections().toArray(function(err, collections){
// if (!_.contains(_.map(collections, db => (db.name)), 'vote')) {
// db.createCollection(
// 'vote',
// {
// autoIndexId: true,
// strict: true,
// });
// }
// });
db.close();
});
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
const wait = function sl(ms) {
let now = new Date().getTime();
while (now + ms >= new Date().getTime()) {}
};
wait(2000);
const collections = {};
collections.vote = db.collection('vote');
collections.votelogs = db.collection('votelogs');
collections.record = db.collection('record');
collections.vote.count({ set: CURRENT_SET, channel: CHANNEL }, function(error, count) {
if (count === 0) {
collections.vote.insert({ set: CURRENT_SET, channel: CHANNEL });
}
});
const collection = db.collection('regprocess');
const results = collection.find({ set: CURRENT_SET }).toArray((err, docs) => {
const arrayRules = {};
docs.forEach((obj, values) => {
let options = '';
options += obj.insensitive === true ? 'i': '';
options += obj.recursive === true ? 'g': '';
const ruleName = (obj.type).capitalize();
arrayRules[`rule${ruleName}`] = (message, user) => {
const reg = new RegExp(obj.regex, options);
if (message.match(reg)) {
const result = ((message.match(reg))[0]).toLowerCase();
if (obj.type === 'vote') {
const inc = {};
inc[`choices.${result}`] = 1;
console.log(`User ${user['display-name']} has voted!`);
collections[obj.type].update({
'set': CURRENT_SET,
'channel': CHANNEL,
}, {
$inc: inc
});
collections['votelogs'].insert({ set: CURRENT_SET, channel: CHANNEL, user, vote: result });
// client.userstate['#hackerloop'] = {};
}
if (obj.type === 'record') {
console.log(`User ${user['display-name']} has sent a playin'command!`);
collections[obj.type].insert({
'set': CURRENT_SET,
'channel': CHANNEL,
'command': `${result}`,
user,
createdAt: new Date()
});
}
}
return false;
}
});
client.on('connected', function(address, port) {
client.say(`${CHANNEL}`, 'Starting a new game...');
client.say(`${CHANNEL}`, 'For vote type: fart or prout');
client.say(`${CHANNEL}`, 'For playing type: w or s or d or a or j or k');
});
const makeRecord = () => {
let text = '';
const possible = 'wsadvb';
for (let i = 0; i < 1; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
const makeVote = () => {
let position = '';
const possible = ['fart', 'prout'];
for (let i = 0; i < 1; i++) {
position += Math.floor(Math.random() * possible.length);
}
return possible[position];
};
client.on("chat", function (channel, user, message, self) {
if (user.mod) {
// User is a mod.
}
if (TESTMODE) {
arrayRules.ruleRecord(makeRecord(), user);
console.log(makeRecord());
arrayRules.ruleVote(makeVote(), user);
console.log(makeVote());
} else {
arrayRules.ruleVote(message, user);
arrayRules.ruleRecord(message, user);
}
// console.log( (new RegExp('^poongYuri$', 'ig')).test(message) );
});
});