-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
397 lines (348 loc) · 12.8 KB
/
main.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
'use strict';
//required modules
import {
Client,
Intents,
Permissions,
MessageEmbed,
MessageActionRow,
MessageButton,
TextChannel,
ButtonInteraction,
ColorResolvable,
Message
} from "discord.js";
const yahooStockPrices = require("yahoo-stock-prices");
const myIntents = new Intents();
myIntents.add('GUILDS', 'GUILD_PRESENCES', 'GUILD_MEMBERS', 'GUILD_VOICE_STATES', 'GUILD_MESSAGES', 'GUILD_MESSAGE_REACTIONS');
const sqlite3 = require('sqlite3').verbose();
//required configs
const secrets = require('./secrets.json');
const imdbapi = require('imdb-api');
const imdb = new imdbapi.Client({apiKey: secrets.omdbkey});
//const wiki = require('wikipedia');
const URL = require("url").URL;
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay))
const stringIsAValidUrl = (s) => {
try {
new URL(s);
return true;
} catch (err) {
return false;
}
};
//connect to sqlite file
let db = new sqlite3.Database('./waggzbot.db', (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the database.');
});
//init discord.js bot client
const client = new Client({ intents: myIntents, partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
client.on('ready', () => {
console.log('I am ready!', `${client.user.tag}`);
});
//message handler
client.on('messageCreate', message => {
console.log("1", message)
//ignore bots
if (message.author.bot) return;
//if (client.user.username === "waggzbot-test" && message.channel.id !== '989648406941159424') return;
//split message into command and args
const args = message.content.trim().split(/ +/);
const command = args.shift().toLowerCase();
//add new response
if (command === "!respond") {
insertResponse(message, command);
return;
}
if (command === "!request") {
plexRequest(message);
return;
}
if (command === "!imdb") {
IMDBLookup(message);
return;
}
if (command === "!embed") {
createEmbed(message);
return
}
if (command === ".metar") {
getMETAR(message);
return
}
if (command === ".stock") {
getStockPrice(message);
return
}
if (command === "!roulette") {
roulette(message);
return
}
//get triggers and responses
let sql = `SELECT *
FROM responses`;
db.all(sql, [], (err, rows) => {
if (err) {
return console.error(err.message);
}
//init loop vars
let triggerLen = 0;
let triggerRow;
let i = 0;
//loop through triggers
rows.forEach((row) => {
//check for the trigger surrounded by breaks in the message content (this makes 'hi' match the word 'hi' but not the word 'this'
// noinspection JSUnresolvedVariable
let myReg = new RegExp("\\b(" + row.trigger.toLowerCase() + ")\\b", 'gi')
let myMatch = message.content.trim().toLowerCase().match(myReg)
if (myMatch){
//if it's a match make note of it and how long the trigger is, if it's longer than a previous match, override it
// noinspection JSUnresolvedVariable
if (row.trigger.length > triggerLen) {
// noinspection JSUnresolvedVariable
triggerLen = row.trigger.length;
triggerRow = i;
}
}
i++;
});
//if there was a trigger match, and spam control passes
if (triggerRow && shouldWeRespond(message)) { // noinspection JSIgnoredPromiseFromCall
let response = rows[triggerRow].response;
//replace %user% in the trigger with message author
let myRegex = /%user%/gi;
response = response.replace(myRegex, message.author);
if (!rows[triggerRow].reply)
response = preface() + response;
(message.channel as TextChannel).send(response).catch(console.error);
}
});
});
client.on('interactionCreate', async interaction => {
//if (!interaction.isButton()) return;
if (interaction.isButton()) {
const thisInteraction = interaction as ButtonInteraction;
const chan = thisInteraction.channel as TextChannel;
// noinspection JSUnresolvedVariable
if (interaction.customId === "cancelticket" || interaction.customId === "completeticket") {
// noinspection JSUnresolvedVariable
chan.delete().catch(console.error);
const user = client.users.cache.get(chan.topic);
// noinspection JSUnresolvedVariable
user.send({
content: `Your Plex request has been ${interaction.customId === "cancelticket" ? "canceled" : "completed"}:`,
embeds: (interaction.message as Message).embeds
}).catch(console.error);
}
}
});
//start bot
// noinspection JSIgnoredPromiseFromCall
client.login(secrets.token);
// const app = require('./app')
//inserting responses
function insertResponse (message, command) {
//check for split syntax
if (!message.content.includes("=>")) {
message.reply("Incorrect format for adding responses. Correct format is `!respond trigger phrase => response phrase`");
return;
}
//split the command
const args = message.content.slice(command.length + 1).split("=>");
let response = args[1].trim();
let isreply;
//check if response starts with <reply> and flag it true if so
let replySearch = response.indexOf("<reply>");
if (replySearch === 0) {
response = response.slice(7);
isreply = true;
}
else isreply = false;
db.run(`INSERT INTO responses(trigger,response,reply) VALUES(?, ?, ?)`, [args[0].trim(), response, isreply], function(err) {
if (err) {
message.channel.send("Error adding response, does it already exist?");
return console.error(err.message);
}
//success, let the author know
message.channel.send(`OK ${message.author}, I will respond to ***${args[0].trim()}*** with ***${args[1].trim()}***`);
// get the last insert id
// noinspection JSUnresolvedVariable
console.log(`A row has been inserted with rowid ${this.lastID}`);
});
}
//return random int from 0 through max-1
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
//spam reduction
function shouldWeRespond (message) {
return getRandomInt(2) === 1 || message.content.includes(client.user.id);
}
//prefaces
function preface (){
let prefaces = [
"i heard",
"someone said",
"somebody mentioned that",
"it has been said that",
"rumor has it",
"did you know that",
"i guess",
"i think"
]
return prefaces[Math.floor(Math.random() * prefaces.length)] + ' ';
}
function plexRequest (message) {
const name = message.content.slice(8).trim();
const regex = /t{2}[0-9]{7,}/g;
const imdbid = name.match(regex);
if (imdbid === null) {
message.reply("Please use an IMDB title id or URL. (You can get these by using !imdb <title>");
return;
}
const params = { 'id': imdbid[0] };
imdb.get(params).then((search) => {
// noinspection JSCheckFunctionSignatures
message.guild.channels.create(search.title, {
type: 'GUILD_TEXT',
parent: '636503079197605911',
permissionOverwrites: [
{
id: message.author.id,
allow: [Permissions.FLAGS.VIEW_CHANNEL],
},
{
id: client.user.id,
allow: [Permissions.FLAGS.VIEW_CHANNEL, Permissions.FLAGS.MANAGE_CHANNELS],
},
{
id: message.guild.id, // shortcut for @everyone role ID
deny: [Permissions.FLAGS.VIEW_CHANNEL],
},
],
topic: `${message.author.id}`,
reason: `New Plex Request by ${message.author.username}#${message.author.discriminator}`,
}).then((result)=> {
message.reply(`Created channel <#${result.id}>`);
const embed = new MessageEmbed()
.setTitle(`${search.title} (${search._yearData} ${search.type})`)
.setURL(search.imdburl)
.setDescription(`${search.imdbid} - ${search.genres}
Starring ${search.actors}
Rated ${search.rated} - ${search.runtime}
${search.plot.substring(0,300)}...`)
.setImage(stringIsAValidUrl(search.poster) ? search.poster : null)
// noinspection JSCheckFunctionSignatures
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('cancelticket')
.setLabel('Cancel')
.setStyle('DANGER'),
new MessageButton()
.setCustomId('completeticket')
.setLabel('Completed')
.setStyle('SUCCESS'),
)
result.send({embeds: [embed], components: [row]})
.catch(console.error);
}).catch(console.error)
})
.catch(console.error)
}
function IMDBLookup (message) {
const query = message.content.slice(5).trim();
let params = {'name': query};
let list = [];
let num;
imdb.search(params).then((search) => {
num = search.results.length;
for (const result in search.results) {
imdb.get({id: search.results[result].imdbid})
.then( (movie) => {
const embed = new MessageEmbed()
.setTitle(`${movie.title} (${search.results[result].year} ${movie.type})`)
.setURL(movie.imdburl)
.setDescription(`${movie.imdbid} - ${movie.genres}
Starring ${movie.actors}
Rated ${movie.rated} - ${movie.runtime}
${movie.plot.substring(0,300)}...`)
.setImage(stringIsAValidUrl(movie.poster) ? movie.poster : null)
.setColor(getRandomColor() as ColorResolvable)
list.push(embed);
})
.catch(console.error)
}
})
.catch(console.error)
.finally(() => {
sleep(2000).then(()=>{
message.author.send({embeds:list}).catch(console.error)
})
})
}
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
async function createEmbed (message) {
// get the file's URL
const file = message.attachments.first()?.url;
if (!file) return console.log('No attached file found');
try {
// fetch the file from the external URL
const response = await fetch(file);
// if there was an error send a message with the status
if (!response.ok)
return message.channel.send(
'There was an error with fetching the file:',
response.statusText,
);
// take the response stream and read it to completion
const text = await response.text();
if (text) {
console.log(JSON.parse(text));
const data = JSON.parse(text);
message.guild.channels.cache.get(data.channel).send({content: data.message.content, embeds: data.message.embeds});
}
} catch (error) {
console.log(error);
}
}
async function getMETAR (message) {
const station = message.content.slice(6).trim();
const url = `https://www.aviationweather.gov/api/data/metar?ids=${station}&format=json`;
console.log(url)
// fetch the file from the external URL
const response = await fetch(url);
// if there was an error send a message with the status
if (!response.ok)
return message.channel.send(
'There was an error with fetching the file:',
response.statusText,
);
// take the response stream and read it to completion
const text = await response.text();
if (text) {
message.channel.send(JSON.parse(text)[0].rawOb)
}
}
function roulette (message) {
const outcome = getRandomInt(6) === 1;
message.channel.send(outcome ? `BANG! You're dead, ${message.author}.` : "Click! Empty chamber..." )
}
async function getStockPrice (message) {
const symbol = message.content.slice(6).trim().toUpperCase();
const price = await yahooStockPrices.getCurrentPrice(symbol);
console.log(price)
message.channel.send(price.toString());
}
export {client}
require('./bin/www')