-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.ts
248 lines (212 loc) · 7.4 KB
/
bot.ts
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import * as fs from 'fs';
import * as yaml from 'js-yaml';
import express from 'express';
import irc from 'irc';
import * as sqlite3 from 'sqlite3';
// Read YAML file
const fileContents = fs.readFileSync('./config.yml', 'utf8');
// Parse YAML content into a plain JavaScript object
const config = yaml.load(fileContents);
// Specify the path to the SQLite database file
const dbFilePath = './data/database.db';
// Create the SQLite database connection
const db = new sqlite3.Database(dbFilePath);
// Create a table to store catchphrase events
db.run(`
CREATE TABLE IF NOT EXISTS events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user TEXT,
channel TEXT,
event_type TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
// Create a table to store opt-in and opt-out preferences
db.run(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user TEXT,
channel TEXT,
opted_in BOOLEAN DEFAULT false,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
// IRC bot configuration
const ircConfig: irc.Config = {
channels: config.irc.channels,
server: config.irc.server,
port: config.irc.port,
secure: config.irc.tls,
userName: config.irc.username,
realName: config.irc.username,
autoConnect: true,
};
const catchphrases = config.moin.variations;
const express = require('express');
const apiApp = express();
const irc = require('irc');
// Create the IRC client
const client = new irc.Client(ircConfig.server, ircConfig.userName, ircConfig);
// Listen for the 'raw' event to log raw IRC messages
// client.addListener('raw', (message) => {
// console.log('Raw IRC message:', message);
// });
// Dictionary to store users who opted-in
const optedInUsers: Record<string, boolean> = {};
// Register event handlers
client.addListener('registered', () => {
// Identify with NickServ after the bot has registered on the server
client.say('NickServ', `IDENTIFY ${config.irc.username} ${config.irc.passphrase}`);
});
// Register event handlers
client.addListener('message', async (from, to, message) => {
// Convert the message to lowercase for case-insensitive matching
const lowercaseMessage = message.trim().toLowerCase();
// Check if the message contains any of the catchphrases
const catchphraseUsed = catchphrases.some((catchphrase) =>
lowercaseMessage.includes(catchphrase)
);
if (to.startsWith('#')) {
const catchphraseUsed = catchphrases.includes(lowercaseMessage);
// Check if the user has opted in and a catchphrase is used
if (catchphraseUsed && (await hasUserOptedIn(from, to) || config.moin.allowedUsers.includes(from))) {
// Log the catchphrase event to the database
logEvent(from, to, lowercaseMessage);
}
// Check if the message is the opt-in command
if (lowercaseMessage === '!ja') {
optInUser(from, to);
client.notice(to, `${from} sagt ja zum moin.`);
}
// Check if the message is the opt-out command
if (lowercaseMessage === '!nein') {
optOutUser(from, to);
client.notice(to, `${from} sagt nein zum moin.`);
}
if (lowercaseMessage === '!moin') {
// Query the database to get the count of "moin" events for the channel
const count = await getChannelCatchphraseCount(to);
client.notice(to, `Bisher gab es ${count} verschiedene moins.`);
}
if (lowercaseMessage.startsWith('!zahl')) {
const parts = message.split(' ');
const nickToCount = parts[1] || from;
const userCount = await getUserCatchphraseCount(to, nickToCount);
if (userCount > 0) {
client.notice(to, `${nickToCount} hat ${userCount} Mal moin gesagt.`);
} else {
client.notice(to, `Der Nick ist nicht bekannt.`);
}
}
}
});
// Function to log events to the database
function logEvent(user: string, channel: string, eventType: string) {
db.run(
'INSERT INTO events (user, channel, event_type) VALUES (?, ?, ?)',
[user, channel, eventType],
(err) => {
if (err) {
console.error('Error logging event:', err.message);
}
}
);
}
// Function to check if a user has opted in
function hasUserOptedIn(user: string, channel: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
const query = 'SELECT opted_in FROM users WHERE user = ? AND channel = ? ORDER BY timestamp DESC LIMIT 1';
db.get(query, [user, channel], (err, row: { opted_in?: boolean }) => {
if (err) {
console.error('Error checking user preference:', err.message);
reject(err);
return;
}
resolve(!!row?.opted_in);
});
});
}
// Function to opt-in a user
function optInUser(user: string, channel: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
const query = 'INSERT OR REPLACE INTO users (user, channel, opted_in) VALUES (?, ?, true)';
db.run(query, [user, channel], (err) => {
if (err) {
console.error('Error opting in user:', err.message);
reject(err);
return;
}
optedInUsers[user] = true;
resolve();
});
});
}
// Function to opt-out a user
function optOutUser(user: string, channel: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
const query = 'INSERT OR REPLACE INTO users (user, channel, opted_in) VALUES (?, ?, false)';
db.run(query, [user, channel], (err) => {
if (err) {
console.error('Error opting out user:', err.message);
reject(err);
return;
}
delete optedInUsers[user];
resolve();
});
});
}
async function getChannelCatchphraseCount(channel: string): Promise<number> {
return new Promise<number>((resolve, reject) => {
const query = 'SELECT COUNT(*) as count FROM events WHERE channel = ?';
db.get(query, [channel], (err, row: { count?: number }) => {
if (err) {
console.error('Error getting catchphrase count:', err.message);
reject(err);
return;
}
resolve(row && typeof row.count === 'number' ? row.count : 0);
});
});
}
// Function to get the count of catchphrase events for a user (defaults to the sender)
async function getUserCatchphraseCount(channel: string, user?: string): Promise<number> {
return new Promise<number>((resolve, reject) => {
const query = 'SELECT COUNT(*) as count FROM events WHERE channel = ? AND user COLLATE NOCASE = ?';
const params = user ? [channel, user] : [channel, user || ''];
db.get(query, params, (err, row: { count?: number }) => {
if (err) {
console.error('Error getting catchphrase count:', err.message);
reject(err);
return;
}
// TypeScript now knows the structure of the result
resolve(row && typeof row.count === 'number' ? row.count : 0);
});
});
}
// Handle errors
client.addListener('error', (message) => {
console.error('Error:', message);
});
// Handle disconnects
client.addListener('disconnect', () => {
console.log('Disconnected. Reconnecting...');
client.connect();
});
// Endpoint to get catchphrase events
apiApp.get('/api', (req, res) => {
const query = 'SELECT * FROM events';
db.all(query, [], (err, rows) => {
if (err) {
console.error('Error getting catchphrase events:', err.message);
res.status(500).json({ error: 'Internal Server Error' });
return;
}
res.json(rows);
});
});
// Start the server
apiApp.listen(config.api.port, () => {
console.log(`Server is running on http://localhost:${config.api.port}`);
});