forked from peksi/tonni-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
125 lines (103 loc) · 3.34 KB
/
index.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
require("dotenv").config();
import { Telegraf, Context } from "telegraf";
import { formatDistance, parseISO } from "date-fns";
import { fi } from "date-fns/locale";
import { Update, Message } from "typegram";
import db = require("./db");
var _ = require("lodash");
db.initializeDb();
const validChatId = (chatId) => {
return true;
// return chatId === -416691354
};
const deadLineDate = new Date(2023, 4, 1); // until mayday 2023. months start indexing from 0
interface DeadLineObject {
months: number;
days: number;
}
const timeUntilDeadLine = (): DeadLineObject => {
const now = new Date();
const diff = deadLineDate.getTime() - now.getTime();
const months = Math.floor(diff / (1000 * 60 * 60 * 24 * 30));
const days = Math.floor(diff / (1000 * 60 * 60 * 24)) % 30;
return { months, days };
};
const timeUntilDeadLineString: () => string = () => {
return `Aikaa Wappuun ${
timeUntilDeadLine().months
} kuukautta ja ${timeUntilDeadLine().days} päivää!`
};
// Define your own context type
interface MyContext extends Context {
myProp?: string;
myOtherProp?: number;
}
const bot = new Telegraf<MyContext>(process.env.BOT_TOKEN);
const cheersReply = async (
ctx: Context<{
message: Update.New & Update.NonChannel & Message.TextMessage;
update_id: number;
}> &
Omit<MyContext, keyof Context<Update>>
) => {
const stats = await db.getStatsForUser(ctx.from.id);
return `
Se oli kunnon repäsy.
Hyvä homma ${
ctx.message.from.first_name
}! Sinulla on nyt ${stats.amount.toFixed(2)} kilometriä kasassa.
`;
};
const statsReply = async (
ctx: Context<{
message: Update.New & Update.NonChannel & Message.TextMessage;
update_id: number;
}> &
Omit<MyContext, keyof Context<Update>>
) => {
const userListWithScores = await db.getStatistics();
const retString: string[] = userListWithScores.map((entry) => {
const agoString = formatDistance(Date.parse(entry.timestamp), new Date(), {
addSuffix: true,
locale: fi,
});
return `<b>${entry.first_name} - ${String(
entry.amount.toFixed(2)
)} kilometriä</b>\nedellinen ${agoString}\n\n`;
});
return `
Nonii, katellaas vähä paljo peli
${retString.join("")}
${timeUntilDeadLineString()}
`;
};
bot.start((ctx) => ctx.reply("Se on raaka peli"));
bot.help((ctx) => ctx.reply("Kannattaa jo suunnata Alkoon"));
bot.on("text", async (ctx) => {
console.log(`${ctx.message.from.first_name}: ${ctx.message.text}`);
if (!!ctx.message.text) {
if (!validChatId((await ctx.getChat()).id)) {
ctx.reply("Kirjaa kaikki jutut chätin kautta");
} else if (ctx.message.text.includes("/latua")) {
// replace comma with dot for correct parsing. Round to 2 decimal with lodash
const userInput = ctx.message.text.split(" ")[1];
const kilometers = parseFloat(userInput?.replace(",", "."));
const kmRounded = _.round(kilometers, 2);
if (isNaN(kmRounded)) {
ctx.reply("lisää kilometrit komennolla /latua <kilometri>");
} else {
await db.writeRecordToDb(
ctx.message.from.id,
ctx.message.from.first_name,
new Date(),
kmRounded
);
ctx.reply(await cheersReply(ctx));
}
} else if (ctx.message.text.includes("/stats")) {
ctx.replyWithHTML(await statsReply(ctx));
}
}
});
bot.launch();
console.log("Ready");