Skip to content

Commit

Permalink
feat: add command guards
Browse files Browse the repository at this point in the history
  • Loading branch information
ematala committed Apr 27, 2023
1 parent ff23159 commit ee7372c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
25 changes: 18 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "dotenv/config";
import { Telegraf } from "telegraf";
import { schedule } from "node-cron";
import { duties, mapping, roomies } from "./data";
import { getTrash } from "./utils";
import { check, getTrash } from "./utils";
import { TRASHID } from "./constants";

if (!process.env.TELEGRAM_BOT_TOKEN)
Expand All @@ -13,13 +13,15 @@ if (roomies.length !== duties.length)

const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);

bot.command("start", (ctx) =>
bot.command("start", (ctx) => {
console.info(new Date(), "\nNEW USER\n", ctx.chat);
ctx.reply(
`Hi ${ctx.chat.type === "private" && (ctx.chat.first_name ?? "stranger")}`
)
);
);
});

bot.command("get", (ctx) => {
if (!check(ctx.chat.id)) return;
const { duty, done } = mapping.find(
({ roomie }) => roomie.id === ctx.chat.id
)!;
Expand All @@ -28,6 +30,7 @@ bot.command("get", (ctx) => {
});

bot.command("getall", (ctx) => {
if (!check(ctx.chat.id)) return;
const message = mapping
.map(({ roomie, duty, done }) =>
done
Expand All @@ -39,6 +42,7 @@ bot.command("getall", (ctx) => {
});

bot.command("done", (ctx) => {
if (!check(ctx.chat.id)) return;
const m = mapping.find(({ roomie }) => roomie.id === ctx.chat.id)!;
if (m.done) return ctx.reply("Du bist schon fertig");
else {
Expand Down Expand Up @@ -70,6 +74,7 @@ const remindTrash = () => {
};

bot.command("remind", async (ctx) => {
if (!check(ctx.chat.id)) return;
await remind();
ctx.reply("Ich habe die anderen daran erinnert ihre Dienste zu machen.");
});
Expand All @@ -85,10 +90,16 @@ const rotate = () => {
}
};

bot.command("rotate", rotate);
bot.command("rotate", (ctx) => {
if (!check(ctx.chat.id)) return;
rotate();
});

// rotate duties every monday at 10am
schedule("0 10 * * mon", rotate);

// rotate duties every sunday at 6pm
schedule("0 18 * * sun", rotate);
// send a reminder every sunday at 6pm
schedule("0 18 * * sun", remind);

// send out trash reminders every tuesday at 6pm
schedule("0 18 * * tue", remindTrash);
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export interface Roomie {
id: number;
name: string;
username: string;
}

export interface Duty {
Expand Down
4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { trash } from "./data";
import { roomies, trash } from "./data";
import { closestTo, format } from "date-fns";

const trashMap: Record<number, string> = {
Expand All @@ -23,3 +23,5 @@ export const getTrash = () => {
const date = format(closest, "yyyy-MM-dd");
return trash[date].map((t) => trashMap[t]);
};

export const check = (id: number) => roomies.map((r) => r.id).includes(id);

0 comments on commit ee7372c

Please sign in to comment.