-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
164 lines (146 loc) · 4.39 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
const Discord = require('discord.js');
const GoogleImages = require('google-images');
const Snoowrap = require('snoowrap');
const Markov = require('markov-strings').default;
const he = require('he');
const bot = new Discord.Client();
const searchClient = new GoogleImages(process.env.GOOGLE_CSE_ID, process.env.GOOGLE_API_KEY);
const reddit = new Snoowrap({
userAgent: 'Cabbagebot/1.0',
clientId: process.env.REDDIT_CLIENT_ID,
clientSecret: process.env.REDDIT_CLIENT_SECRET,
refreshToken: process.env.REDDIT_REFRESH_TOKEN
});
var markov = null;
const rebuildMarkov = function () {
// Search for "cabbage" posts on Reddit
const cabbagePromise = reddit
.search({
query: 'cabbage',
time: 'all',
sort: 'relevance',
limit: 1000
})
.map(post => `${post.title}\n${post.selftext}`.trim());
// Fetch the hot self-posts from the FortNiteBR subreddit
const fortnitePromise = reddit.getSubreddit('FortNiteBR')
.getHot({
limit: 1000
})
.filter(post => post.is_self)
.map(post => `${post.title}\n${post.selftext}`);
// Combine both results
return Promise.all([cabbagePromise, fortnitePromise])
.then(bothResults => {
const texts = bothResults.flat();
// Initialize Markov chain text generator
markov = new Markov(texts, { stateSize: 2 });
return markov.buildCorpusAsync();
})
.catch(error => console.log(error));
};
const randomImage = function (imageSearchResults) {
const urls = imageSearchResults
// Get urls
.map(image => image.url)
// Filter to files with extensions that Discord recognizes
.filter(url => (/\.(gif|jpg|jpeg|png)$/i).test(url));
// Select one randomly
return urls[Math.floor(Math.random() * urls.length)];
};
bot.on('message', message => {
// !cabbagebot
if (message.content === '!cabbagebot') {
// If the message is from cabbagebot, freak out
if (message.author.username === 'cabbagebot') {
message.reply({
files: [{
attachment: 'https://i.imgur.com/U5EJjfG.jpg'
}]
});
}
// Otherwise reply with a cabbage image
else {
// Search for images
searchClient.search('cabbage')
// Choose a random image from the list
.then(randomImage)
// Reply with image file
.then(url => message.reply({
files: [{
attachment: url
}]
}));
}
}
// !mrscabbagebot
else if (message.content === '!mrscabbagebot') {
// Reply with a banana image
// Search for images
searchClient.search('banana')
// Choose a random image from the list
.then(randomImage)
// Reply with image file
.then(url => message.reply({
files: [{
attachment: url
}]
}));
}
// !cabbageboy
else if (message.content === '!cabbageboy') {
// Reply with a "young happy man holding cabbage"...
message.reply({
files: [{
attachment: 'https://thumbs.dreamstime.com/b/young-happy-man-cabbage-2350823.jpg'
}]
});
}
// !cabbagebaby
else if (message.content === '!cabbagebaby') {
// Reply with baby
message.reply({
files: [{
attachment: 'https://s1.1zoom.ru/b5050/271/Cabbage_Creative_White_background_Infants_528329_2880x1800.jpg'
}]
});
}
// !cabbagedog
else if (message.content === '!cabbagedog') {
// Reply with corgi attack
message.reply({
files: [{
attachment: 'https://cdn.discordapp.com/attachments/241039241197518850/496931244924469248/zK7h7Zl.gif'
}]
});
}
// !cabbageai
else if (message.content === '!cabbageai') {
markovPromise
.then(() => {
// Generate a post
const result = markov.generate({
maxTries: 100,
filter: (result) => {
return result.score > 5 &&
result.refs.length > 2 &&
result.string.length < 400 &&
result.string.toLowerCase().includes("cabbage");
}
});
// Decode HTML entities
const response = he.decode(result.string);
// Send reply on Discord
message.reply(response);
})
// Silently catch if fail to generate sentence
.catch(error => console.log(error));
}
});
// Rebuild corpus every hour
var markovPromise = rebuildMarkov();
var hour = 60 * 60 * 1000;
setInterval(() => {
markovPromise = rebuildMarkov();
}, hour);
bot.login(process.env.DISCORD_TOKEN);