-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
114 lines (82 loc) · 2.64 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
//Modules
const Discord = require('discord.js');
const cheerio = require('cheerio');
const fetch = require('node-fetch');
//Client
const client = new Discord.Client();
let generated = 0
//Imports
const presidentes = require('./presidentes.json');
const strings = require('./strings/strings.json');
client.on('ready', () => {
client.user.setActivity('Hino Nacional Brasileiro', { type: 'LISTENING' });
console.log(`Logged in as ${client.user.tag}!`);
})
var randomProperty = function (obj) {
var keys = Object.keys(obj);
return obj[keys[keys.length * Math.random() << 0]];
};
client.on("guildCreate", guild => {
let channelID;
let channels = guild.channels.cache;
channelLoop:
for (let key in channels) {
let c = channels[key];
if (c[1].type === "text") {
channelID = c[0];
break channelLoop;
}
}
let channel = guild.channels.cache.get(guild.systemChannelID || channelID);
channel.send(strings.arrive);
});
client.on('message', async msg => {
if (msg.author.bot)
return;
if (msg.content == strings.help_cmd) {
msg.reply(strings.help);
return;
}
if (msg.content == strings.about_cmd) {
msg.channel.send(strings.about);
return;
}
if (msg.content == strings.status_cmd) {
msg.channel.send(`Já foram geradas ${generated} frases aleatórias.`);
return;
}
if (msg.content == strings.random_cmd) {
const safePresidentes = JSON.parse(JSON.stringify(presidentes))
const parsePresidentes = (obj) => {
const keys = Object.keys(obj)
for (key of keys) {
if (!obj[key].key) {
delete obj[key]
}
}
}
parsePresidentes(safePresidentes)
const randomPresident = randomProperty(safePresidentes)
const nome = randomPresident.name;
const presidentKey = randomPresident.key
const response = await fetch(`https://www.pensador.com/autor/${presidentKey}/`);
const body = await response.text();
const $ = cheerio.load(body)
const frases = []
$('.frase.fr').each((index, item) => {
const children = item.children
children && children.map(item => frases.push(item.data))
})
$('.frase.fr0').each((index, item) => {
const children = item.children
children && children.map(item => frases.push(item.data))
})
const frase = frases[Math.floor(Math.random() * frases.length)];
const msg_channel = frase + '\n-' + nome
generated++
msg.channel.send(msg_channel, {
files: [`${randomPresident.avatar}`]
})
}
});
client.login(process.env.DISCORD_TOKEN);