-
Notifications
You must be signed in to change notification settings - Fork 0
/
vezdekod_bot.js
173 lines (142 loc) · 4.77 KB
/
vezdekod_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
165
166
167
168
169
170
171
172
173
require("dotenv").config();
const VkBot = require("node-vk-bot-api");
const Session = require("node-vk-bot-api/lib/session");
const Stage = require("node-vk-bot-api/lib/stage");
const Markup = require("node-vk-bot-api/lib/markup");
const api = require("node-vk-bot-api/lib/api");
const mysql = require("mysql");
const Database = require("./modules/database");
const messages = require("./messages.json");
const questions = require("./scenes/questions");
const { makeButton } = require("./modules/makeButton");
const getMeme = require("./modules/getMeme");
// Init bot instance
global.bot = new VkBot(process.env.TOKEN);
const session = new Session();
const stage = new Stage(questions);
bot.use(session.middleware());
bot.use(stage.middleware());
bot.event("message_event", (ctx) => {
api("messages.sendMessageEventAnswer", {
event_id: ctx.message.event_id,
user_id: ctx.message.user_id,
peer_id: ctx.message.peer_id,
event_data: JSON.stringify({
type: "show_snackbar",
text: "???",
}),
access_token: process.env.TOKEN,
});
});
const gotoCommands = async (ctx) => {
let command = JSON.parse(ctx.message.payload).command.split("_");
switch (command[0]) {
case "action":
const find = await db.request(
`SELECT COUNT(*) as count FROM rating WHERE meme_id = ? AND user_id = ?`,
[command[2], ctx.message.from_id]
);
if (find[0].count !== 0) return;
await db.request(
`INSERT INTO rating(user_id, meme_id, liked) VALUES(?,?,?)`,
[ctx.message.from_id, command[2], command[1] === "like"]
);
const meme = await getMeme(ctx.message.from_id);
if (meme.length !== 0) {
ctx.reply(
messages.meme,
`photo${meme[0].photo_id}`,
Markup.keyboard([
[
makeButton("Нравится!", `action_like_${meme[0].id}`, "positive"),
makeButton(
"Не нравится :(",
`action_dislike_${meme[0].id}`,
"negative"
),
],
]).oneTime()
);
} else ctx.reply(messages.notFoundMeme);
break;
}
};
bot.on(async (ctx) => {
const message = ctx.message;
if (message.payload) return gotoCommands(ctx);
if (message.attachments.length !== 0 && message.attachments[0].photo) {
let photo = message.attachments[0].photo;
photo = photo.owner_id + "_" + photo.id + "_" + photo.access_key;
const findDublicate = await db.request(
`SELECT COUNT(*) as count FROM memes WHERE photo_id = ?`,
[photo]
);
if (findDublicate[0].count === 0) {
await db.request(`INSERT INTO memes(photo_id) VALUES(?)`, [photo]);
return ctx.reply(messages.memeAdded);
} else {
return ctx.reply(messages.memeAlreadyAdded);
}
}
switch (message.text.toLowerCase()) {
case "привет":
ctx.scene.enter("questions");
break;
case "статистика":
const likesInfo = await db.request(
`SELECT COUNT(*) as count FROM rating WHERE user_id = ? AND liked = 1`,
[ctx.message.from_id]
);
const dislikesInfo = await db.request(
`SELECT COUNT(*) as count FROM rating WHERE user_id = ? AND liked = 0`,
[ctx.message.from_id]
);
const topMemes = await db.request(
`SELECT memes.photo_id, COUNT(meme_id) as likes FROM rating INNER JOIN memes ON memes.id = rating.meme_id WHERE rating.liked = 1 GROUP BY rating.meme_id ORDER BY likes DESC LIMIT 9`
);
let attachments = [];
topMemes.map((el) => attachments.push(`photo${el.photo_id}`));
ctx.reply(
messages.statistics
.replace("$liked", likesInfo[0].count)
.replace("$disliked", dislikesInfo[0].count),
attachments
);
break;
case "мем":
const meme = await getMeme(ctx.message.from_id);
if (meme.length !== 0) {
ctx.reply(
messages.meme,
`photo${meme[0].photo_id}`,
Markup.keyboard([
[
makeButton("Нравится!", `action_like_${meme[0].id}`, "positive"),
makeButton(
"Не нравится :(",
`action_dislike_${meme[0].id}`,
"negative"
),
],
]).oneTime()
);
} else ctx.reply(messages.notFoundMeme);
break;
default:
ctx.reply(messages.default);
break;
}
});
const app = async () => {
const connection = await mysql.createPool({
host: process.env.DB_IP,
user: process.env.DB_USER,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
waitForConnections: true,
queueLimit: 0,
});
global.db = new Database(connection);
bot.startPolling();
};
app().then(() => console.log("Bot started!"));