-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (134 loc) · 6.21 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const fs = require('fs');
const readline = require('readline');
const translate = require('@iamtraction/google-translate');
const https = require('https');
const cheerio = require('cheerio');
const config = JSON.parse(fs.readFileSync('config.json'));
const channels = require('./channels.json').channels;
const irc = require('irc');
const client = new irc.Client(config.server, config.nick, {
channels
});
console.log(`Connected to ${config.server} as ${config.nick}`);
console.log('You can type messages in the terminal to send them to the default channel.');
// Default channel (initially set to the first one)
let defaultChannel = channels[0];
// IRC Message Handlers
client.on('message', (nick, to, text) => {
const lowercase = text.toLowerCase();
if (lowercase === '-help' || lowercase === `${config.nick.toLowerCase()} help` || lowercase === config.nick.toLowerCase()) {
client.say(to, `${nick}: Hello, I'm a translation bot developed by forero, translate to your desired language using the ISO 639-1 code or ISO 639-3 code like this: -es Text to be translated. Note: if the iso code is invalid I will not respond.`);
client.say(to, 'I can join any channel you want! just invite me using the the /invite command and I\'ll join immediately :) if you want me to stop joining your channel just kick me and I won\'t come back. My source code is available at https://github.com/forerosantiago/irc-translate-bot');
} else if (lowercase.startsWith('!say ')) {
const messageToSend = text.slice(5); // Extract the message to send
client.say(to, messageToSend); // Make the bot say the message
} else if (lowercase.startsWith('-')) {
const iso = lowercase.slice(1).trim().split(/ +/)[0];
const textToTranslate = text.slice(iso.length + 2).trim(); // Correctly extract the text to be translated
translate(textToTranslate, { to: iso }).then(res => {
const translatedText = res.text;
client.say(to, translatedText.trim());
console.log(res);
}).catch(err => {
console.error(err);
});
} else if (lowercase.startsWith('!weather')) {
// Fetch weather information from wttr.in
https.get('https://wttr.in/Boca+Raton', (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
// Parse HTML using cheerio
const $ = cheerio.load(data);
// Extract text content from the HTML
const weatherText = $('pre').text().trim();
// Send only the top part of the weather information to the channel with blank lines before and after
const lines = weatherText.split('\n');
const topWeatherInfo = `${lines.slice(1, 7).join('\n')}\n`; // Add space for blank lines
client.say(to, topWeatherInfo);
});
}).on('error', (err) => {
console.error('Error fetching weather:', err.message);
});
}
});
// Terminal Input (Interactive)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '> ' // Show prompt when waiting for input
});
// Handle user input
rl.prompt();
rl.on('line', (line) => {
const input = line.trim();
if (input.startsWith('/join ')) {
const channel = input.split(' ')[1];
client.join(channel, () => {
console.log(`Joined channel: ${channel}`);
defaultChannel = channel; // Update the default channel
});
} else if (input.startsWith('/quit')) {
console.log('Exiting bot...');
rl.close();
process.exit(0);
} else {
// Send message to the default channel
client.say(defaultChannel, input);
console.log(`(You) ${defaultChannel}: ${input}`);
}
rl.prompt(); // Show prompt again after handling input
});
const fs = require('fs');
const translate = require('@iamtraction/google-translate');
const https = require('https');
const cheerio = require('cheerio');
const config = JSON.parse(fs.readFileSync('config.json'));
const channels = require('./channels.json').channels;
const irc = require('irc');
const client = new irc.Client(config.server, config.nick, {
channels
});
console.log(config.server, config.nick);
client.on('message', (nick, to, text) => {
const lowercase = text.toLowerCase();
if (lowercase === '-help' || lowercase === `${config.nick.toLowerCase()} help` || lowercase === config.nick.toLowerCase()) {
client.say(to, `${nick}: Hello, I'm a translation bot developed by forero, translate to your desired language using the ISO 639-1 code or ISO 639-3 code like this: -es Text to be translated. Note: if the iso code is invalid I will not respond.`);
client.say(to, 'I can join any channel you want! just invite me using the the /invite command and I\'ll join immediately :) if you want me to stop joining your channel just kick me and I won\'t come back. My source code is available at https://github.com/forerosantiago/irc-translate-bot');
} else if (lowercase.startsWith('!say ')) {
const messageToSend = text.slice(5); // Extract the message to send
client.say(to, messageToSend); // Make the bot say the message
} else if (lowercase.startsWith('-')) {
const iso = lowercase.slice(1).trim().split(/ +/)[0];
const textToTranslate = text.slice(iso.length + 2).trim(); // Correctly extract the text to be translated
translate(textToTranslate, { to: iso }).then(res => {
const translatedText = res.text;
client.say(to, translatedText.trim());
console.log(res);
}).catch(err => {
console.error(err);
});
} else if (lowercase.startsWith('!weather')) {
// Fetch weather information from wttr.in
https.get('https://wttr.in/Boca+Raton', (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
// Parse HTML using cheerio
const $ = cheerio.load(data);
// Extract text content from the HTML
const weatherText = $('pre').text().trim();
// Send only the top part of the weather information to the channel with blank lines before and after
const lines = weatherText.split('\n');
const topWeatherInfo = `${lines.slice(1, 7).join('\n')}\n`; // Add space for blank lines
client.say(to, topWeatherInfo);
});
}).on('error', (err) => {
console.error('Error fetching weather:', err.message);
});
}
});