Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Vkozulya kakoy feature #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 63 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,72 @@
require('dotenv').config();
const Telegraf = require('telegraf');
const Extra = require('telegraf/extra');
require("dotenv").config();
const Telegraf = require("telegraf");
const Extra = require("telegraf/extra");
const Random = require("./prng");
const kakoy = require("./kakoy.json");

const bot = new Telegraf(process.env.BOT_TOKEN);
const COMMAND = "/send@vkozulya_bot";
const HISTORY_DAYS_COUNT = 5;
const HISTORY_WORDS = [
"Вчера",
"Позавчера",
"3 дня назад",
"4 дня назад",
"5 дней назад"
];

const getKakoy = (userId, date) => {
const normalizedDate = new Date(date.setHours(0, 0, 0, 0));
const seed = userId + Math.round(normalizedDate.getTime() / 1e7);
const random = new Random(seed).next();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ты каждый раз инициируешь новый инстанс рандома? Зачем?

const wordsCount = random % 4 + 1;

return Array(wordsCount)
.fill()
.reduce((result, _, idx) => {
const wordIdx = new Random(seed + idx).next() % kakoy.length;

bot.start(ctx => ctx.reply('Добавь меня в чат и имитируй Козулю'));
return `${result}${kakoy[wordIdx]} `;
}, "");
};

const COMMAND = '/send@vkozulya_bot';
const bot = new Telegraf(process.env.BOT_TOKEN);

bot.start((ctx) => ctx.reply("Добавь меня в чат и имитируй Козулю"));

bot.on('message', async ({message, reply, telegram}) => {
if (message.text && message.text.indexOf(COMMAND) === 0) {
console.log(JSON.stringify(message));
const extra = message.reply_to_message ? Extra.inReplyTo(message.reply_to_message.message_id) : null;
await reply(message.text.slice(COMMAND.length).trim() || 'Ух бля', extra);
bot.on("message", async ({ message, reply, telegram }, next) => {
if (message.text && message.text.indexOf(COMMAND) === 0) {
console.log(JSON.stringify(message));
const extra = message.reply_to_message
? Extra.inReplyTo(message.reply_to_message.message_id)
: null;
await reply(message.text.slice(COMMAND.length).trim() || "Ух бля", extra);

try {
await telegram.deleteMessage(message.chat.id, message.message_id);
} catch(err) {};
try {
await telegram.deleteMessage(message.chat.id, message.message_id);
} catch (err) {}
} else {
next();
}
});

bot.command("kakoy", ({ message, reply }) => {
const answer = getKakoy(message.from.id, new Date());

reply(`Сегодня ты ${answer} Козуля!`);
});

bot.command("hist", ({ message, reply }) => {
const answer = Array(HISTORY_DAYS_COUNT)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Интересные отступы

.fill()
.reduce((result, _, idx) => {
const date = new Date();
const resultDate = new Date(date.setDate(date.getDate() - idx));
const kakoy = getKakoy(message.from.id, resultDate);

return `${result}${HISTORY_WORDS[idx]} ты был ${kakoy} Козуля!\n`;
}, "");

reply(answer);
});

bot.startPolling();
30 changes: 30 additions & 0 deletions kakoy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
"яндексовый",
"финамовый",
"мэйлрушный",
"авитовый",
"жумовый",
"рокетбанковый",
"безработный",
"беззаботный",
"мемующий",
"твиттерный",
"жсжобсовый",
"седой",
"бородатый",
"интровертный",
"собеседующий",
"собеседующийся",
"решающий задачу на листочке",
"удаляющий чят",
"ливнувший",
"интровертный",
"выбравшийся",
"суеверный",
"сувенирный",
"докладывающий",
"пиарящий",
"нативный",
"undefined",
Copy link

@PaulineNemchak PaulineNemchak Apr 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

мб вместо undefined — is not defined
и ещё мб function is not a function

или просто undefined и null оставить

"unhandled promise rejection"
]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
Expand Down
14 changes: 14 additions & 0 deletions prng.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Random {
constructor(seed) {
this._seed = seed % 2147483647;
if (this._seed <= 0) {
this._seed += 2147483646;
}
}

next() {
return (this._seed = (this._seed * 16807) % 2147483647);
}
}

module.exports = Random;