-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
200 lines (170 loc) · 6.18 KB
/
bot.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
require('dotenv').config();
const { MongoClient } = require("mongodb");
const tmi = require('tmi.js');
/**
* DataBase Functions
*/
// Setup MongoDB
const url = process.env.DB_TOKEN;
const dbclient = new MongoClient(url);
// The database to use
const dbName = "slugs";
async function addSlugBoomer(username) {
try {
await dbclient.connect();
console.log("Connected correctly to server");
const db = dbclient.db(dbName);
// Use the collection "boomer"
const col = db.collection("boomer");
// Construct a document
let slugDocument = {
"name": username,
"boomer": true
}
// Check for existance
if (await col.findOne(slugDocument) == null) {
// Insert a single document, wait for promise so we can read it back
const p = await col.insertOne(slugDocument);
}
} catch (err) {
console.log(err.stack);
}
}
async function updateSlugBoomer(username) {
try {
await dbclient.connect();
console.log("Connected correctly to server");
const db = dbclient.db(dbName);
// Use the collection "boomer"
const col = db.collection("boomer");
// Construct a document
let slugDocument = {
"name": username,
}
// Insert a single document, wait for promise so we can read it back
const p = await col.deleteOne(slugDocument);
} catch (err) {
console.log(err.stack);
}
}
async function checkDB(username) {
try {
await dbclient.connect();
console.log("Connected correctly to server");
const db = dbclient.db(dbName);
// Use the collection "boomer"
const col = db.collection("boomer");
// Construct a document
let slugDocument = {
"name": username,
"boomer": true
}
// Check for existance
if (await col.findOne(slugDocument) == null) {
return false;
} else {return true;}
} catch (err) {
console.log(err.stack);
}
}
/**
* Chatbot functions
*/
// Define configuration options
const client = new tmi.Client({
options: { debug: true },
connection: {
secure: true,
reconnect: true
},
identity: {
username: process.env.BOT_USERNAME,
password: process.env.OAUTH_TOKEN
},
channels: ['Sliggytv', 'GhostsShadow03']
});
// Connect to Twitch:
client.connect();
// Called every time a message comes in
client.on('message', (channel, tags, message, self) => {
if (self || !message.startsWith('!')) return;
const commandName = message.trim();
// If the command is known, let's execute it
if (commandName.slice(0, 4) === '!age') {
const boomer = isBoomer(parseInt(commandName.slice(5)));
if (boomer) {
addSlugBoomer(tags.username);
client.say(channel, `You're a sliggy1BOOMER.`);
} else if (!Number.isInteger(parseInt(commandName.slice(5)))){
return;
} else {
updateSlugBoomer(tags.username);
client.say(channel, `You're a sliggy1SLUG.`);
}
console.log(`* Executed ${commandName} command`);
} else if (commandName === '!dev') {
client.say(channel, `chatbot by @GhostsShadow03`);
console.log(`* Executed ${commandName} command`);
}
else {
console.log(`* Unknown command ${commandName}`);
}
});
// Function called when the "age" command is issued
function isBoomer(age) {
if (age >= 29) {
return true;
} else {
return false;
}
}
// Called everytime a Subscription comes in
client.on("subscription", (channel, username, method, message, userstate) => {
// Check if BoomerSlug
if (checkDB(username)) {
client.say(channel, `Give me the sliggy1SLUG sliggy1SLUG sliggy1SLUG for ${username} in the chat!`);
} else {
client.say(channel, `Give me the sliggy1BOOMER sliggy1BOOMER sliggy1BOOMER for ${username} in the chat!`);
}
console.log(`* Executed Subscription Event`);
});
// Called everytime a Resubscribtion happens
client.on("resub", (channel, username, months, message, userstate, methods) => {
// Check if BoomerSlug
if (checkDB(username)) {
client.say(channel, `Give me the sliggy1SLUG sliggy1SLUG sliggy1SLUG for ${username} in the chat!`);
} else {
client.say(channel, `Give me the sliggy1BOOMER sliggy1BOOMER sliggy1BOOMER for ${username} in the chat!`);
}
let cumulativeMonths = ~~userstate["msg-param-cumulative-months"];
console.log(`* Executed Resubscribtion Event`);
});
// Called on Subgift
client.on("subgift", (channel, username, streakMonths, recipient, methods, userstate) => {
// Check if BoomerSlug
if (checkDB(recipient)) {
client.say(channel, `Give me the sliggy1SLUG sliggy1SLUG sliggy1SLUG for ${recipient} in the chat!`);
} else {
client.say(channel, `Give me the sliggy1BOOMER sliggy1BOOMER sliggy1BOOMER for ${recipient} in the chat!`);
}
let senderCount = ~~userstate["msg-param-sender-count"];
console.log(`* Executed Subgift Event`);
});
// Called on myterysubgift
client.on("submysterygift", (channel, username, numbOfSubs, methods, userstate) => {
if (checkDB(username)) {
client.say(channel, `Give me the sliggy1SLUG sliggy1SLUG sliggy1SLUG for ${username} in the chat!`);
} else {
client.say(channel, `Give me the sliggy1BOOMER sliggy1BOOMER sliggy1BOOMER for ${username} in the chat!`);
}
let senderCount = ~~userstate["msg-param-sender-count"];
console.log(`* Executed Mysterysubgift Event`);
});
// Called on Raid
client.on("raided", (channel, username, viewers) => {
if (checkDB(username)) {
client.say(channel, `Give me the sliggy1SLUG sliggy1SLUG sliggy1SLUG for ${viewers} in the chat!`);
} else {
client.say(channel, `Give me the sliggy1BOOMER sliggy1BOOMER sliggy1BOOMER for ${viewers} in the chat!`);
}
});