-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
163 lines (136 loc) · 3.95 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
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
import { Telegraf, Context } from "telegraf";
import { join } from "path";
import { Low, JSONFile } from "lowdb";
import { formatDistance, parseISO } from "date-fns";
import { fi } from "date-fns/locale";
import { Update, Message } from "typegram";
import cron from "./lib/cron";
import { sakkoReply } from "./lib/sakkoReply";
import _ from "lodash";
const validChatId = (chatId: any) => {
// return true
return chatId === parseInt(process.env.CHAT_ID ?? "");
};
interface Entry {
userId: number;
timestamp: Date;
firstName: string;
}
type Data = {
log: {
userId: number;
timestamp: Date;
firstName: string;
}[];
sakko: {
userId: number;
timestamp: Date;
firstName: string;
}[];
};
require("dotenv").config();
// Use JSON file for storage
// if docker, use /data/db.json otherwise ./db.json
const file = process.env.DOCKER
? "/usr/src/app/db/db.json"
: join(__dirname, "./db.json");
console.log(process.env.DOCKER ? "yes docker" : "no docker");
console.log(process.env);
console.log(__dirname, file);
const adapter = new JSONFile<Data>(file);
export const db = new Low(adapter);
(async function () {
await db.read();
db.data ||= { log: [], sakko: [] };
})();
// Define your own context type
interface MyContext extends Context {
myProp?: string;
myOtherProp?: number;
}
export 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>>
) => {
await db.read();
const entries = db.data?.log;
const tonnis = entries?.filter((i) => i.userId === ctx.from.id).length;
return `
Se oli tonnin repäsy.
Hyvä homma ${ctx.message.from.first_name}! Taas voi pötcöttää pari viikkoa. Sinulla on nyt ${tonnis} tonnia kasassa
`;
};
const statsReply = async (
ctx: Context<{
message: Update.New & Update.NonChannel & Message.TextMessage;
update_id: number;
}> &
Omit<MyContext, keyof Context<Update>>
) => {
await db.read();
const entries = (db.data ||= { log: [], sakko: [] }).log;
const groupedEntries = _.chain(entries)
.groupBy("userId")
.mapValues((entry) => {
return {
amount: entry.length,
first_name: entry[entry.length - 1].firstName,
last: entry[entry.length - 1].timestamp,
};
})
.values()
.sortBy("amount")
.reverse()
.value();
const retString: string[] = groupedEntries.map((entry) => {
const agoString = formatDistance(parseISO(entry.last as any), new Date(), {
addSuffix: true,
locale: fi,
});
return `<b>${entry.first_name} - ${String(
entry.amount
)} tonnia</b>\nedellinen ${agoString}\n\n`;
});
return `
Nonii, katellaas vähä paljo peli
${retString.join("")}
`;
};
bot.start((ctx) => ctx.reply("Se on raaka peli"));
bot.help((ctx) => ctx.reply("Apua ei tule"));
bot.on("text", async (ctx) => {
if (!!ctx.message.text) {
if (!validChatId((await ctx.getChat()).id)) {
ctx.reply("Kirjaa kaikki jutut chätin kautta");
} else if (ctx.message.text.includes("/tonni")) {
db.data?.log.push({
userId: ctx.message.from.id,
timestamp: new Date(),
firstName: ctx.message.from.first_name,
});
await db.write();
ctx.reply(await cheersReply(ctx));
} else if (ctx.message.text.includes("/stats")) {
ctx.replyWithHTML(await statsReply(ctx));
} else if (ctx.message.text.includes("/sakkotilanne")) {
// add one sakko to user
ctx.replyWithHTML(await sakkoReply(ctx));
} else if (ctx.message.text.includes("/sakko")) {
// add one sakko to user
db.data?.sakko.push({
userId: ctx.message.from.id,
timestamp: new Date(),
firstName: ctx.message.from.first_name,
});
await db.write();
ctx.reply("Sakko lisätty");
}
}
});
bot.launch();
console.log("Ready");
cron();